Topic 1.6
Renaming, Removing & Cleaning Files: git mv, git rm, git clean
In one line
git mv old new renames a file and stages the rename. git rm file deletes it and stages the deletion. git rm --cached file stops tracking a file but keeps it on disk, which is how you un-commit a file that should have been ignored. git clean deletes untracked files: always preview with -n first. Git doesn't store renames, it detects them by comparing content, so git log --follow traces a file's history across renames.
Think of it like this
Reorganising a filing cabinet with an assistant who keeps the index. Moving a folder, shredding a page, or taking a page out of the index (but keeping it on your desk) are three different requests, and you have to tell the assistant which one you mean.
Words you'll meet
New words in this topic, in plain English. Come back here whenever one feels fuzzy.
- git mv
- Rename or move a tracked file and stage the change.
- git rm
- Remove a tracked file from disk and stage the removal.
- git rm --cached
- Remove a file from Git's tracking (the index) but leave it on disk.
- git clean
- Delete untracked files and folders from the working directory.
- Rename detection
- Git pairing a deleted file with a similar added file and showing it as a rename.
- --follow
- A
git logoption that keeps tracing a single file's history through renames.
Step by step
01Rename a file
The team decides src/cart.js should live in src/checkout/cart.js. Priya uses git mv, and status shows a clean rename. She commits the move alone, before any edits, so the history stays easy to follow.
02History across the rename
Plain git log on the new path stops at the move, as if the file was born there. --follow traces it back through the rename.
03Un-committing a file that should be ignored
Arjun's first commit included node_modules/ (40,000 files). He removes it from tracking without deleting it from disk, ignores it, and commits. It stays in old history, but future commits are clean.
04Cleaning build leftovers safely
After switching branches, Priya has stray build output and test reports. She previews first with -n, then deletes. Because -x would also remove ignored files like .env, she leaves it off.
Break it on purpose
Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.
Break #1
The case-only rename that broke Linux builds
On her Mac, Priya renames menu.js to Menu.js in Finder and updates imports to ./Menu.js. Git shows nothing to commit for the rename.
Myth vs fact
Myth
Git tracks renames.
Fact
It stores snapshots and detects renames afterwards by comparing content. That's why separate 'move' and 'edit' commits keep history readable.
Pro corner
Extra depth for experienced readers. New to this? Skip it for now and come back later.
- ▸
git clean -igives an interactive menu to choose what to delete, and a.gitignorethat matches your build output meansgit statusstays readable andcleanrarely needs-x.
Remember this
- 1
git mv old.js new.js= rename on disk and stage it. Renaming in your file manager works too, followed bygit add -A, because Git sees 'deleted old, added new' and pairs them up. - 2
Git detects renames by similarity (by default 50% of content the same). It doesn't record them. Rename *and* heavily edit in one commit, and Git may see a delete plus an unrelated new file. Rename in one commit, edit in the next.
- 3
git rm filedeletes the file and stages the deletion.git rm -r folder/for folders. - 4
git rm --cached file= stop tracking, keep the file. Use it when a file like.envornode_modules/was committed by mistake, then add it to.gitignore(Topic 1.4). If it contained a secret, see Topic 7.5. - 5
git cleandeletes untracked files (never tracked ones):-npreview,-fforce,-dinclude folders,-xalso ignored files,-iinteractive. These files aren't in Git, so they can't be recovered. - 6
Case-only renames (
menu.js→Menu.js) are invisible on Windows and macOS by default, because those filesystems ignore case. Usegit mv menu.js Menu.jsso Git records it.
Explain it without notes
What's the difference between git rm and git rm --cached?
Why is git clean dangerous?
Practice
Rename a file with git mv and trace its history.
Stop tracking a committed log file without deleting it.
Trade-offs
- ↔
Separate move and edit commits keep history and blame clear but add a commit.
git cleanis a quick reset of build clutter but is permanent, so previews and good ignore rules matter.
Done when you can
I rename with git mv and commit moves separately.
I use git rm --cached for files that should be ignored.
I preview git clean with -n.