Sometimes you come up with a better description for a previous commit, where simply git commit --amend cannot do the job.
Note: if you have push the commit to a remote repository based on which, others have done more commits, the following steps cannot do the magic.
Learned from http://blog.jacius.info/2008/6/22/git-tip-fix-a-mistake-in-a-previous-commit/
[Update 2008-09-06: Since posting this article, I've stumbled upon an easier way than squashing commits — editing commits!]
- Save and stash your work so far with git stash
- Look at
git logand copy the first 5 or so characters from the ID of the commit you want to edit onto your clipboard. - Start the interactive rebase process, pasting in the characters from the ID:
git rebase --interactive ID^,^to refer to the parent of that commit. - Your editor will come up with several lines like
pick d3adb33 Commit message, one line for each commit since the older one. - Change the word “pick” to “edit” in front of the commit you want to change.
- Save and quit.
- Edit your project files to make the correction, then run
git commit --all --amend. - After you’ve committed the fixed version, do
git rebase --continue. - Git will do its magic, recreating all the commits since then.
- You might need to resolve some conflicts, if the change you made affected later commits. Follow Git’s instructions to resolve those.
- Once the rebase is done, re-apply the stash with git stash apply.
Because of how Git commit IDs works, the commits will have new IDs now, which is why you shouldn’t do this if someone already has the commits with the original IDs. In that case, you should just suck it up and make a new commit to fix it, like you would with Subversion.
No comments:
Post a Comment