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?
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?
Use git rebase --interactive HEAD~4
and set edit
option for the commit you'd like to amend.
Remember that you should not modify commits pushed to the remote repository this way. It's better to add a new commit with missing file in that case.
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!
If you have NOT pushed these 4 commits, you can do it as follows:
Create patch files for all these commits:
git format-patch -4
Rewind back by 4 commits:
git reset --hard HEAD~4
Add missing file:
git add missing-file
Commit it with --amend
:
git commit --amend
Apply all saved patches back:
git am *.patch
If you have pushed, you should NOT use this method. Instead, just admit your blunder and create one more commit on top of HEAD which fixes this issue.
Although the accepted answer is correct, it lacks detailed instructions on how to perform editing a commit during a rebase process.
First, start a rebase process:
git rebase --interactive HEAD~4
A list of commits will be presented, choose a commit you want to edit by changing the word pick
to edit
and save the file.
Make necessary modifications in your code (remember to invoke git add
for new files)
After all modification are done, issue git commit --amend
- this will amend a commit marked as edit
Invoke git rebase --continue
that will finish the process (if there are more commits marked as edit
, the above steps need to be repeated)
Important notes:
DO NOT remove lines marked as pick
that you don't want to edit - leave them as is. Deleting these lines will result in deleting related commits
GIT forces you to stash
before rebasing if your working directory is not clean; you can however git stash pop / git stash apply
during rebase, in order to amend these changes (i.e. changes stashed before starting the rebase process) to a commit marked as edit
if something went wrong and you want to revert changes made during the rebase process before it finished (i.e. you want to revert to the point before starting the rebase), use git rebase --abort
- also read: How to abort an interactive rebase if --abort doesn't work?
As said in the accepted answer:
Remember that you should not modify commits pushed to the remote repository this way. It's better to add a new commit with missing file in that case.
The answer why, is in the Git Book (paragraph entitled "The Perils of Rebasing"):
Do not rebase commits that exist outside your repository.
If you follow that guideline, you’ll be fine. If you don’t, people will hate you, and you’ll be scorned by friends and family.
When you rebase stuff, you’re abandoning existing commits and creating new ones that are similar but different. If you push commits somewhere and others pull them down and base work on them, and then you rewrite those commits with git rebase and push them up again, your collaborators will have to re-merge their work and things will get messy when you try to pull their work back into yours.
[...]