Skip to content

Instantly share code, notes, and snippets.

@urbanautomaton
Last active December 21, 2015 07:29
Show Gist options
  • Save urbanautomaton/6271638 to your computer and use it in GitHub Desktop.
Save urbanautomaton/6271638 to your computer and use it in GitHub Desktop.
Git: interactive rebase autosquash helpers

Update: as I suspected, this is a stupid exercise in wheel-reinventing, and I could've done git commit --fixup=<commitish> all along. Thanks, Mudge.

For interactive rebase and autosquash background, see here.

All this helper does is construct commit messages that will be picked up by autosquash, using a commitish reference so you don't have to do the message yourself. If we know we have some changes that should've gone in the commit before last, and we don't want to add to its commit message, our workflow is now:

  1. make changes
  2. stage changes
  3. $ fixup HEAD^
  4. $ git rebase -i --autosquash when ready

We can now happily commit fixup changes and carry on working, then do one honking great git rebase -i before pushing, without having to remember what fix goes with what original commit.

I fully expect to have duplicated an already-existing piece of git functionality here, and await brickbats.

function ri_commit() {
local cmd
local commitish
local subject
cmd=$1
commitish=$2
if [[ ! -n "$cmd" ]]; then
echo "usage: ri_commit <rebase_command> [commitish]"
return 1
fi
[[ -n "$commitish" ]] || commitish="HEAD"
subject=$(git show --quiet --pretty=format:"%s" $commitish)
git commit -m "$cmd! $subject"
}
alias squash='ri_commit squash'
alias fixup='ri_commit fixup'
alias ffs='ri_commit fixup HEAD && git rebase -i HEAD~2'
$ echo "blah" > blah
$ git add . && git commit -m "Blah blah blah"
[master 5f72e83] Blah blah blah
$ echo "waffle" > waffle
$ git add . && git commit -m "Waffle waffle waffle"
[master aa27d96] Waffle waffle waffle
$ echo "wafflefix" >> waffle
$ git add . && squash
[master 676e70b] squash! Waffle waffle waffle
$ echo "blahfix" >> blah
$ git add . && fixup HEAD~2
[master f35b3ca] fixup! Blah blah blah
$ git log -n 4 --pretty=oneline --abbrev-commit
f35b3ca fixup! Blah blah blah
676e70b squash! Waffle waffle waffle
aa27d96 Waffle waffle waffle
5f72e83 Blah blah blah
$ git rebase --interactive --autosquash HEAD~4
[in $EDITOR]
pick 5f72e83 Blah blah blah
fixup f35b3ca fixup! Blah blah blah
pick aa27d96 Waffle waffle waffle
squash 676e70b squash! Waffle waffle waffle
# Rebase 1e9ffa9..f35b3ca onto 1e9ffa9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment