>_ IzzyLab

Hands-on task

This task is step 12 of the course “Git: rewriting and rescuing history”

Reorder commits with rebase

hard~10 min

In /root/repo there are three commits in this order (oldest to newest): base, then hotfix, then feature. Logically the hotfix should have landed on top of the feature, not beneath it. Reorder the commits so the order becomes base, feature, hotfix — that is, hotfix becomes the newest commit and feature the one before it. Each commit touches its own file, so there won't be any conflicts; the number of commits stays three.

What you'll practise:
  • git rebase
  • interactive rebase
  • commit order
  • GIT_SEQUENCE_EDITOR
  • git history

Topic breakdown

Git history is a sequence of commits, and it can be replayed anew with the git rebase tool. In interactive mode (-i) git opens the list of commits from oldest to newest as instruction lines: the order of lines sets the order of application, so by swapping the lines you change the sequence of commits. A replacement list editor can be supplied non-interactively via the GIT_SEQUENCE_EDITOR variable. When each commit touches its own file, the reordering goes through without conflicts.

What's checked

  1. hotfix became the last one
  2. feature is second to last
  3. all three files and commits in place

What it looks like