How to change past commit to include a missed file?

kolrie picture kolrie · Jan 16, 2013 · Viewed 37.3k times · Source

I have committed a change and forgot to add a file to the change set. After other commits, I realized the file is now missing from a HEAD^4 commit.

How do I rewrite a previous commit to include the missing file?

Answer

DrBeco picture DrBeco · Jun 16, 2016

I realize people can google and come here to find a simpler answer: What if it was just the last commit? (OP's question is for fixing the 4th commit back in history)

In the case you commit and realize you forgot to add some file immediately, just do:

# edited file-that-i-remember.txt
git add file-that-i-remember.txt
git commit

# realize you forgot a file
git add file-that-i-forgot.txt
git commit --amend --no-edit

Where --no-edit will keep the same commit message.

Easy peasy!