Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Remove a File

git rm old-script.sh
git commit -m "Remove old deploy script"

git rm does two things at once: it deletes the file from your working tree and stages the deletion. After the commit, the file is gone from the current version of the repository. Other clones lose the file when they pull that commit.

git rm or Deleting the File Yourself

You can also delete the file with the normal rm command and stage the change afterward:

rm old-script.sh
git add old-script.sh

The result is the same commit. git status shows the file as deleted in both cases; git rm only saves the second step. git add -A stages every deletion in the repository at once.

Stop Tracking a File but Keep It

A common case: a configuration file such as .env was committed by mistake and should stay on your machine but leave the repository.

git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"

--cached removes the file from the index only. The .gitignore line keeps Git from offering to add it again; the patterns are explained in how to use .gitignore. One side effect to know: when others pull this commit, Git deletes the file from their working trees, so tell them to keep a copy first.

Remove a Folder

git rm -r old-assets
git rm -r --cached node_modules

-r is needed for folders. The second line is the usual fix for a dependency folder committed by mistake: it stays on disk and leaves the repository. Add it to .gitignore as well.

Preview and Patterns

git rm -n -r old-assets
git rm '*.log'

-n (dry run) lists what would be removed without removing it. In the second line the quotes matter: Git expands the pattern itself and matches tracked files only, including those in subfolders.

Files with Uncommitted Changes

If a file has changes that are not committed, git rm refuses, so you do not lose them by accident. Commit or stash the changes, or keep the file with --cached.

Warning: -f forces the removal and discards those uncommitted changes for good.

git rm -f draft.md

Undo a Removal Before the Commit

To bring the file back into both the index and your working tree:

git restore --staged --worktree old-script.sh

After git rm --cached, git restore --staged file is enough, because the file is still on disk. If the removal is already committed, restore the file from the commit before it with git restore --source=HEAD~1 old-script.sh, or undo the whole commit as shown in how to undo the last commit in Git.

The File Stays in History

git rm removes a file from now on. Every earlier commit still contains it, and anyone with a clone can check out an old commit and read it. That matters for secrets: a password, key, or token that was ever committed and pushed must be treated as exposed.

  1. Revoke or change the secret first, wherever it is used. This is the step that actually protects you.
  2. Remove the file with git rm --cached and add it to .gitignore.
  3. If the file must disappear from history too, rewrite the history with a dedicated tool such as git filter-repo, then force push, as described in how to force push safely. Everyone has to clone again afterward.

Rewriting history does not reach copies that others already made, which is why the first step comes first.

Was this answer helpful?
Loading...