我已经从GitHub的存储库中分叉了一个分支,并提交了一些具体的东西给我。现在我发现原来的存储库有一个很好的功能,这是在HEAD。
我想合并它只是没有以前的提交。我该怎么办?我知道如何合并所有的提交:
git branch -b a-good-feature
git pull repository master
git checkout master
git merge a-good-feature
git commit -a
git push
我已经从GitHub的存储库中分叉了一个分支,并提交了一些具体的东西给我。现在我发现原来的存储库有一个很好的功能,这是在HEAD。
我想合并它只是没有以前的提交。我该怎么办?我知道如何合并所有的提交:
git branch -b a-good-feature
git pull repository master
git checkout master
git merge a-good-feature
git commit -a
git push
当前回答
我们将不得不使用git cherry-pick <commit-number>
场景:我在一个名为发布的分支上,我只想从主分支添加一些更改到发布分支。
步骤1:签出您想要添加更改的分支
Git签出版本
步骤2:获取你想要添加的变更的提交号
例如
git cherry-pick 634af7b56ec
第三步:git push
注意:每次合并都会创建一个单独的提交号。不要为不能工作的合并取提交号。相反,是你想添加的任何常规提交的提交号。
其他回答
让我们举个例子来理解:
我有一个分支,比如master,指向X <commit-id>,还有一个新分支指向Y <sha1>。
哪里Y <commit-id> = <主>分支提交-很少提交
现在,对于Y分支,我必须关闭主分支和新分支之间的提交。下面是我们可以遵循的程序:
步骤1:
git checkout -b local origin/new
其中local是分支名称。任何名字都可以给出。
步骤2:
git merge origin/master --no-ff --stat -v --log=300
合并从主分支到新分支的提交,并创建一个日志消息的合并提交,其中包含来自最多<n>个正在合并的实际提交的一行描述。
有关Git合并的更多信息和参数,请参考:
git merge --help
另外,如果你需要合并一个特定的提交,那么你可以使用:
git cherry-pick <commit-id>
我会提出一个答案。这就是通过Export手动更改并提交到目标分支,对于使用像tortoise这样的工具,导出可以真正有效地实现这一点。我知道这在SVN中工作得很好,到目前为止,在git的一些测试中也取得了同样的成功,只要它们是100%相同的,就不应该在未来合并特性分支时解决任何冲突。
Let me show an example: c:/git/MyProject_Master/ModuleA/ c:/git/MyProject_FeatureA/ModuleA/ Let's suppose we wanted all the files from ModuleA's FeatureA branch to be in the master branch. Assume for a moment this is a huge project and there are other modules, and we know from experience that ModuleA has no dependencies that would cause a compiler or functional issue from the new changes of the feature branch. Select the ModuleA folder and choose Export from tortoise. Then pick ModuleA of master to export it into. Finally, do a file difference check on each file to review the changes, using the diff tool, be sure that all calls out are still compatible. Make sure it compiles, thoroughly test. Commit, push. This solution is a time-tested and effective for svn.
假设你想合并提交e27af03从分支X到主分支。
git checkout master
git cherry-pick e27af03
git push
Git -pick应该是你的答案。
应用现有提交引入的更改。
不要忘记阅读bdonlan在这篇文章中关于择优选择后果的回答: “从一个分支中提取所有提交,将指定的提交推到另一个分支”,其中:
A-----B------C
\
\
D
就变成:
A-----B------C
\
\
D-----C'
这个提交的问题是git认为提交包含了它们之前的所有历史
其中C'有不同的SHA-1 ID。 同样地,从一个分支向另一个分支选择提交基本上涉及生成一个补丁,然后应用它,因此也会丢失历史记录。
这种提交id的改变破坏了git在其他事情之间的合并功能(尽管如果谨慎使用,有一些启发式方法会掩盖这一点)。 更重要的是,它忽略了函数依赖关系——如果C语言真的使用了B语言中定义的函数,你永远不会知道。
您可以使用git自行选择将单个提交应用到当前分支。
例如:git选择d42c389f