我已经从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
当前回答
让我们举个例子来理解:
我有一个分支,比如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>
其他回答
Git -pick应该是你的答案。
应用现有提交引入的更改。
不要忘记阅读bdonlan在这篇文章中关于择优选择后果的回答: “从一个分支中提取所有提交,将指定的提交推到另一个分支”,其中:
A-----B------C
\
\
D
就变成:
A-----B------C
\
\
D-----C'
这个提交的问题是git认为提交包含了它们之前的所有历史
其中C'有不同的SHA-1 ID。 同样地,从一个分支向另一个分支选择提交基本上涉及生成一个补丁,然后应用它,因此也会丢失历史记录。
这种提交id的改变破坏了git在其他事情之间的合并功能(尽管如果谨慎使用,有一些启发式方法会掩盖这一点)。 更重要的是,它忽略了函数依赖关系——如果C语言真的使用了B语言中定义的函数,你永远不会知道。
我会提出一个答案。这就是通过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.
在我的用例中,我们对CI CD有类似的需求。 我们使用git流来开发和master分支。 开发人员可以自由地将他们的更改直接合并到开发中,或者通过来自功能分支的拉请求进行合并。但是为了master,我们通过Jenkins以自动的方式合并来自开发分支的稳定提交。
在这种情况下,做樱桃选择不是一个好的选择。然而,我们从commit-id创建一个本地分支,然后将该本地分支合并到master并执行mvn清洁验证(我们使用maven)。如果成功,则使用maven发布插件,使用localCheckout=true选项和pushChanges=false将产品版本工件发布到nexus。最后,当一切都成功时,再将更改和标签推到原点。
示例代码片段:
假设您是在主,如果手动完成。然而在jenkins上,当你签出repo时,你将在默认分支上(如果配置为master)。
git pull // Just to pull any changes.
git branch local-<commitd-id> <commit-id> // Create a branch from the given commit-id
git merge local-<commit-id> // Merge that local branch to master.
mvn clean verify // Verify if the code is build able
mvn <any args> release:clean release:prepare release:perform // Release artifacts
git push origin/master // Push the local changes performed above to origin.
git push origin <tag> // Push the tag to origin
这将给你一个无所畏惧的合并或冲突地狱完全控制。
如果有更好的选择,请随时提出建议。
如果您已将更改提交给主分支。现在您想要将相同的提交移动到release分支。检查提交id(例如:xyzabc123)。
现在试试下面的命令
git checkout release-branch
git cherry-pick xyzabc123
git push origin release-branch
假设你想合并提交e27af03从分支X到主分支。
git checkout master
git cherry-pick e27af03
git push