Skip to main content

Rebase

What is git rebase (in plain English)?

Rebase takes the commits on your current branch and replays them on top of another commit (often the latest main).
This creates a clean, linear history (no merge commits) as if you had started your work from that newer point.

  • Merge: combines histories as they happened, preserving branches and creating a merge commit.

  • Rebase: rewrites history to look linear by moving your branch’s commits on top of a new base.

When to use:

  • Before opening a PR, to clean up/squash commits and sync with main.

  • To remove accidental commits, fixup messages, or reorder small changes.

Safety note: Rebase rewrites commit IDs. It’s safe for local branches or branches only you own. If you’ve already pushed, use --force-with-lease when pushing, and coordinate with collaborators.


Make Git use Nano (not Vim)

One‑time for this PowerShell session


# If a rebase is in progress and you're unsure:
git rebase --abort
# Use Nano with proper quoting (space in "Program Files"!)
$env:GIT_SEQUENCE_EDITOR = '"C:\Program Files\Git\usr\bin\nano.exe" -w'
$env:GIT_EDITOR = $env:GIT_SEQUENCE_EDITOR
# Start interactive rebase (edit last 3 commits)
git rebase -i HEAD~3

Make Nano the default (all repos)


git config --global core.editor '"C:\Program Files\Git\usr\bin\nano.exe" -w' git config --global sequence.editor '"C:\Program Files\Git\usr\bin\nano.exe" -w'

Alternatives if needed:

  • Short path (no spaces): C:\Progra~1\Git\usr\bin\nano.exe -w

  • If nano is in PATH: set just nano -w


Interactive Rebase (Nano) — Quick How‑To

  1. Start:  git rebase -i HEAD~N

    Replace N with how many recent commits you want to edit (e.g., 3).

  2. In Nano, you’ll see lines like:


    pick abc123 First change pick def456 Duplicate message to squash pick ghi789 Tweak backoff limit
  3. Change actions at the start of each line (use arrow keys to move cursor):

    • pick — keep as‑is

    • reword — change commit message

    • edit — pause to amend content

    • squash — combine with previous, keep messages

    • fixup — combine with previous, discard this message

    • drop — remove this commit

  4. Save & exit Nano: Ctrl+O, Enter, then Ctrl+X.

  5. If you used squash/fixup, Git opens another Nano window to edit the final message. Save & exit again.

If conflicts appear:

# fix files, then: git add <files> git rebase --continue # to skip the current commit: git rebase --skip # to back out entirely: git rebase --abort

Common Workflows

A) Clean up duplicate commits (your case)

git rebase -i HEAD~3 # In Nano: change the second duplicate from 'pick' → 'squash' (or 'fixup') # Save/exit; then edit the combined message if prompted.

B) Update your feature branch to the latest main

git fetch origin git rebase origin/main # resolve conflicts → git add ... → git rebase --continue

C) Edit a commit’s content from a few commits ago

git rebase -i HEAD~3 # mark the target commit as 'edit' # when Git stops: # make changes git add <files> git commit --amend git rebase --continue

D) Change a commit message only

git rebase -i HEAD~3 # mark the commit as 'reword' # Git opens Nano to edit the message

E) Drop an accidental commit

git rebase -i HEAD~3 # change that line from 'pick' → 'drop'

F) Autosquash “fixup!” commits

git commit --fixup=<target-hash> git rebase -i --autosquash HEAD~5

Pushing after a rebase (history changed)

git push --force-with-lease

Safer than --force; refuses to overwrite unexpected remote changes.


Quick Troubleshooting

  • “In the middle of a rebase”


    git rebase --continue # or git rebase --abort
  • Editor error with spaces in path
    Use quotes when setting the editor:


    git config --global core.editor '"C:\Program Files\Git\usr\bin\nano.exe" -w' git config --global sequence.editor '"C:\Program Files\Git\usr\bin\nano.exe" -w'
  • Confirm Nano works


    & "C:\Program Files\Git\usr\bin\nano.exe" --version