Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a rebase after a cherry-pick not apply the same commit twice?

Tags:

git

When you cherry pick a commit from one branch (say "topic") to another (lets call it "master") the history of that commit is rewritten, its hash changes and it effectively becomes a new, independent, commit.

However when you subsequently rebase topic against master git is clever enough to know not to apply to the commit twice.

Example:

A --- B <- master       \        \---- C ---- D <- topic  $ git checkout master $ git cherrypick D  A --- B --- D' <- master       \        \---- C ---- D <- topic  $ git checkout topic $ git rebase master First, rewinding head to replay your work on top of it... Applying 'C'  A --- B --- D' <- master             \              \---- C' <- topic 

How does this magic work? Ie. how does git know it should apply C to D', but not D to D'?

like image 416
jleahy Avatar asked Jan 24 '13 20:01

jleahy


People also ask

Does rebase use cherry-pick?

In fact, cherry picks can even be used to replace rebases.

Does cherry-pick duplicate commits?

Failure to this, cherry-picking can cause duplicate commits and users are encouraged to use git merge in scenarios where it is risky. PS: The branch you cherry-pick from should be deleted, cherry-pick the commits into two or more different branches then delete the faulty branch to avoid code duplication.

Do I need to commit again after rebase?

The purpose of rebase is make your commits look as if they were changes to the branch you rebase onto. So the most logical way is to incorporate merge conflicts into these commits. No additional commits is required thus.

What happens if you rebase twice?

Rebasing a branch on another "re-applies" (fairly smartly, these days) the commits of the currently checked out branch on top of the tip of the target. So, yes, if you do it over and over again then you will keep your current branch up to date.


1 Answers

The answer is in the man page for git-rebase:

Note that any commits in HEAD which introduce the same textual changes as a commit in HEAD.. are omitted (i.e., a patch already accepted upstream with a different commit message or timestamp will be skipped).

Rebase looks at the textual change, and refuses to replay that commit if it already exists on the branch you're rebasing onto.

like image 148
John Szakmeister Avatar answered Sep 19 '22 14:09

John Szakmeister