git-rebase

Common use patterns

Warning

git-rebase is a branch history rewriting command. It is generally a good idea to make a backup branch before rebase, especially when you are new to rebasing.

Re-ordering refactorings and squashing fixup commits

You have ended up with git-log like:

* commit C
|
|     Fix commit A
|
* commit B
|
|     Add feature X to test-runner or whatever
|
* commit A
|
|     Add test

but you want to convert it to:

* commit A'
|
|     Add test
|
* commit B
|
|     Add feature X to test-runner or whatever

so

  1. commit B, which is unrelated to the current branch, is at the bottom so it gets out of your way on future git commit --amend and you can test it a bit by dog-fooding before eventually git cherry-pick -ing it to master.

  2. commit C and A are single, correct, coherent commit A' instead of a commit A adding a draft of the test and commit C finallazing the draft.

Assuming your HEAD is commit C, run:

git rebase -i HEAD~3

which opens your ${EDITOR} with prompt like:

pick commit A
pick commit B
pick commit C

which reflects the current commit order.

To achieve goal 1, you re-order the commits:

pick commit B
pick commit A
pick commit C

To achieve goal 2, you then squash C into A:

pick commit B
pick commit A
squash commit C

Hint

TODO cover git-rebase --autosquash + git-commit --fixup/--squash