How to rebase intermediate git commits with shell script

My Personal Workflow

git commit -am "WIP"

This generate a lot of intermediate commits, which I don’t want to include in my final commit.

The problem is that I don’t want to count or remember how many of such commits manually.

Solution Background

Your branch is ahead of `some-repo/some-branch` by N commit.`

Solution Detail

#!/bin/bash
## file: ~/bin/gri
git rebase -i HEAD~`git status | grep -i by | sed 's/by/:/' | cut -d : -f 2 | awk '{print $1}'`

Sample sessions

Let say that we have made 8 commits using git commit -am "WIP" above.

When running the gri script above we should get the following output:

pick 659e13b WIP
pick 40b6da3 WIP
pick 1c2c0d1 WIP
pick eb4b6d1 WIP
pick 6950462 WIP
pick b753aef WIP
pick 12a441b WIP
pick 8dfe19a WIP

# Rebase 4dab8be..8dfe19a onto 4dab8be
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Notes

Useful Links

comments powered by Disqus