Home·Terminal commands

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

FlagMeaning
--cachedstop tracking but leave the file on disk
-rrecurse into directories
-fremove even when the file has unstaged changes
-ndry run
--ignore-unmatchexit 0 even if nothing matched

Examples

git rm --cached .env

stops 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