Git Rebase & Cherry-pick

git rebase main (run from your branch, for example feature-promo) moves that branch's commits onto the tip of main (its latest commit), so the history is…

You've been working on the feature-promo branch for two days. Meanwhile main has moved forward: there's a new footer your branch doesn't have. You want the latest version, but you don't want a history full of merge commits. There's a way.

git rebase moves your branch's commits onto the tip of another branch (the latest commit on that branch), as if you had only just started working from there. git cherry-pick copies one particular commit from another branch onto yours. Think of notes you wrote on a photocopy of yesterday's chapter, while the real chapter has since gained new pages. Rebase is like copying your notes again on top of the latest version of the chapter. Cherry-pick is like picking one note and copying it into another book. In Git, the real chapter is main, and your notes are the commits on your branch. Rebase and cherry-pick both make copies: new paper, not the same sheets.

A branch that is behind

We use the cake-shop repo. The feature-promo branch was made from b2d4f60 and already has 2 commits. Meanwhile main moved forward by one commit, c7e9a12 add footer. Read the picture below from left to right. Each hash is one commit, the line --- joins a commit to the commit after it, and / marks where feature-promo splits from main:

                    e7a1b23---f8b2c34   feature-promo
                   /
a1c3e5f---b2d4f60---c7e9a12   main

feature-promo doesn't have footer.html yet. There are two ways to bring main's new commit into it: git merge main (the Merging lesson), or git rebase main.

git rebase main

Before you run it, guess: after the rebase, do the hashes of the two commits on feature-promo stay the same or change?

$ git switch feature-promo
Switched to branch 'feature-promo'
$ git rebase main
Successfully rebased and updated refs/heads/feature-promo.

This is the shape of the history after the rebase:

a1c3e5f---b2d4f60---c7e9a12   main
                           \
                            4c8d2f1---9b3e6a7   feature-promo

What rebase does, step by step:

  1. Find the commits that are only on feature-promo: e7a1b23 and f8b2c34.
  2. Go to the tip of main, which is c7e9a12.
  3. Apply each of those commits' changes again, one by one, as new commits.
  4. Move feature-promo to the last new commit.

git checkout feature-promo works the same as git switch feature-promo. What matters is that you run rebase from the branch you want to move, with the name of the branch it goes onto.

Rebase makes new commits

This table compares the two feature-promo commits before and after the rebase. The parent is the commit before:

Commit Before the rebase After the rebase
add promo banner e7a1b23, parent b2d4f60 4c8d2f1, parent c7e9a12
add discount f8b2c34, parent e7a1b23 9b3e6a7, parent 4c8d2f1

The hashes change. The messages and changes are the same, but a hash is calculated from the commit's content and its parent, so a new parent means a new commit. The old commits are not deleted right away. No branch points at them any more, but the reflog can still find them. The hashes the Lab's rebase makes will be different from these.

Now feature-promo simply continues main, so merging it into main is a fast-forward, with no merge commit:

$ git switch main
$ git merge feature-promo
Updating c7e9a12..9b3e6a7
Fast-forward

Merge or rebase?

git merge main git rebase main
Shape of the history branching, plus one merge commit straight (linear)
The old commits stay, same hashes replaced by copies with new hashes
Safe for a branch that is pushed and used by others yes no

Rebase for a clean history, merge to keep the history as it happened. The most important rule: only rebase private branches, ones that aren't pushed or that only you use. Why? Say you already pushed feature-promo, and Rina, a teammate, already has e7a1b23 and f8b2c34 on her laptop. After you rebase, GitHub rejects your push, because your history no longer continues the history on GitHub. If you force it with --force, Rina's history and GitHub's no longer match. When she pulls, the same commits appear twice, or she gets conflicts.

Another case that happens often: you already pushed feature-promo, but only you use that branch. After git rebase main, a push without --force is still refused with ! [rejected] feature-promo -> feature-promo (non-fast-forward). Send it with git push --force-with-lease. It overwrites the branch on GitHub only if that branch still matches origin/feature-promo, your laptop's note of where it is on GitHub. Use this only on a branch that is yours, never on main.

If a rebase stops on a conflict, fix the file, run git add, then git rebase --continue. Want to cancel and go back to how things were before the rebase? Run git rebase --abort.

git cherry-pick: take just one commit

In another repo, feature-promo has 3 commits. The middle one, d5c4b3a fix price typo, fixes menu.html ("Layer cak" becomes "Layer cake"). That fix has to go into main now, but the promo feature isn't ready. So you switch to main and copy only that commit:

$ git switch main
$ git cherry-pick d5c4b3a
[main 6a1f0e3] fix price typo
 Date: Tue Oct 6 09:12:00 2026 +0700
 1 file changed, 1 insertion(+), 1 deletion(-)

📌 Tidying commits before a PR: git rebase -i

A PR (pull request) is a request to join your branch into the main branch on GitHub. git rebase -i HEAD~3 opens a list of the last 3 commits in a text editor. Change the word pick at the start of a line: reword to change the message, squash to join it with the commit above, fixup like squash but the message is dropped, and drop to throw the commit away. Save, and Git rewrites those commits. Same rule: only for commits that aren't pushed.

⚠️ Rebases that cause trouble, and the way out

TL;DR: git rebase main (run from your branch, for example feature-promo) moves that branch's commits onto the tip of main (its latest commit), so the history is straight. But every one of those commits is replaced by a copy with a new hash. That's why rebase is only for private branches that aren't pushed, and merge is for branches that are shared. Rebase for a clean history, merge to keep the history. git cherry-pick <hash> copies one commit from another branch onto yours as a new commit.

⚡ Move commits onto the tip of main

Write:

git switch feature-promo
git rebase main

Meaning: git rebase main applies the commits of feature-promo again on top of the tip of main (the latest commit on main), as if you'd just started work from there. The history becomes straight, with no merge commit. Run it from the branch you want to move, here feature-promo.

⚡ The hashes change

Guess first: feature-promo has two commits. After the rebase, do those two commits keep their hashes?

They change. The messages and changes are the same, but a hash comes from the commit's content and its parent (the commit before it). A new parent means a new commit.

Meaning: Rebase makes copies, it doesn't move the same commits.

⚡ Merge or rebase?

Meaning: Rebase for a clean history, merge to keep the history as it was. The most important rule: rebase only on a private branch that isn't pushed yet, because rebase replaces commits with copies that have new hashes, while other people still have the old commits. For a branch others already use, just merge.

⚡ Take just one commit

Write:

git switch main
git cherry-pick d5c4b3a

Meaning: git cherry-pick copies the changes of one commit from another branch onto yours, here the commit d5c4b3a onto main. The result is a new commit with the same message. The other commits on the branch it came from are not included.

What you will learn