S

super-dev — full-stack

@super-dev.app · Full-stack · ★ open to work
12.8k subscribers·47 videos·Active since 2017

Full-stack .NET / Angular dev with a DevOps streak on Azure Cloud. Also fluent in Flutter + Firebase. I build products, ship them, and keep them healthy.

Back to articles
TUTO
TUTO

Tutorial — mastering interactive rebase

A clean Git history isn't vanity: it's what makes a review readable and a git bisect effective. Interactive rebase ( git rebase -i ) is the tool to rewrite a branch before pushing it — squash, rename, reorder, drop commits. Here's how to wield it without getting burned.

Opening the todo

git rebase -i takes a base : every commit that comes after it becomes editable. You usually target the last N commits of the current branch.

Bash
1# Rewrite the last 4 commits
2git rebase -i HEAD~4
3
4# Or: everything between my branch and main
5git rebase -i main

Git then opens a list, oldest at the top, newest at the bottom. Each line starts with a command you replace:

Bash
1pick a1b2c3d Add cart service
2pick d4e5f6a Fix a typo
3pick 7g8h9i0 Implement total
4pick 1j2k3l4 wip

The everyday commands

  • reword ( r ): keep the commit but rewrite its message.
  • squash ( s ): merge into the previous commit while keeping both messages.
  • fixup ( f ): like squash, but discard the merged commit's message — perfect for a

"wip" or a typo fix.

  • edit ( e ): stop on the commit to amend the code or split it.
  • drop ( d ): remove the commit entirely.

Reordering is just a matter of moving the lines . Here's the previous todo cleaned up:

Bash
1pick a1b2c3d Add cart service
2fixup d4e5f6a Fix a typo
3pick 7g8h9i0 Implement total
4fixup 1j2k3l4 wip

On save, Git replays the commits in the new order. If two changes touch the same line, a conflict appears: resolve it, then git add and git rebase --continue . At any point, git rebase --abort brings the branch back to its pre-rebase state.

Automatic fixup

To prepare a fix aimed at a specific commit, --fixup then --autosquash order everything for you:

Bash
1git commit --fixup=7g8h9i0
2git rebase -i --autosquash main

The golden rule

Never rebase shared history. Rebase rewrites commits: their SHAs change. If the branch is already on the remote and colleagues have pulled it, your git push --force will diverge from their copy and cause nasty conflicts. So you rebase only a local branch not yet pushed — or a branch you own alone, with a git push --force-with-lease that refuses to crush unexpected work.

Recovering after a mistake

A rebase gone wrong is never fatal: git reflog keeps a trace of every position of HEAD , even the ones "lost" by the rewrite.

Bash
1git reflog
2# ... 89abcde HEAD@{5}: rebase (start): ...
3git reset --hard HEAD@{5}

You get the branch back exactly as it was before the rebase. The reference is the git-rebase manual .

Interactive rebase rewrites history to make it tellable : one commit = one idea, one clear message. Keep it local, secure your pushes with --force-with-lease , and remember the reflog is your safety net.
super-dev — portfolio.app
// More in TUTO
Tutorial — testing zoneless Angular components with Vitest
6 min • 1.4k reads