Learn you an interactive rebase and other fancy git stuff - part 2

2019-05-02

In yesterday's installment we added the git spike alias to quickly checkout a copy of our branch. We also want a quick way to delete these spike branches, as we'll be making a lot of mistakes, lets add an alias to do that.

# ~/.gitconfig
[alias]
  cb = rev-parse --abbrev-ref HEAD
  spike = "!git checkout -b $(git cb)-spike"
  dspike = "!git branch -d $(git cb)-spike"
  Dspike = "!git branch -D $(git cb)-spike"

Now we can remove our spikes by checking out our previous branch git checkout - and running git dspike or git Dspike if we've committed changes and want to force deleting the branch.

The last alias I want to cover is git tree-state. It returns a hash of the files committed to git. If any file changes you get a different hash. This is a good sanity check to make sure you end up in the same state when messing around with git history.

# ~/.gitconfig
[alias]
  cb = rev-parse --abbrev-ref HEAD
  spike = "!git checkout -b $(git cb)-spike"
  dspike = "!git branch -d $(git cb)-spike"
  Dspike = "!git branch -D $(git cb)-spike"
  tree-state = "!git log --pretty --format='%t' | head -n1"

Given a branch with a bunch of commits you want to clean up, my learning git rebasing workflow is.

  1. git tree-state - get the current state of files known to git.
  2. git spike - checkout a copy of the branch to experiment on.
  3. Do the rebase or whatever gnarly thing you want to try.
  4. git tree-state make sure we end up with our files in the same state as when we started.
  5. git checkout - to get back to our 'real' branch.
  6. Redo all the git wizardry, now we know what we need to do, on the main branch.
  7. git Dspike to remove our spike branch.

Step 6 is a bit redundant, but I recommend repeating the rebasing as practice. If you don't want to redo the whole rebase you can use git branch --move to rename your spike branch and make that the new authoritative copy.

That's today's 200 words covered.