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'?
In fact, cherry picks can even be used to replace rebases.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With