How to Remove a File From a Git Commit
To take a file out of your last commit but keep it on disk, run git rm --cached file and then git commit --amend --no-edit. The commit is rewritten without the file, and the file stays in your folder as an untracked file.
Quick answer
git rm --cached .env
git commit --amend --no-edit
echo ".env" >> .gitignore

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 18 November 2026, and the output shown is copied from the terminal. Your commit hashes will differ.
We committed a .env file together with real code. Here’s what the commit contained:
$ git show --stat --oneline HEAD
a248b7b Add payments
.env | 1 +
pay.js | 1 +
2 files changed, 2 insertions(+)
The fix many guides get wrong
A lot of answers say “unstage the file with git reset HEAD file, then amend”. After the commit, that does nothing at all:
$ git reset HEAD .env
$ git status --short
No output, nothing changed. git reset HEAD .env makes the staging area match HEAD, and HEAD is the commit that already contains .env. You have to either remove the file from the index (below) or reset it to the commit before (git reset HEAD~1 -- .env).
Remove it from the commit, keep the file
$ git rm --cached .env
rm '.env'
$ git commit --amend --no-edit
[main 986c9d5] Add payments
Date: Wed Nov 18 10:37:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 pay.js
$ git show --stat --oneline HEAD
986c9d5 Add payments
pay.js | 1 +
1 file changed, 1 insertion(+)
$ git status --short
?? .env
The commit now only contains pay.js, and .env shows as untracked (??), still on disk with your settings in it. Add it to .gitignore so it doesn’t sneak back in next time you run git add ..
Remove it from the commit and delete it
For a file you never wanted at all, like a database dump, drop --cached:
$ git rm dump.sql
rm 'dump.sql'
$ git commit --amend --no-edit
[main f83ffbb] Add payments
Date: Wed Nov 18 10:49:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 pay.js
$ git show --stat --oneline HEAD
f83ffbb Add payments
pay.js | 1 +
1 file changed, 1 insertion(+)
git rm without --cached deletes the file from your folder too.
Remove a file from an older commit
If more commits came after, use interactive rebase and stop at the commit that added the file. Run git rebase -i HEAD~2 (enough to reach it), change pick to edit on that line, save and close. Git pauses there:
$ git rebase -i HEAD~2 # changed 'pick' to 'edit' on "Add payments"
Stopped at 31dba9d... Add payments
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
$ git rm --cached keys.txt
rm 'keys.txt'
$ git commit --amend --no-edit
[detached HEAD ea9ad39] Add payments
Date: Wed Nov 18 10:57:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 pay.js
$ git rebase --continue
Successfully rebased and updated refs/heads/main.
$ git log --oneline --stat -2
cd7bd47 Add tests
ea9ad39 Add payments
pay.js | 1 +
1 file changed, 1 insertion(+)
The later commit (“Add tests”) was replayed on top, and keys.txt is no longer in “Add payments”.
Already pushed?
All of the above rewrites the commit, so the remote will reject a normal push. On your own branch, push with git push --force-with-lease. On a shared branch, a simpler route is a new commit that removes the file (git rm --cached file, then git commit -m "Stop tracking file"). The file stays in old commits, which is fine for a stray build file.
git filter-branch.Related guides
- How to Undo the Last Commit in Git (and Keep Your Changes)
- How to Change a Git Commit Message (Even After Pushing)
- How to Discard Local Changes in Git
- Common Git mistakes and how to fix them
FAQ
How do I remove a file from the last commit but keep it locally?
Run git rm --cached <file>, then git commit --amend --no-edit. Add the file to .gitignore so it isn’t committed again.
Why doesn’t git reset HEAD <file> remove it from the commit?
Because HEAD already contains the file, so resetting to HEAD changes nothing. Use git rm --cached or git reset HEAD~1 -- <file> before amending.
How do I remove a file from an older commit?
Run git rebase -i back to that commit, mark it edit, then git rm --cached <file>, git commit --amend --no-edit and git rebase --continue.
What if I committed a secret?
Revoke and replace the secret first. Then remove it from history with git filter-repo and force push. Treat the old value as leaked either way.