🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

Code has been added to clipboard!

Moving, Renaming and Removing Files at Git Repositories

Reading time 4 min
Published Jan 31, 2018
Updated Oct 3, 2019

After getting familiar with the previous tutorials, you can now create files, Git add folder and save changes in repositories. For this, you always use specific commands. However, mistakes happen and sometimes not all the files end up placed where they are supposed to be. Sometimes you will find yourself needing to move a file from one repository to others or simply to delete it.

How does Git move files? How to remove a file from being tracked and exchange it with another one? These are common questions to ask; however, in the language of Git, it would sound more like how to undo files from the staging area. You can remove a file by using Git rm command and add a new one or do the same action by executing Git move files command abbreviated as Git mv. The result is the same: a file will be removed from the repository.

If you simply delete a file from your working directory, it will be regarded as a change which has not been committed. You will learn in this tutorial how to make these changes permanent and how to use Git move files, and Git mv commands in order to freely operate your Git files.

Start reading below!

Git Move Files: Main Tips

  • Just like you can use Git add folder or files, so can you delete them, too.
  • You can use git rm command to remove the files from a repository and git mv command to remove and instantly add a new file instead of the old one.

Removing Git Files

git rm command is usually used when a developer wants to remove a certain file from the working tree and the index, not only from the working directory. This process is called Git remove or Git remove file. It means that after running this command on certain files, changes in them will not be staged and you will not see it as a tracked file the next time you perform a status check.

There might be cases when you want to keep a file on your hard drive, but want that Git would stop tracking it or you want to remove one of them from the staging so that you wouldn't commit it accidentally. To do this, add --cached option:

$ git rm --cached text.txt

Just like you stage multiple files, you can do the same only in reverse, by removing batches of files:

$ git rm --\*a

Git remove or Git remove file command will eliminate all the files with 'a' name ending. If you use, for example, .db, it will remove all the files with db extensions.

Note: Add a / forward slash for the directory, e.i. myproject/. There is a backslash in an example above \ in front of * because Git has its own file extension.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

Moving Git Files and the Process Behind It

Paradoxically, even though Git is a file version control system, it doesn't track the movement of the files. It means that no metadata is saved if you rename or move a file. However, Git mv command facilitates the process as you don't need to manually delete a file and add a new one. You can use it as in the following example:

.$ git mv from__this_file to this _file

The same can be done by removing a file and adding a new file with the same content. You can use other ways to rename files as well, only that it will require you to run more commands as you still will have to add the file and remove the other version by using rm:

$ mv file_name1 file_name2
$ git rm name1
$ git add file_name2

Git Move Files: Summary

  • Files are flexible to move from one place to another or to be deleted completely.
  • Git rm and Git mv are two main commands to execute these actions.