Git, what if you forgot to add a file to a commit?

By

Published · Updated

Tagged: Git

Forgot to add a file to your last Git commit? Use git commit --amend to add it, keep or change the message, fix an older commit, undo the amend, and push it safely.

This is common, you commit something but realize you forgot to include a specific file, maybe because you forgot to run git add to stage it.

No worries - you can use git commit --amend to create a replacement for the previous commit that includes everything currently staged:

git add file-forgotten.txt
git commit --amend

If you need, you can also change the commit message while you’re adding the file, using the -m option:

git commit --amend -m "New commit message"

As with any operation that rewrites the history, I would only use it if you are working on a local branch, or if you are 100% sure no one else is working on the same branch.

Keep the same message without the editor

A plain git commit --amend opens your editor with the old message, so you can edit it. Most of the time you just want to add the file and move on. Use --no-edit:

git add file-forgotten.txt
git commit --amend --no-edit

The message stays as it was, and no editor opens.

Amend creates a new commit

--amend doesn’t change the old commit. It replaces it with a new one, with a new hash. You can see it with git log --oneline before and after:

4c722d7 Add app    # before
a8aa004 Add app    # after the amend

The new commit keeps the same parent and the same author. If you want the commit to show you as the author with the current time (for example, you amended someone else’s commit), add --reset-author.

The new hash is why amending a pushed commit causes trouble, as you’ll see below.

Before you amend, check what’s staged

--amend takes everything in the staging area, not only the file you just added. If you had other changes staged, they end up in the commit too.

Run this first:

git status

Or see the exact changes that will go in:

git diff --staged

You committed a file you didn’t want

It works the other way too. Say you committed debug.log by mistake. Remove it from the staging area without deleting it from disk, then amend:

git rm --cached debug.log
git commit --amend --no-edit

The file is gone from the commit but still in your folder, now as an untracked file. Add it to .gitignore so it doesn’t come back.

If the file contains a secret like an API key and you already pushed, amending is not enough. Anyone who fetched the branch has it. Rotate the key.

Undo an amend

Amended by mistake? Git keeps the old commit around, and git reflog shows where HEAD was before:

git reflog
a8aa004 HEAD@{0}: commit (amend): Add app
4c722d7 HEAD@{1}: commit (initial): Add app

HEAD@{1} is the commit before the amend. Move the branch back to it, keeping your changes staged:

git reset --soft HEAD@{1}

Now the forgotten file is staged again and the original commit is back. Run this right after the amend: every Git command that moves HEAD adds a reflog entry, so HEAD@{1} changes.

The file belongs to an older commit

--amend only works on the last commit. If the file belongs to a commit further back, create a fixup commit that targets it, then let an interactive rebase merge it in:

git add file-forgotten.txt
git commit --fixup=<hash>
git rebase -i --autosquash <hash>~1

Replace <hash> with the hash of the commit you want to fix, which you find with git log --oneline.

--fixup creates a commit named fixup! Original message. With --autosquash, the rebase moves it right after the original commit and marks it to be squashed in. The editor opens with the plan ready, so save and close it.

The original message stays the same. The commits after it all get new hashes, so the same warning about pushed commits applies. For more ways to combine commits, see squashing Git commits.

You already pushed the commit

If you amend a commit you already pushed, the next git push fails:

 ! [rejected]        main -> main (non-fast-forward)
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart.

Your local branch now has a different commit than the remote, so Git refuses. Don’t follow the hint and run git pull, which would merge the old commit back in.

If you’re the only one on the branch, force push, with the safer option:

git push --force-with-lease

Plain --force overwrites the remote no matter what. --force-with-lease only overwrites it if the remote is still where you last saw it. If someone pushed in the meantime, it refuses, and you don’t lose their work.

On a shared branch like main, don’t amend at all. Make a new commit with the file:

git add file-forgotten.txt
git commit -m "Add forgotten file"

One extra commit in the history is better than breaking everyone else’s copy.

Try the incomplete-commit repair exercise to inspect the staged diff and compare the commit IDs before and after.

Want me to talk about your product? You can sponsor this site.

Related posts about git: