Reputation: 4788
I did a git rebase master
which involved some manual merging in three files. In one of them, I missed a trailing newline, resulting in the following at the end of the file:
<<<<<<< HEAD
=======
>>>>>>> ... commit message
which naturally results in a PHP error. I can fix the file easily enough, but I don't want to add an erroneous "fix sloppy rebase" commit.
How can I amend this change to the rebase? Or, repeat the rebase so I can catch it?
Upvotes: 2
Views: 9841
Reputation: 185663
This question basically boils down to "how do I modify a commit that's not the most recent?". Assuming that you haven't pushed the commit anywhere, and further assuming that history is linear from that commit onwards, it's pretty easy.
$SHA
but you can use any means you want to identify this commit.git commit --fixup $SHA
.git rebase -i --autosquash $SHA^
This will give you an editor with a list of commits. The first commit should be $SHA
, the second should be your edit, and following that should be the rest of the commits up to HEAD
. Save and close this editor. Git will now do an interactive rebase, applying your change to the commit you wanted to modify.
There's an alternative solution you can use, which is a bit more manual, but works without the interactive rebase. Basically, you can checkout the commit you want to modify directly (producing a detached head), make your change, run git commit --amend
, and then rebase the branch back on top of your new amended commit. You may actually prefer this style since it's a bit more explicit about what you're doing, but the benefit of the interactive rebase version is you can do a lot more things with an interactive rebase. For example, you can reorder commits, squash multiple commits together, delete commits, reword commits, and more. You can also use the --fixup
and --squash
flags to git commit
to record these actions that you wish to perform without actually doing the rebase immediately, and then you can go back and do one big interactive rebase when you're done.
Upvotes: 7