我这里有 2 个分支,即分支 1 和分支 2。分支1增加了很多新功能,分支2也很稳定。今天,我只想将分支 1 中的 1 个功能合并到分支 2 中。因此,我只需运行 gitcherry-pick 。我想应该只有 中的更改会合并到branch2中。但我发现还有更多其他功能的变化。
我认为它只会获得指定提交的差异,对吧?
仅供参考,branch1 中的提交是从其他开发分支合并的,这是否可能导致此问题?
我做错了什么吗?
谢谢。
I have 2 branches here, say branch1 and branch2. There are lots of new feature added in branch1, and the branch2 is stable. Today, I want to merge just 1 feature from branch1 to branch2. So, I just run git cherry-pick <commit-for-feature1-in-branch1
. I suppose there should be only the change in <commit-for-featur1-in-branch1
will be merged into branch2. But I found there are more changes for other features are included.
I thought it will get the diff just for only that specified commit, right?
FYI, the commit in branch1 was merged from other development branch, does this possibly cause this issue?
Anything wrong I did?
Thanks.
发布评论
评论(2)
我也遇到过这种行为...我已经将其追踪到以下解释,但也许有人进一步澄清了这一点:
这是因为提交中的更改取决于先前的更改。因此,在创建您想要挑选的目标分支后,这部分代码被更改了多次。
Git 会回溯历史记录,直到首选源与目标匹配并根据此修订创建补丁。这就是为什么可能会出现更多更改......
我发现这种行为有点可怕,因为人们会期望只选择给定提交哈希中的更改
I have also come across this behavior ... I've tracked it down to the following explanation, but perhaps someone clarify this a it more:
This is because the change in the commit depends on a previous change. So this area of code was changed multiple time after the target branch you want to cherry pick was created.
Git goes back in the history until the cherry pick source matches the target and creates the patch based on this revision. That's why more changes might appear ...
I find this behavior a bit scary, because one would expect that only the changes from the given commit hash are picked
gitcherry-pick 的作用是获取您指定的提交并读取它与其父级之间的差异。这有效地制作了一个补丁。然后它会将此补丁应用到您当前签出的分支。
就您而言,提交包含其他功能的添加。您可以通过查看此提交使用 git log 生成的补丁来仔细检查提交消息是否与您认为的功能相对应:
-p
告诉 log不仅显示提交信息,如作者、日期和提交消息,还包括提交引入的补丁(或差异)。-1
选项告诉 git log 在 1 次提交后停止列出历史记录。What
git cherry-pick
does is it takes the commit that you specify and reads the difference between it and it's parent. This effectively makes a patch. It then applies this patch to your currently checked out branch.In your case, the commit contained the addition of other features. You can double check that the commit message corresponds to what you thought the feature was by looking at the patch that this commit would generate with
git log
:The
-p
tells log to not only show the commit information like author, date and commit message, but to also include the patch (or difference) that the commit introduces. The-1
option tells git log to stop listing history after 1 commit.