樱桃选择工作后git如何合并? [英] How does git merge after cherry-pick work?
问题描述
主分支。
然后我们创建一个 newbranch
git checkout -b newbranch
并向 newbranch
提交两个新提交: commit1 和 commit2
然后我们切换到master并使 cherry-pick
git checkout master
git cherry-pick hash_of_commit1
纵观 gitk
,我们看到 commit1 及其樱桃挑选版本具有不同的哈希值,因此从技术上讲它们是两个不同的哈希值提交。
最后,我们将 newbranch
合并为 master
:
git merge newbranch
,并且看到这两个使用不同哈希的提交合并时没有问题,尽管它们意味着应该应用两次相同的更改,所以其中一个应该会失败。
在合并时,git是否真的在提交内容中做出了明智的分析,并决定不应该将更改应用两次,或者将这些提交内部标记为链接在一起?
<不要担心,Git会处理它。
$ b $
$ b 不要担心,Git会处理它。
长答案
不像例如SVN 1 ,Git不以delta格式存储提交,而是基于快照 2,3 。虽然SVN会天真地尝试将每个合并的提交作为补丁应用(并且出于您描述的确切原因而失败),但Git通常能够处理这种情况。
当合并,Git会尝试将两个HEAD提交的快照合并到一个新的快照中。如果代码或文件的一部分在两个快照中都相同(即因为提交已被选中),Git不会触及它。
来源
1 Subversion中的Skip-Deltas
2 //shafiulazam.com/gitbook/1_the_git_object_model.htmlrel =noreferrer> Git对象模型
Let's imagine that we have a master
branch.
Then we create a newbranch
git checkout -b newbranch
and make two new commits to newbranch
: commit1 and commit2
Then we switch to master and make cherry-pick
git checkout master
git cherry-pick hash_of_commit1
Looking into gitk
we see that commit1 and its cherry-picked version have different hashes, so technically they are two different commits.
Finally we merge newbranch
into master
:
git merge newbranch
and see that these two commits with different hashes were merged without problems although they imply that the same changes should be applied twice, so one of them should fail.
Does git really do a smart analysis of commit's content while merging and decide that changes shouldn't be applied twice or these commits are marked internally as linked together?
解决方案 Short answer
Don't worry, Git will handle it.
Long answer
Unlike e.g. SVN1, Git does not store commits in delta format, but is snapshot-based2,3. While SVN would naively try to apply each merged commit as a patch (and fail, for the exact reason you described), Git is generally able to handle this scenario.
When merging, Git will try to combine the snapshots of both HEAD commits into a new snapshot. If a portion of code or a file is identical in both snapshots (i.e. because a commit was already cherry-picked), Git won't touch it.
Sources
1 Skip-Deltas in Subversion
2 Git Basics
3 The Git object model
这篇关于樱桃选择工作后git如何合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!