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.

Summary

This lesson teaches you how to properly rename and delete files using Git commands rather than operating system tools. You'll learn `git mv` to rename files while maintaining Git's change tracking, and `git rm` to delete files with automatic staging. Both operations must be committed to take effect, ensuring your file changes are properly recorded in Git history.

Key points

  • Use `git mv old_filename new_filename` to rename files; Git tracks the change with a confidence percentage (100% if unmodified)
  • The `git mv` command stages the rename automatically, visible in `git status` as a rename operation
  • Use `git rm filename` to delete files; this stages the deletion in the staging area
  • Both rename and delete operations require `git commit` to finalize the change
  • Git's file operations provide tracking advantages over direct file system modifications

FAQ

Why use `git mv` instead of renaming files in the operating system?

Using `git mv` ensures Git properly tracks and stages the rename operation, providing better version control and history tracking compared to simple file system modifications.

What does the 100% confidence percentage mean when Git shows a rename?

The 100% confidence indicates Git recognizes the renamed file as the exact same file (unchanged content). If the file had modifications, the confidence would be lower than 100%.

Do I need to commit after using `git rm` to delete a file?

Yes, you must run `git commit` after `git rm` to finalize the deletion, as `git rm` only stages the deletion in the staging area.