Rework Your Git History
Published on January 16, 2009 by Jesse Storimer
Git is an awesome tool. Recently I have been discovering more and more cool things I can do with git. Here is one such thing.
I was recently working in a development branch and had made more commits than were necessary. I had been committing every time I got a small piece of new functionality to work and I had about 12 commits that should have been only 3 commits. Enter
git rebase --interactive:
git rebase -i
This is awesome. It gives you an interactive way to reorganize your commit history since the one specified in <commit>. It opens up your
$EDITOR with the list of commits, something like this:
pick c9205f4 Ok, so spawn doesn’t work exactly right yet.
pick c770395 Added Errno::ENOTDIR to the list of errors when rmdiring
pick 49221b6 There’s no reason to load anything but test for the database.yml
pick bc1af0c Removed rake/gempackagetask and the rubyforge rake task
pick 8a43c13 Added requirement for thoughtbot-shoulda >=2.0.0 in test_helper
pick 06f9b99 #47: Multipage PDFs fixed by Pete Deffendol
pick 279b757 Updated the rake task to take STI into consideration.
# Rebase 701abb0..279b757 onto 701abb0
# pick = use commit
# edit = use commit, but stop for amending
# squash = use commit, but meld into previous commit
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
You can rewrite
pick to change the behaviour.
If you want to combine a commit with the one above, just change it from
If you want to leave a commit as it is, just leave it at
If you want to change a commit, change it to
Edit is awesome, you can change which files are included in the commit and even update old commit messages.
You can even change the order of commits by just rearranging the lines in the file. But be careful, if you delete a line, then that commit will be deleted.
When you exit the text editor, it will start rebasing your commits. If there is a merge conflict, or when a commit has been marked as edit, then the rebase stops. You make your changes and amend your commit with
git commit --amend. Then just
git rebase --continue to continue on with the rebase!