Log inSign up

Rewrite and move commits with rebase and cherry-pick

Update a feature branch with git rebase main to replay its commits on top of the newest main, giving a straight history without merge commits. Use git rebase -i to squash or reword commits before sharing them, and git cherry-pick COMMIT to copy one specific commit onto another branch, such as a fix for a release branch.

Topic: Git on a server · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

When should you rebase instead of merge?

Rebase when you want your branch to look as if it started from today's main, and only for commits nobody else has built on. A merge preserves exactly what happened, including the parallel lines; a rebase rewrites your commits into new ones placed after the current tip, which reads more easily in git log.

The one firm rule: do not rebase commits that others already pulled, unless everyone agrees. Rebasing creates new commit IDs, so a teammate holding the old ones would see duplicates and conflicts. Your own unpublished branch, or a personal branch only you push to, is fair game. If merging is new to you, read the branches guide first.

How do you rebase a feature branch onto main?

Fetch the latest main, switch to your branch and run git rebase with the target.

git fetch origin
git switch feature-invoices
git rebase origin/main
git log --oneline --graph -10

Git sets your commits aside, moves the branch to the tip of origin/main, and applies your commits one by one. Once it finishes, a later git merge feature-invoices on main is a clean fast-forward. To make git pull rebase instead of merge for this repository only, run git config pull.rebase true; pair it with rebase.autoStash true so local edits are parked and restored automatically.

What do you do when a rebase stops on a conflict?

Fix the file, stage it and tell Git to continue; repeat for each commit that clashes.

git status                    # shows which commit is being applied
nano src/billing.py           # resolve the markers
git add src/billing.py
git rebase --continue

git rebase --skip             # drop this commit entirely
git rebase --abort            # give up, branch returns to its old state

During a rebase the labels flip compared with a merge: --ours refers to the branch you are rebasing onto, and --theirs to your own commit being replayed. With rerere.enabled turned on, Git remembers each resolution, so a conflict you solved once is not asked again in a later rebase.

How do you tidy commits with an interactive rebase?

Run git rebase -i with the commit just before the ones you want to edit; Git opens a list where each line starts with an action word.

git rebase -i HEAD~4
# in the editor:
pick 3e1a9c2 Add invoice model
squash 7b44d10 Fix typo in invoice model
reword c09f5e1 Add PDF export
drop 5d21aa8 Debug print, remove
  • pick: keep the commit as it is.
  • reword: keep the changes, edit the message.
  • squash: merge into the commit above and combine messages; fixup does the same but discards this message.
  • edit: stop after applying it, so you can amend or split it.
  • drop: remove it.

You can also reorder lines to reorder commits. Save and close the editor, and Git replays the list.

How does the fixup workflow save time?

Mark small corrections for a specific earlier commit as you make them, then let Git sort them in automatically.

git commit --fixup 3e1a9c2
git rebase -i --autosquash origin/main

The fixup commit gets a subject starting with fixup! and the original subject, and --autosquash moves it right under its target with the action already set. Setting git config --global rebase.autoSquash true makes this the default for every interactive rebase.

How do you copy one commit with cherry-pick?

git cherry-pick takes the change introduced by a commit and applies it as a new commit on the branch you are on. It is the usual way to bring a fix from main to a maintenance branch without merging everything else.

git log --oneline main -5          # find the fix, e.g. 9fa31b7
git switch release-1.4
git cherry-pick -x 9fa31b7
git push

The -x flag appends "cherry picked from commit ..." to the message, which makes the link between the two copies traceable later. A range works too: git cherry-pick A..B applies every commit after A up to and including B. Conflicts are handled like a rebase, ending with git cherry-pick --continue, --skip or --abort. Add -n to apply the changes without committing, so you can adjust them first.

How do you push a branch after rewriting it?

A rebased branch no longer matches the remote copy, so a plain push is rejected. Use --force-with-lease, never a bare --force.

git push --force-with-lease

The lease check refuses the push if the remote branch moved since your last fetch, for example because a colleague pushed a commit you have not seen; a plain force would silently delete their work. Many Git hosts can block force pushes to main entirely. On a self-hosted bare repository, set receive.denyNonFastForwards true in its config, as the deploy hooks guide does for the production repository.

How do you recover from a rebase that went wrong?

The reflog remembers where your branch pointed before the rebase, so you can jump straight back.

git reflog
# find the line just before "rebase (start)", e.g. HEAD@{5}
git reset --hard HEAD@{5}

# or, right after the rebase:
git reset --hard ORIG_HEAD

ORIG_HEAD is set by risky commands like rebase and merge to the position just before they ran. A hard reset discards uncommitted edits, so check git status first. The undo guide explains reset and the reflog in more depth.

What other rebase options are worth knowing?

Three options cover most of the remaining cases:

  • --onto moves part of a branch to a new base: git rebase --onto main old-base feature takes the commits after old-base and places them on main, which helps when a branch was started from the wrong place.
  • --update-refs (Git 2.38 and later) keeps stacked branches that sit on top of each other pointing at the rewritten commits.
  • --exec runs a command after every replayed commit, for example git rebase -i --exec "make test" origin/main, so you find which commit breaks the tests.

On Ubuntu 22.04, --update-refs needs a newer Git from the PPA mentioned in the install guide.

Questions about Rebase and cherry-pick

Is rebasing dangerous?

Only for shared history. Rebasing local or private commits is routine, and the reflog keeps the old versions for weeks, so mistakes can be undone.

Should I use squash-merge or interactive rebase?

Both end with fewer commits. A squash-merge collapses the whole branch into one commit at merge time; interactive rebase lets you keep several meaningful commits and only drop the noise.

Why did cherry-pick create an empty commit error?

The change is already on the target branch, perhaps through an earlier merge. Skip it with git cherry-pick --skip, or use --allow-empty if you deliberately want the record.

Can I cherry-pick a merge commit?

Yes, but you must say which parent counts as the base: git cherry-pick -m 1 MERGE_COMMIT applies the changes the merge brought into its first parent.

How do I check what a rebase will do first?

Compare with git log --oneline origin/main..HEAD for the commits that will be replayed, and git range-diff afterwards to compare the old and new versions of the branch.