Git Remove and Move
Why git rm and git mv?
You can delete or rename files with your normal tools, but then you have to stage the change yourself.
git rm and git mv do both in one step: they change the file and stage the change for the next commit.
Remove a File (git rm)
Example: Delete a File
git rm notes.txt
rm 'notes.txt'
git status --short
D notes.txt
Try it Yourself »
The file is deleted from your folder, and the deletion is staged. Commit it like any other change:
Example: Commit the Deletion
git commit -m "Remove old notes"
[main 29b1760] Remove old notes
1 file changed, 1 deletion(-)
delete mode 100644 notes.txt
Try it Yourself »
Stop Tracking a File, But Keep It (--cached)
Use --cached to remove a file from Git but keep it in your folder. This is useful when you committed a file by mistake, like a log or a file with passwords:
Example: Untrack a File
git rm --cached notes.txt
rm 'notes.txt'
git status --short
D notes.txt
?? notes.txt
ls
index.html notes.txt style.css
Try it Yourself »
Add the file to .gitignore too, so it is not added again by mistake.
Remove a Folder (-r)
Use -r (recursive) to remove a folder and everything in it:
Example: Remove a Folder
git rm -r old-pages
rm 'old-pages/about.html'
rm 'old-pages/contact.html'
Try it Yourself »
Rename or Move a File (git mv)
git mv renames or moves a file, and stages the change:
Example: Rename a File
git mv style.css main.css
git status --short
R style.css -> main.css
Try it Yourself »
Example: Move a File into a Folder
git mv style.css css/style.css
git status --short
R style.css -> css/style.css
Try it Yourself »
Git keeps the history of a moved file. Use git log --follow to see the commits from before the move:
Example: History of a Moved File
git log --follow --oneline css/style.css
5cabbc0 (HEAD -> main) Move style.css into css folder
ebcff72 Style paragraphs
87e4062 First version
Try it Yourself »