Git Remove and Move

Updated · 13 min read
easyGit 2.53gitgit-rmgit-mvrenameinterview

When a library takes a book off its shelves, it is not enough to carry the book away. Someone also has to take its card out of the catalog, or readers will keep looking for a book that is no longer there. When a book moves to a new shelf, the catalog needs the new place.

Git is the catalog of your project. Deleting, renaming or moving a file in your folder is only half the job: the change also has to be staged and committed, so that the next snapshot matches your folder. git rm and git mv do both halves in one step.

In this lesson, you will learn how to delete a file, how to stop tracking a file while keeping it, how to rename and move files, and how Git recognizes a rename on its own.

The Starting Point

Navika's project has five committed files:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
echo 'Fees for 2025: $11,000' > old-fees.txt
echo "Ask about the hostel fee" > draft.txt
echo "db-password=chalk42" > secrets.txt
git add .
git commit --quiet -m "Add the first files"
git ls-files
Output
README.md
draft.txt
fees.txt
old-fees.txt
secrets.txt

git ls-files lists the files that Git tracks, the ones in the staging area. It is a quick way to see what the next commit would contain if you changed nothing.

Delete a File the Ordinary Way

First, Navika deletes old-fees.txt with the normal rm command, as she would delete any file:

Terminal
rm old-fees.txt
git status -s
Output
 D old-fees.txt

The file is gone from the folder, but Git only reports it. The D (a space, then D) means "deleted in the working tree, not staged". The deletion still has to be staged, the same way as any other change:

Terminal
git add old-fees.txt
git status -s
Output
D  old-fees.txt

Now it is D (D, then a space): the deletion is staged and will be part of the next commit. git add on a deleted file looks odd, but it means "stage the current state of this path", and the current state is "gone".

Delete a File with git rm

git rm does both steps at once: it deletes the file from your folder and stages the deletion.

Terminal
git rm draft.txt
git status -s
ls
Output
rm 'draft.txt'
D  draft.txt
D  old-fees.txt
README.md
fees.txt
secrets.txt

Git prints rm 'draft.txt' for each file it removes. Both deletions are now staged, and draft.txt is gone from the folder. Navika commits them together:

Terminal
git commit --quiet -m "Remove the old fee list and the draft notes"
git ls-files
Output
README.md
fees.txt
secrets.txt

Syntax

Terminal
git rm <file>...
OptionIt is used to
git rm <file>Delete the file and stage the deletion
--cachedStage the deletion but keep the file in your folder
-rRemove a folder and everything in it
-fRemove a file even though it has changes that were never committed
-nShow what would be removed, without removing anything

Git Protects Unsaved Work

Suppose fees.txt has an edit that was never committed, and Navika tries to remove it:

Terminal
echo 'Term 2 fee: $12,500' >> fees.txt
git rm fees.txt
Output
error: the following file has local modifications:
    fees.txt
(use --cached to keep the file, or -f to force removal)

Git refuses. Removing the file would throw away the new line, which exists nowhere else, not even in a commit. The message lists the two ways forward: --cached to stop tracking the file but keep it, or -f to delete it anyway. Navika wants to keep this file, so she commits the change instead:

Terminal
git commit --quiet -am "Add the term 2 fee"
git log --oneline -1
Output
5512aee (HEAD -> main) Add the term 2 fee

Stop Tracking a File but Keep It: git rm --cached

secrets.txt holds a password. It should never have been committed, but Navika still needs the file on her computer. --cached removes it from the staging area only, so Git stops tracking it, and the file stays in the folder:

Terminal
git rm --cached secrets.txt
git status -s
ls
Output
rm 'secrets.txt'
D  secrets.txt
?? secrets.txt
README.md
fees.txt
secrets.txt

How it works

  1. rm 'secrets.txt' is printed even with --cached. It means "removed from the staging area", not deleted from the disk.
  2. D secrets.txt says the deletion is staged. The next commit will no longer contain the file.
  3. ?? secrets.txt says the same file is still in the folder, now untracked.
  4. ls confirms that the file was not deleted from the disk.
Terminal
git commit --quiet -m "Stop tracking the secrets file"
git status -s
Output
?? secrets.txt

After the commit, the file still shows as ??, and git add . would track it again by accident. The next lesson, "Git .gitignore", shows how to tell Git to ignore it for good.

Three ways to remove a file

Rename a File with git mv

git mv renames or moves a file and stages the change in one step:

Terminal
git mv fees.txt fee-list.txt
git status -s
Output
R  fees.txt -> fee-list.txt
?? secrets.txt

R means renamed, and the arrow shows the old name and the new one. The ?? secrets.txt line is the untracked file from the previous section, and it will stay there until the next lesson. Moving a file into a folder works the same way. The folder must exist first:

Terminal
mkdir docs
git mv README.md docs/
git status -s
git commit --quiet -m "Rename the fee list and move the README into docs"
Output
R  README.md -> docs/README.md
R  fees.txt -> fee-list.txt
?? secrets.txt

Syntax

Terminal
git mv <old-path> <new-path>
git mv <file>... <folder>/
OptionIt is used to
git mv <old> <new>Rename a file and stage the rename
git mv <file> <folder>/Move a file into an existing folder
-fOverwrite a file that already has the new name
-nShow what would happen, without doing it

Rename Without git mv

You don't have to use git mv. If you rename a file in your file manager, your editor or with the ordinary mv command, Git first sees a deleted file and a new one:

