Moving and Renaming Files in Git

Git Can Track the changes which have made in Git's working directory, we can move the file from one directory to another directory and also can change the file name as well.

If we rename any file, then Git tells us that we have deleted our old file and added a new file even if the contents are the same. This may happen only when you are working with IDE tools. where it moves the file for you and not aware of Git.

  • So here am going to show you how to Move and Rename the files in Git.
  • Am starting it with my current project folders where I have all my project details.

Now am going to my git repository and I will do an ls, you can see all the files which are associated with my projects. We can do this project inside the few levels as well.

So first I will do pwd to check currently am on which level, So, am currently on level1/level2/level3. Let's go to the level3 and do an ls, you can see that, the level3-file.txt file and do a git status, hence Git will show you that, the working directory is clean.

Now let's start with renaming the file level3-file.txt either by using operating system level or using the file manager or on windows using windows explorer through the command prompt. or else even we can use Git commands to do the rename or the move operation.

To rename the file am going to use Git's mv command as git mv current file name new file name that means git mv level3-file.txt level.txt and if do an ls, Git shows us that it has already renamed the file at the operating system level.

And also git status tells us that, it has staged the fact that, the rename has taken place but it has not committed the file, this means that we can still back out the rename at this level.

But here am going to move forward by committing this as git commit -m "renaming level3 file" and press enter. So the Git commit command confirms that git renamed our file from level3-file.txt to level3.txt.

Note: It is always better to rename your files before you make any changes because it will be easy for git to track renaming has happened or not.
  • Now let's go back to the level2 and do pwd to check your current path and also do an ls, you can see level2-file.txt.
  • I am going to rename this file also in the operating system level without using Git, Going to do this with bash's mv command, here am not using Git preface, mv level2-file.txt level2.txt .
  • If you do an ls, you can notice that the file has been renamed.
  • Now check git status, Git returns two operations such as 1. you deleted the old file and added the new file, but even if it seems to have the same content.

We need to add the fact that, we did the renaming process outside the git by using git add command, by using a parameter "-a". Here "-a" will recursively add any changes to the file and also update if any file has been renamed, moved or deleted.

  • If I do git status, it will say that the git has renamed the file again, hence I can proceed forward by committing git commit -m "renaming level2-file.txt"
  • Git commit tells us that we renamed the file from level2-file.txt to level2.txt.

Next, am going to show you how to rename the file and back it out, before that first check your path using pwd , So now you are in the level2 directory and let's do an ls, so you can see that there is level2-file.txt file.

Let's rename this file using git and back out by using the git mv command
git mv level2.txt 2.txt , and once again if you do an ls to see, the file has been renamed at the operating system level.

  • And check git status, Git says that it has staged the renaming.

Before committing, let's decide whether you don't want that file name as 2.txt or the original name as level2.txt.if you want the original name, we can set by using git's suggestion using reset command. Rather than that, we do have a simpler way just to use git mv command as git mv new filename old filename.

  • After that do an ls to see, there is rename happened at the operating system level and do a git status, Git says that we are on a clean directory.
  • Now let's move the file level2.txt to another directory and see whether we have in our level2.txt or not.
  • Let's move this file to level3 by keeping the same name as level2.txt as git mv level2.txt
  • Now go to level3 and do an ls , you can see the file here. and if you do git status, it states that we have moved the file from previous to the current directory.


  • Now let's do git commit -m "moving file from level2 to level3" then save and close.
  • Now we do not have any file in the level2 directory, so now go up to level3 and do an ls , you will see level2.txt and level3.txt
  • Having file level2.txt in the level3 directory doesn't make any sense, so let's move this file back to the level2 directory by using bash command "mv" as mv filename target in this case as
    mv level2.txt level2..
  • Where two dots indicates back to directory. and now go back one level and do an "ls" you can see the level2.txt file in the level2 directory.
  • Check git status, it tells us that we have deleted the file and then created the new file.
  • So to fix this, we are going to use command git add -a .
  • Once again do git status, it says we have moved to file which is indicated by the file being renamed to a different folder.
  • Now let's move forward and commit git commit -m "moving level2 file back to level2 directory" then save and close.
  • So git commit returns that, we have renamed or moved the file back to its original folder.
  • So let's go back to level1 directory.

GitHub synchronization