Git rm and mv: Delete and Rename Files

When you delete or rename a file from the normal file manager, Git notices it but you still need to tell Git about it. The commands git rm and git mv do the file operation and stage it in one step.

Delete a file with git rm

Terminal output

$ git rm a.txt
rm 'a.txt'
$ git status -s
D a.txt

The file is removed from the folder, and the deletion is staged (D). Now you can commit it.

Terminal output

$ git commit -m "Remove a.txt"
[main 8837906] Remove a.txt
1 file changed, 1 deletion(-)
delete mode 100644 a.txt

If you deleted the file by hand

If you used the file manager or the rm command, Git shows it as deleted but not staged. Stage it with git rm (or git add).

Terminal output

$ rm b.txt
$ git status -s
D b.txt
$ git rm b.txt
rm 'b.txt'
$ git status -s
D b.txt
$ git commit -q -m "Remove b.txt"

Remove a folder

Terminal output

$ git rm -r old
rm 'old/x.txt'
rm 'old/y.txt'
$ git status -s
D old/x.txt
D old/y.txt
$ git commit -q -m "Remove old folder"

Stop tracking a file but keep it on disk

The --cached option removes the file from Git only. The file stays in your folder. It is used when you added a file by mistake, for example a file with passwords.

Terminal output

$ git rm –cached c.txt
rm 'c.txt'
$ git status -s
D c.txt
?? c.txt
$ ls
c.txt
d.txt
$ git commit -q -m "Stop tracking c.txt"

Rename or move a file with git mv

Terminal output

$ git mv d.txt letters.txt
$ git status -s
R d.txt -> letters.txt
?? c.txt

Git shows R (renamed). The git mv command is the same as doing three steps: rename the file, git rm the old name, and git add the new name.

Terminal output

$ mkdir docs
$ git mv letters.txt docs/letters.txt
$ git status -s
R d.txt -> docs/letters.txt
?? c.txt
$ git commit -q -m "Move letters.txt to docs"
$ git log –oneline –follow — docs/letters.txt
5a0a64e (HEAD -> main) Move letters.txt to docs
d7a1368 Add files

The option --follow in the log keeps showing the history of a file after it was renamed.

Useful options

OptionMeaning
-rRemove a folder and everything inside
-fForce the removal, even if the file has changes
--cachedRemove from the repository only, keep the file on disk
-nDry run, only show what will be removed
Note: Deleted files are not lost. They are in the history, so you can bring them back with git restore. Next: Git Restore.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Check the status of files in a Git repository
Stage only selected files and commit them separately
Show the commit history in one line per commit
See the changes of a file before staging and before commit
Ignore all .log files with .gitignore
Unstage a file
Delete a file with git rm
Interview Questions & Answers
How can you add files to Git?
How can you commit a file in Git?
What are the common files to ignore in Git?
How to remove untracked files in Git?
What is the staging area in Git?
What is the difference between git add and git commit?
What is HEAD in Git?