inter-git

Renaming and Removing Files the Long Way

Renaming and Removing Files the Long Way
You learned the git mv and git rm commands to rename and remove files.
These commands are actually shortcuts for using the git add command. You can achieve the same results by manually renaming or deleting files and then updating the Index with git add.

Renaming a file:

First, rename the file in the Working Directory, then use git add <filename> to add the renamed file to the Index.

For example, if you have a file named my-file in your Working Directory, you can rename it to something like file-1. Then, use the git add file-1 command to add the renamed file to the Index. This updates the file name in both the Working Directory and the Index.

Removing a file:

First, delete the file in the Working Directory, then use git add <filename> to update the Index.

For example, if you have a file named my-file in your Working Directory, you can delete it from there. Then, if you run the git add my-file command, Git will notice that the file no longer exists in the Working Directory. As a result, it will remove the file from the Index as well.

You might be wondering: "Why not just delete the file from the Working Directory and leave it at that?"
The issue is that if you only delete the file from the Working Directory, it still remains in the Index. Since Git saves everything in the Index when making a commit, the file would still be included in the next commit.
That’s why you need to remove it from the Index as well, ensuring it’s not stored in the next commit.

Lesson Completed!