How to add a changed file to an older (not last) commit in Git
with git 1.7, there's a really easy way using git rebase
:
stage your files:
git add $files
create a new commit and re-use commit message of your "broken" commit
git commit -c master~4
prepend fixup!
in the subject line (or squash!
if you want to edit commit (message)):
fixup! Factored out some common XPath Operations
use git rebase -i --autosquash
to fixup your commit
To "fix" an old commit with a small change, without changing the commit message of the old commit, where OLDCOMMIT
is something like 091b73a
:
git add <my fixed files>
git commit --fixup=OLDCOMMIT
git rebase --interactive --autosquash OLDCOMMIT^
You can also use git commit --squash=OLDCOMMIT
to edit the old commit message during rebase.
See documentation for git commit and git rebase. As always, when rewriting git history, you should only fixup or squash commits you have not yet published to anyone else (including random internet users and build servers).
Detailed explanation
git commit --fixup=OLDCOMMIT
copies theOLDCOMMIT
commit message and automatically prefixesfixup!
so it can be put in the correct order during interactive rebase. (--squash=OLDCOMMIT
does the same but prefixessquash!
.)git rebase --interactive
will bring up a text editor (which can be configured) to confirm (or edit) the rebase instruction sequence. There is info for rebase instruction changes in the file; just save and quit the editor (:wq
invim
) to continue with the rebase.--autosquash
will automatically put any--fixup=OLDCOMMIT
commits in the correct order. Note that--autosquash
is only valid when the--interactive
option is used.- The
^
inOLDCOMMIT^
means it's a reference to the commit just beforeOLDCOMMIT
. (OLDCOMMIT^
is the first parent ofOLDCOMMIT
.)
Optional automation
The above steps are good for verification and/or modifying the rebase instruction sequence, but it's also possible to skip/automate the interactive rebase text editor by:
- Setting
GIT_SEQUENCE_EDITOR
to a script. - Creating a git alias to automatically autosquash all queued fixups.
- Creating a git alias to automatically fixup a single commit.
Use git rebase
. Specifically:
- Use
git stash
to store the changes you want to add. - Use
git rebase -i HEAD~10
(or however many commits back you want to see). - Mark the commit in question (
a0865...
) for edit by changing the wordpick
at the start of the line intoedit
. Don't delete the other lines as that would delete the commits.[^vimnote] - Save the rebase file, and git will drop back to the shell and wait for you to fix that commit.
- Pop the stash by using
git stash pop
- Add your file with
git add <file>
. - Amend the commit with
git commit --amend --no-edit
. - Do a
git rebase --continue
which will rewrite the rest of your commits against the new one. - Repeat from step 2 onwards if you have marked more than one commit for edit.
[^vimnote]: If you are using vim
then you will have to hit the Insert key to edit, then Esc and type in :wq
to save the file, quit the editor, and apply the changes. Alternatively, you can configure a user-friendly git commit editor with git config --global core.editor "nano"
.