如何将开发分支中的 git 提交合并到功能分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17859915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:38:19  来源:igfitidea点击:

How to merge git commits in the develop branch to a feature branch

gitgit-mergegit-rebasefeature-branch

提问by gitq

I have a develop branch and a feature branch in my git repo. I added a commit to develop and now I want that commit to be merged to my feature branch. If I do this

我的 git 存储库中有一个开发分支和一个功能分支。我添加了一个提交到开发,现在我希望该提交合并到我的功能分支。如果我这样做

git checkout feature
git merge develop

I end up with a merge commit. Since I'll be merging new commits on develop to my feature branch frequently, I'd like to avoid all these unnecessary merge commits. I saw this answerthat suggested doing a git rebase developbut it ends up rewinding my branch way too far and the rebase fails.

我最终得到了一个合并提交。由于我会经常将新提交的开发合并到我的功能分支,我想避免所有这些不必要的合并提交。我看到这个答案建议做一个,git rebase develop但它最终将我的分支倒退得太远并且变基失败。

Update:What I ended up doing was

更新:我最终做的是

git checkout feature
git merge develop # this creates a merge commit that I don't want
git rebase # this gets rid of the merge commit but keeps the commits from develop that I do want
git push

Update:I just noticed that the original commit on develop gets a different hash when I merge then rebase to the feature branch. I don't think that's what I want because eventually I'll merge feature back into develop and I'm guessing this won't play nice.

更新:我刚刚注意到,当我合并然后 rebase 到功能分支时,develop 上的原始提交获得了不同的哈希值。我不认为那是我想要的,因为最终我会将功能合并回开发中,我猜这不会很好玩。

回答by ben_h

To integrate one branch into another, you have to either merge or rebase. Since it's only safe to rebase commits that aren't referenced anywhere else (not merged to other local branches; not pushed to any remote), it's generally better to merge.

要将一个分支集成到另一个分支中,您必须合并或变基。由于仅对未在其他任何地方引用的提交进行变基是安全的(未合并到其他本地分支;未推送到任何远程),因此合并通常更好。

If your feature branch is purely local, you can rebase it on top of develop. However, it takes time to understand how rebase works, and before you do, it's quite easy to accidentally produce duplicated or dropped commits. Merge commits might look noisy but merging is guaranteed to always be safe and predictable.

如果您的功能分支纯粹是本地的,您可以在开发的基础上重新构建它。然而,了解 rebase 的工作原理需要时间,在你这样做之前,很容易意外地产生重复或丢弃的提交。合并提交可能看起来很吵,但保证合并总是安全且可预测的。

For a better view, try logging everything together in a graph:

为了更好地查看,请尝试将所有内容一起记录在图表中:

git log --all --graph --oneline --decorate

It's also worth considering whether you really needthe commits on developmerged into feature. Often they're things that can be left seperate until featureis merged into developlater.

还值得考虑是否真的需要将提交develop合并到feature. 通常它们是可以分开的东西,直到以后feature合并develop

If you regularly find you do need developcode on featurethen it might be a sign that your feature branches are too long-running. Ideally features should be split in such a way that they can be worked on independently, without needing regular integration along the way.

如果您经常发现确实需要develop代码,feature那么这可能表明您的功能分支运行时间过长。理想情况下,功能应该以可以独立处理的方式进行拆分,而无需在此过程中进行常规集成。

回答by CharlesB

If you only want one commit from the developbranch you can cherry-pick it in your featurebranch:

如果你只想要一个来自develop分支的提交,你可以在你的feature分支中挑选它:

 git checkout feature
 git cherry-pick -x <commit-SHA1>

The commit will be applied as a new one on top of your branch (provided it doesn't generate a conflict), and when you'll merge back the featurebranch Git will cope with it without conflicts.

提交将作为一个新提交应用到您的分支之上(前提是它不会产生冲突),并且当您合并回feature分支时,Git 将在没有冲突的情况下处理它。