Terminal
mv fee-list.txt term-fees.txt
git status -s
Output
 D fee-list.txt
?? secrets.txt
?? term-fees.txt

Once both sides are staged, Git compares them, sees that the content is the same, and reports a rename. Navika stages the two paths by name. git add -A would also work, but it would stage the untracked secrets.txt as well:

Terminal
git add fee-list.txt term-fees.txt
git status -s
git commit --quiet -m "Rename the fee list to term-fees.txt"
Output
R  fee-list.txt -> term-fees.txt
?? secrets.txt

So git mv old new is a shortcut for three commands: mv old new, git rm old (staging the deletion) and git add new. The result is exactly the same.

Common Mistakes

Deleting a file and forgetting to stage the deletion. git status keeps showing D, and the file stays in the next commit's snapshot. Run git add <file> or git rm <file>.

Using git rm when you meant git rm --cached. Plain git rm deletes the file from your folder. If it had no unsaved changes, you can get it back from the last commit, but to stop tracking a file that you still need, use --cached.

Thinking git rm --cached hides a committed secret. It only stops tracking from now on. Earlier commits still contain the file, so change any password that was committed.

Forcing with -f without reading the message. The refusal is there because the file has changes that exist nowhere else. Commit them, or be sure you don't need them, before you force.

Expecting git mv to create the target folder. It doesn't. Run mkdir first.

Renaming and rewriting a file in the same commit. If the content changes too much, Git can no longer match the old and new files, and history shows a deletion and a new file. Rename in one commit and make big edits in the next.

Under the Hood

A commit doesn't record "this file was renamed". It records a snapshot of the whole project, and nothing more. Every time you ask, Git compares two snapshots, notices that one path disappeared and a new path appeared with the same or very similar content, and reports a rename. By default, two files count as a rename when they are at least 50% the same.

This is why git mv and a plain mv followed by staging both paths give exactly the same commit. It is also why git log with a file name stops at the rename: it follows the path, not the content. Add --follow to make git log look across renames:

Terminal
git log --oneline -- term-fees.txt
git log --oneline --follow -- term-fees.txt
Output
7debfe5 (HEAD -> main) Rename the fee list to term-fees.txt
7debfe5 (HEAD -> main) Rename the fee list to term-fees.txt
05d3653 Rename the fee list and move the README into docs
5512aee Add the term 2 fee
c573f3b Add the first files

Without --follow, the history of term-fees.txt starts at the commit that gave it that name. With --follow, Git traces the content back through fee-list.txt and fees.txt to the first commit. The -- separates options from file names, so Git never mistakes a file name for something else.

Interview Corner

What is the difference between rm and git rm?

rm deletes the file from the working tree only, and the deletion still has to be staged with git add. git rm deletes the file and stages the deletion in one step. Either way, the file is removed from the next commit and stays in earlier ones.

How do you stop tracking a file without deleting it?

git rm --cached <file>. It removes the file from the index (staging area), so the next commit no longer contains it, but leaves it in the working tree as an untracked file. Add it to .gitignore so it isn't added again by accident.

Does Git track renames?

Not directly. A commit stores only a snapshot. Git detects renames when it compares two snapshots: a removed path and an added path with similar content (50% by default) are shown as a rename. git log --follow <file> uses this to show a file's history across renames.

What does git mv do?

It renames or moves the file in the working tree and stages the change, the same as running mv, then git rm on the old path and git add on the new one. The resulting commit is identical either way.

Why does git rm sometimes refuse to remove a file?

When the file has staged or unstaged changes that are not in the last commit, removing it would lose that work. Git stops and suggests --cached (keep the file, stop tracking it) or -f (remove it anyway).

Practice

Challenge: rename, then edit. Continue in the same repository. Rename term-fees.txt to fees-2026.txt with git mv, then add the line Term 3 fee: $13,000 to the renamed file without staging that edit. Print the short status.

What does Git print? Before you open the solution, decide what the two letters at the start of the line will be.

Show solution
Terminal
git mv term-fees.txt fees-2026.txt
echo 'Term 3 fee: $13,000' >> fees-2026.txt
git status -s
Output
RM term-fees.txt -> fees-2026.txt
?? secrets.txt

RM combines two facts. The left letter, R, is the staging area: a rename is staged. The right letter, M, is the working tree: the file was modified after that, and the new line is not staged yet. Run git add fees-2026.txt and the line becomes a plain R. The ?? secrets.txt line is unchanged: that file is still untracked.

Checkpoint. In a practice repository, delete one file with rm, one with git rm, and stop tracking a third with git rm --cached. Run git status -s after each step and explain each two-letter code before you commit.

Quick check: Remove and move

4 questions
1Which command deletes a file from your folder and stages the deletion?
2You want Git to stop tracking app.log, but keep the file. What do you run?
3What does R old.txt -> new.txt mean in git status -s?
4Why does git log new.txt stop at the commit that renamed the file?

Key Takeaways

Quick reference

CommandIt is used to
git ls-filesList the files Git tracks
git rm <file>Delete a file and stage the deletion
git rm --cached <file>Stop tracking a file, keep it in the folder
git rm -r <folder>Remove a folder and its files
git mv <old> <new>Rename a file and stage the rename
git mv <file> <folder>/Move a file into a folder
git log --follow -- <file>Show a file's history across renames

Was this tutorial helpful?

Lesson 15 of 106