我已经从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 -pick应该是你的答案。

应用现有提交引入的更改。

不要忘记阅读bdonlan在这篇文章中关于择优选择后果的回答: “从一个分支中提取所有提交,将指定的提交推到另一个分支”,其中:

A-----B------C
 \
  \
   D

就变成:

A-----B------C
 \
  \
   D-----C'

这个提交的问题是git认为提交包含了它们之前的所有历史

其中C'有不同的SHA-1 ID。 同样地,从一个分支向另一个分支选择提交基本上涉及生成一个补丁,然后应用它,因此也会丢失历史记录。

这种提交id的改变破坏了git在其他事情之间的合并功能(尽管如果谨慎使用,有一些启发式方法会掩盖这一点)。 更重要的是,它忽略了函数依赖关系——如果C语言真的使用了B语言中定义的函数,你永远不会知道。

其他回答

在我的用例中,我们对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

这将给你一个无所畏惧的合并或冲突地狱完全控制。

如果有更好的选择,请随时提出建议。

我会提出一个答案。这就是通过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.

您可以使用git自行选择将单个提交应用到当前分支。

例如:git选择d42c389f

假设你想合并提交e27af03从分支X到主分支。

git checkout master
git cherry-pick e27af03
git push

我们将不得不使用git cherry-pick <commit-number>

场景:我在一个名为发布的分支上,我只想从主分支添加一些更改到发布分支。

步骤1:签出您想要添加更改的分支

Git签出版本

步骤2:获取你想要添加的变更的提交号

例如

git cherry-pick 634af7b56ec

第三步:git push

注意:每次合并都会创建一个单独的提交号。不要为不能工作的合并取提交号。相反,是你想添加的任何常规提交的提交号。