Renaming, moving and deleting files
Use git mv and git rm to rename, move and delete tracked files, and learn that Git detects renames by comparing content rather than recording them.
Renaming, moving and deleting files
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- git mv renames or moves a file and stages the change in one step, and git rm deletes a file and stages the deletion.
- Git does not record renames. It spots them afterwards by finding a deleted file and an added file with mostly the same content.
- A plain mv or rm in your file manager works too, because git add -A stages both halves and Git then detects the rename.
- git rm refuses a file with uncommitted edits unless you pass -f, and git rm --cached untracks a file but leaves it on disk.
- git log -- <path> stops at a rename, so use git log --follow -- <path> to see a file's history from before it was moved.
Why this deserves its own lesson
Renaming looks like the simplest operation there is. In Git it’s a small puzzle, and understanding it removes a whole class of confusion about “why does my history show a delete and an add”.
The puzzle comes from the storage model. A commit is a snapshot, a tree of names pointing at blobs. There is no “rename” record anywhere in it. A renamed file is a snapshot where one name is missing and another name points at the same blob. Git has to work out that it was a rename.
git mv
I added a design note to tinynotes and committed it, then moved it into a docs folder and changed the extension:
$ git mv design.txt docs/design.md
$ git status -s
R design.txt -> docs/design.md
git mv does the move on disk and stages the result in one step. The R means Git sees a rename. The commit reports it too:
$ git commit -m "Move design notes into docs"
[main ecbaa7f] Move design notes into docs
1 file changed, 0 insertions(+), 0 deletions(-)
rename design.txt => docs/design.md (100%)
The 100% is similarity: the content is identical. No new blob was needed, because the file’s content didn’t change. git mv and git mv -n (a dry run that lists what it would do) work on folders too. Renaming docs to documentation worked as a single command, and the status afterwards showed R docs/design.md -> documentation/design.md. git mv refuses a missing source (“bad source”) or an existing destination (“destination exists”).
Renames are detected, not recorded
To prove it, I renamed a file with a plain mv, with Git not involved:
$ mv docs/design.md docs/overview.md
$ git status -s
D docs/design.md
?? docs/overview.md
A deletion and a new file, no rename. Stage both and Git connects them:
$ git add -A
$ git status -s
R docs/design.md -> docs/overview.md
So git mv is a convenience, a mv plus an add. You can use your file manager or editor’s rename and then run git add -A. The one requirement is that the old and new paths are staged together. Stage only the new file and Git has no deletion to match it against.
Detection tolerates edits. I renamed the file and added a line, staged both, and git diff --staged -M --stat printed docs/{design.md => overview.md} | 1 +. Git decided it was still the same file, because it was mostly unchanged. The default threshold is about half the content, per Git’s documentation, and I did not test where the line falls. If you rewrite most of a file as you move it, Git will report a delete and an add. A good habit: make the rename its own commit, and edit the file in the next one.
Following history across a rename
Here is the catch. History is stored per path, so asking for one path stops where that path began:
$ git log --oneline -- docs/design.md
ecbaa7f Move design notes into docs
$ git log --oneline --follow -- docs/design.md
ecbaa7f Move design notes into docs
f416046 Add design notes
Without --follow, the earlier commit under the old name is invisible. --follow makes Git track the file through renames. It works for one file at a time.
git rm
git rm deletes a tracked file and stages the deletion:
$ git rm docs/design.md
rm 'docs/design.md'
$ git status -s
D docs/design.md
If you had already deleted it by hand, git status -s shows D (unstaged deletion), and git rm on the missing file stages it. There’s a safety check. When the file has uncommitted edits, Git won’t delete it:
$ git rm README.md
error: the following file has local modifications:
README.md
(use --cached to keep the file, or -f to force removal)
-f forces it, throwing away your edits. Remember from the undoing lesson that such edits can’t be recovered.
--cached is the other option. It removes the file from the staging area only, so Git stops tracking it, and it stays on disk:
$ git rm --cached README.md
rm 'README.md'
$ git status -s
D README.md
?? README.md
The staged D plus an untracked ?? for the same name says exactly that: gone from Git, still in the folder. You use it to untrack something you committed by mistake, as in the gitignore lesson.
Getting a deleted file back
An accidental delete isn’t a disaster as long as the file was committed. I deleted notes.py from a scratch clone and ran git restore notes.py, and it returned from the last commit. The older command for this was git checkout -- notes.py.
A trap on macOS and Windows
Those systems usually treat README.md and readme.md as the same file. Renaming only the capitalisation is awkward there, because the file system sees no change, so use git mv and check git status. On my Mac git mv README.md readme.md worked, and Git reported R README.md -> readme.md. I couldn’t test Windows. On Linux the two names are different files.
What the video covers
The embed plays two chapters of freeCodeCamp’s “Git & GitHub Crash Course for Beginners [2026]” (taught by Sumit Saha, published December 2025): “git rm: Deleting Files” from 35:32 and “git rm –cached: Stop Tracking Files” up to 39:50, about four minutes of a 81-minute video, taken from the chapter list. That covers the delete half of this lesson. The chapter list has no entry for git mv, so renaming and moving, rename detection and --follow are not covered, and CodeLucky’s short git mv video is linked for the rename half. I couldn’t read a transcript. The channel is English-language, judged from the title and description.
Where this fits
That finishes everyday work on a single line of history. Everything so far happened on one branch, and the next group is about making more of them, starting with what a branch is: what a branch is.
Check yourself
Does Git store a rename as a special event?
No. A commit stores snapshots. Git compares two snapshots and, when a deleted file and an added file are similar enough, reports it as a rename.
You renamed a file with your file manager. What do you run so Git sees one rename and not a delete plus a new file?
git add -A (or git add on both paths). Staging the deletion and the new file together lets Git match them as a rename.
What is the difference between git rm file and git rm --cached file?
git rm deletes the file from disk and stages the deletion. --cached stages the deletion only, so the file stays on disk as an untracked file.
Why does git log -- docs/design.md show only one commit for a file that has been around longer?
The file had another name before the rename, and plain log follows the current path only. git log --follow -- docs/design.md follows it across the rename.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.