GIT - 4.11 Rename and delete files
This lesson covers how to rename, move and delete files through Git, instead of using only the operating system. The starting point is a clean test repository on the master branch. We begin by creating a new file jupiter.html, adding a small piece of text inside it, then staging and committing it with git add jupiter.html and git commit -m "add file".
Renaming through Git instead of the OS
If we decide we do not like the file name anymore, we can rename it through Git. Doing it via Git has clear advantages over a plain OS rename, because Git records the operation explicitly as a rename instead of seeing one deletion plus one new file:
git mv jupiter.html pluto.html
After the command, git status shows that jupiter.html is being renamed to pluto.html, and a quick ls confirms that the file has already been renamed in the filesystem too. We commit the rename with git commit -m "rename". The log shows the operation with a similarity score in parentheses (100%): Git is fully confident that it is exactly the same file. If we had also modified the content before committing, that confidence level would be lower than 100%.
To remove a file we use git rm instead of the plain OS rm. This way Git automatically tracks the deletion:
git rm pluto.html
git commit -m "delete pluto"
Immediately after git rm, the file disappears from the filesystem (verifiable with ls), but git status reports that the deletion is staged and not yet finalized. A commit is required to record the change permanently. Once committed, git status returns to a clean working directory and the deletion appears in the history. Using git mv and git rm instead of OS-level moves and deletes keeps the history accurate and lets future readers understand what really happened.