Last Updated: July 11, 2019
·
971
· lxe

Easy Git Rebase

Ever been asked to rebase your messy commits into a nice one? Are you doing a lot of git commit -am "removed whitespace"? You can easily squash those commits into a single message without entering git rebase. Here's how:

# doing some work....
git commit -am "feature - add widget confabulator"
# oops left in a `console.log`... gotta remove it and make a commit
git commit -am "removed console log"
# oops I've indented with tabs... gotta fix that
git commit -am "tabs -> spaces"

All we want now, is one nice commit saying "feature - add widget confabulator." You can enter git rebase -i and mark the extra commits for fixup, but the easier way is to soft-reset your tree to the first commit in your workflow, stage what you need, and just make the commit again:

git reset --soft HEAD~3 # 3 commits back
git commit -am "feature - add widget confabulator"

Now all my console.log removal and indentation fix is a part of one nice commit.

2 Responses
Add your response

Thanks for sharing! Are there any caveats with this approach compared to the interactive rebase?

over 1 year ago ·

@dpashkevich you're welcome! This does the same exact thing as going into the interactive rebase mode, and contiguously marking all commits for fixup up to the point of the reset. (in this case both "removed console log" and "tabs -> spaces"). It doesn't let you select individual commits and "break" the commit chain.

over 1 year ago ·