Moving, Deleting and Untracking Files
What you will learn on this page
- Rename tracked files with
git mv - Delete tracked files with
git rm - Use
git rm --cachedto stop tracking a file while keeping it on your machine - Restore a file you deleted by mistake from the last commit
On this page10 items
In Part 2, #12, we saw that .gitignore has no effect on files that have already been committed. This page covers the correct steps for moving, deleting and untracking files that are already tracked. Each of these commands performs the file operation and reports it to Git in a single step.
Tracking states and the commands that change them#
From Git’s point of view, every file in the folder is either “tracked” (committed at least once) or “untracked.” The .gitignore from Part 2, #12 further sorts untracked files into “ignored.”
| What you want to do | Command | File | Tracking |
|---|---|---|---|
| Make it something to record | git add | Unchanged | Untracked → tracked |
| Rename or move it | git mv <old> <new> | Name changes | Stays tracked |
| Delete it | git rm | Removed | Stops being tracked |
| Keep it locally but stop tracking it | git rm --cached | Unchanged | Tracked → untracked |
When you run any of these commands, the change is only staged. To record it, you need git commit as usual.
Renaming: git mv#
Rename memo.txt to shopping.txt, a name that matches its contents.
cd ~/git-practice
git mv memo.txt shopping.txt
git status
# Output:
# On branch main
# Changes to be committed:
# (use "git restore --staged <file>..." to unstage)
# renamed: memo.txt -> shopping.txtgit commit -m "Rename memo.txt to shopping.txt"From the next page on, the shopping list is referred to as shopping.txt.
If you rename without git mv#
Renaming in Finder, File Explorer or with the mv command is no problem. However, Git sees it like this:
mv todo.txt todo-list.txt
git status -s
# Output:
# D todo.txt
# ?? todo-list.txtIt looks as if “todo.txt disappeared and an unknown file called todo-list.txt appeared.” When you git add both, Git notices that the contents are almost identical and regroups them as a rename.
git add todo.txt todo-list.txt
git status -s
# Output:
# R todo.txt -> todo-list.txtgit mv is a command that does this “rename, then git add both” in one step. The result is the same, so you can use either.
Rename the todo-list.txt from this exercise back to its original name.
git restore --staged todo.txt todo-list.txt
mv todo-list.txt todo.txtDeleting: git rm#
git rm todo.txt
# Output:
# rm 'todo.txt'The file disappears from the working tree, and the change “deleted” is staged. If you git commit now, todo.txt won’t be included in later commits. However, it still remains inside past commits. If you need the deleted file again, you can retrieve it from the history.
This time we’ll keep it, so restore it.
git restore --staged todo.txt
git restore todo.txtThe first line unstages the deletion, and the second writes the file back from the last commit.
If you delete a file by mistake#
It’s the same if you delete a file normally without git rm.
rm todo.txt
git status -s
# Output:
# D todo.txt
git restore todo.txtIf you haven’t committed yet, git restore <file> brings back the last committed contents. What comes back is the contents as of the last commit, so edits you hadn’t committed won’t come back.
You can’t delete a file you’re editing#
echo "Go to the post office" >> todo.txt
git rm todo.txt
# Output:
# error: the following file has local modifications:
# todo.txt
# (use --cached to keep the file, or -f to force removal)If you git rm a file that has uncommitted changes, Git stops you. Those changes aren’t recorded anywhere, so deleting the file would lose them forever. Once you’ve seen this, discard the line you added (git restore todo.txt).
Stop tracking only: git rm –cached#
Let’s recreate a situation where you accidentally committed a config file containing a password.
echo "API_KEY=abc" > config.txt
git add config.txt
git commit -m "Add settings"You need this file locally, but you don’t want it in the records. Adding --cached stops Git from tracking it while keeping the file on your machine.
git rm --cached config.txt
# Output:
# rm 'config.txt'
git status
# Output:
# On branch main
# Changes to be committed:
# (use "git restore --staged <file>..." to unstage)
# deleted: config.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# config.txtGit sees it as “deleted from the records” and “present locally as an untracked file.” Left like this, the next git add . would bring it back, so also add it to .gitignore at the same time and commit both together.
echo "config.txt" >> .gitignore
git add .gitignore
git commit -m "Stop tracking config.txt"Now config.txt stays on your machine, isn’t included in later commits, and no longer appears in git status.
FAQ#
Q. Can I delete or move a whole folder?
A. Yes. Use git mv <old-folder> <new-folder>, and to delete a folder, git rm -r <folder>. -r means “including its contents.”
Q. Is this the same git rm --cached that came up in Part 2, #08?
A. Yes. It’s the command Git suggested as a hint for unstaging before the first commit. Since there were no records yet, “unstaging” and “stopping tracking” meant the same thing.
Summary#
git mvstages a rename andgit rmstages a deletion, at the same time as performing the file operation- Git determines renames by comparing the contents before and after
- Even a deleted file can be restored to its last committed contents with
git restore, as long as you haven’t committed git rm --cachedstops tracking a file while keeping it on your machine. Use it together with.gitignore- Untracking a file doesn’t erase its contents from past commits
Next page#
14. Undoing Things, Part 1 (restore, reset, amend) ── Learn which method to use to “make it as if it never happened” at each stage: edits, the staging area and the last commit.