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.
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.
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.
pwdto check your current path and also do an
ls, you can see level2-file.txt.
mv level2-file.txt level2.txt.
ls, you can notice that the file has been renamed.
git status, Git returns two operations such as 1. you deleted the old file and
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.
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"
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.
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.
lsto see, there is rename happened at the operating system level and do a
git status, Git says that we are on a clean directory.
git mv level2.txt
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.
git commit -m "moving file from level2 to level3"then save and close.
ls, you will see level2.txt and level3.txt
mv filename targetin this case as
mv level2.txt level2..
git status, it tells us that we have deleted the file and then created the new file.
git add -a.
git status, it says we have moved to file which is indicated by the file being renamed to a different folder.
git commit -m "moving level2 file back to level2 directory"then save and close.