Skip to content
BENIBIX Learn

Git Rebase and Cherry-Pick

Level 2 · Branches and merges

How rebase moves your commits onto a newer main, and why it gives them new hashes. How cherry-pick copies one fix, like a hotfix, to another branch.

Course contentsLesson 6 of 13

After this lesson you can

  • Explain what a rebase changes
  • Say when not to rebase
  • Copy one commit with cherry-pick

13 mincovers 3 glossary entries

Worth reading first: Git Merge and Conflicts

How rebase and cherry-pick move commits

A rebase moves your branch so it starts from a newer point, usually the tip of main. Git lifts off each of your commits and replays them, one at a time, on top. The result is one straight line with no merge commit. A branch of 3 commits comes out as 3 new commits with 3 new hashes.

Merge and rebase both bring main's new work into your branch. A merge keeps both lines and adds a joining commit. A rebase rewrites your commits as if you had started them today. The old commits drop out of the branch, and the new ones carry the same changes.

The straight line is the reason people rebase. A log with no joining commits is easier to read and to search. Many teams rebase a branch on main just before they join it, so main reads as one clean line of work.

The rule that matters is this: never rebase commits other people already have. Their copies still hold the old commits, and yours now hold new ones with the same changes. When they pull, Git sees two sets of commits, and work gets doubled or lost. Rebase only your own branch, before you share it.

A rebase can hit conflicts, one commit at a time. Git stops at the commit that clashes. You fix the file, add it, and run git rebase --continue. If it goes wrong, git rebase --abort puts the branch back as it was before you started.

A cherry-pick copies one chosen commit from another branch onto yours. You name the commit by its hash, and Git applies just that change. The rest of the other branch is left alone. Like a rebase, the copy gets a new hash, because it now sits on a different parent.

The usual reason is a hotfix: an urgent fix for the live code. The fix may have been made on a branch that is not ready to ship. Cherry-picking that one commit onto main gets the fix out today, while the rest of the branch keeps waiting.

Cherry-picking lots of commits, instead of merging, is the mistake to avoid. The same change then sits twice in the history under two hashes. Later merges get confusing, because Git sees two copies of one change. Use cherry-pick for one or two commits, and merge for a whole branch.

console
$ git switch search
Switched to branch 'search'
$ git rebase main
Successfully rebased and updated refs/heads/search.
$ git log --oneline -3
e22ab1e Add search
cb0fe9d Add footer
b2326a8 Blue title
Add search now sits on top of main's newest commit, Add footer, with a new hash.
Merge adds a joining commit. Rebase makes copies of your commits on top, so the line is straight but the hashes change.

Worked example

Emma ships one fix early

Emma's branch has 9 commits for a new checkout page that is not ready. One of them, 3f9a1c2, fixes a wrong price in the live price list.

She switches to main and runs git cherry-pick 3f9a1c2. The fix goes live that day as a new commit, and the other 8 commits wait on her branch.

The words for this lesson3

Check yourself

Why should I not rebase a branch other people have pulled?

Finished this lesson?

Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.