git rm
git
Usage
git rm [--cached] [-r] <pathspec>Removes a file from the index and from disk; --cached keeps the file and only stops tracking, but the old content stays in history, so a leaked secret is still there.
Common flags
| Flag | Meaning |
|---|---|
| --cached | stop tracking but leave the file on disk |
| -r | recurse into directories |
| -f | remove even when the file has unstaged changes |
| -n | dry run |
| --ignore-unmatch | exit 0 even if nothing matched |
Examples
git rm --cached .envstops tracking a secret file while keeping it locally
git rm -r build/deletes the folder from the repository and from disk
git rm -n "*.log"shows what would be removed
The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does git rm do?
Removes a file from the index and from disk; --cached keeps the file and only stops tracking, but the old content stays in history, so a leaked secret is still there.
Q. How do I type it?
git rm [--cached] [-r] <pathspec> — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
5 are listed here; the full set is in man git. This command sits under git.
Related commands
man page: man git-rm