11g_aq
Last Updated: May 03, 2017
·
797
· lxe
Me

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.

Say Thanks
Respond

2 Responses
Add your response

14385
D42a7264714dee5006b9c99d2567a320

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

over 1 year ago ·
14394
Me

@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 ·
Filed Under