How to inject a commit between some two arbitrary commits in the past?

It’s even easier than in OP’s answer.

  1. git rebase -i <any earlier commit>. This displays a list of commits in your configured text editor.
  2. Find the commit you want to insert after (let’s assume it’s a1b2c3d). In your editor, for that line, change pick to edit.
  3. Begin the rebase by closing your text editor (save your changes). This leaves you at a command prompt with the commit you chose earlier (a1b2c3d) as if it has just been committed.
  4. Make your changes and git commit (NOT amending, unlike most edits). This creates new a commit after the one you chose.
  5. git rebase --continue. This replays the successive commits, leaving your new commit inserted in the correct place.

Beware that this will rewrite history, and break anyone else who tries to pull.

Leave a Comment