如何使 git 分支与 master 保持同步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16329776/
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:04:15  来源:igfitidea点击:

How to keep a git branch in sync with master

git

提问by Mr. EZEKIEL

At the moment git is doing my head in, I cannot come up with the best solution for the following.

目前 git 正在做我的头脑,我无法想出以下最佳解决方案。

There are two branches, one called masterand one called mobiledevicesupport. I want to keep mobiledevicesupport as a continuous branch that will be merged/synced with the master branch whenever mobiledevicesupport is stable. This would merge changes from mobiledevicesupport into master but also bring all the changes from master into mobiledevicesupport so that branch can continue to be worked on and the features improved or amended. This needs to work with a central repository and multiple developers.

有两个分支,一个叫做master,一个叫做mobiledevicesupport。我想将 mobiledevicesupport 作为一个连续的分支,只要 mobiledevicesupport 稳定,就会与 master 分支合并/同步。这会将来自 mobiledevicesupport 的更改合并到 master 中,但也将所有更改从 master 带入 mobiledevicesupport,以便分支可以继续工作并改进或修改功能。这需要与中央存储库和多个开发人员一起使用。

Please an example of similar workflows other people use or just tell me if this idea is stupid and I should consider other options. At the moment the workflow seems sound, but I just don't know how I can make git work this way.

请举一个其他人使用的类似工作流程的例子,或者告诉我这个想法是否愚蠢,我应该考虑其他选择。目前工作流程似乎很合理,但我不知道如何让 git 以这种方式工作。

Thanks, all help much appreciated.

谢谢,非常感谢所有帮助。

Update 1: If I was to merge master into mobiledevicesupport and mobiledevice support into master, do I get replicated commits across both branches. Or is git smart enough to work out that I have pulled the latest changes from branch A into branch B and add merge commit C to branch B. And I have pulled the latest changes from branch B into branch A and add merge commit D to branch A?

更新 1:如果我将 master 合并到 mobiledevicesupport 并将 mobiledevice support 合并到 master,我是否会在两个分支之间复制提交。或者 git 足够聪明,可以计算出我已将最新更改从分支 A 拉到分支 B 并将合并提交 C 添加到分支 B。并且我已将最新更改从分支 B 拉到分支 A 并将合并提交 D 添加到分支一种?

I was going to post an image but I don't have enough reputation for it, so I guess the following illustration will have to do. Two branches continuously running with merges going both directions often. The key thing I am not sure about is how git will play out the commits and will it fill either branch with the commits from the other branch on merges or will it stay clean. I have used rebase before but it seems to end the branch and put all the commits into the master, or I did it wrong. Thanks for the help so far.

我打算发布一张图片,但我没有足够的声誉,所以我想下面的插图将不得不做。两个分支连续运行,合并经常双向进行。我不确定的关键是 git 将如何执行提交,以及它是否会在合并时用来自另一个分支的提交填充任一分支,或者它会保持干净。我之前使用过 rebase,但它似乎结束了分支并将所有提交放入 master,或者我做错了。感谢你目前的帮助。

master
A--B--C-----H--I--J--M--N
       \   /    \
mobile  \ /      \
D--E--F--G--------K--L

回答by concept47

yes just do

是的就做

git checkout master
git pull
git checkout mobiledevicesupport
git merge master

to keep mobiledevicesupport in sync with master

保持 mobiledevicesupport 与 master 同步

then when you're ready to put mobiledevicesupport into master, first merge in master like above, then ...

然后当你准备好将 mobiledevicesupport 放入 master 时,首先像上面一样合并 master,然后......

git checkout master
git merge mobiledevicesupport
git push origin master

and thats it.

就是这样。

the assumption here is that mobilexxx is a topic branch with work that isn't ready to go into your main branch yet. So only merge into master when mobiledevicesupport is in a good place

这里的假设是 mobilexxx 是一个主题分支,其工作尚未准备好进入您的主分支。所以只有在mobiledevicesupport处于良好位置时才合并到master

回答by euphoria83

Whenever you want to get the changes from master into your work branch, do a git rebase <remote>/master. If there are any conflicts. resolve them.

每当您想将 master 的更改放入您的工作分支时,请执行git rebase <remote>/master. 如果有任何冲突。解决它们。

When your work branch is ready, rebase again and then do git push <remote> HEAD:master. This will update the master branch on remote (central repo).

当您的工作分支准备就绪时,再次变基,然后执行git push <remote> HEAD:master. 这将更新远程(中央仓库)上的主分支。

回答by IwishIcanFLighT

concept47's approach is the right way to do it, but I'd advise to merge with the --no-ff option in order to keep your commit history clear.

concept47 的方法是正确的方法,但我建议与 --no-ff 选项合并,以保持您的提交历史清晰。

git checkout develop
git pull --rebase
git checkout NewFeatureBranch
git merge --no-ff master

回答by sachinjain024

Yeah I agree with your approach. To merge mobiledevicesupport into master you can use

是的,我同意你的方法。要将 mobiledevicesupport 合并到 master 中,您可以使用

git checkout master
git pull origin master //Get all latest commits of master branch
git merge mobiledevicesupport

Similarly you can also merge master in mobiledevicesupport.

同样,您也可以在 mobiledevicesupport 中合并 master。

Q. If cross merging is an issue or not.

问:交叉合并是否是一个问题。

A. Well it depends upon the commits made in mobile* branch and master branch from the last time they were synced. Take this example: After last sync, following commits happen to these branches

答:这取决于上次同步时在 mobile* 分支和 master 分支中所做的提交。举个例子:上次同步后,这些分支发生以下提交

Master branch: A -> B -> C [where A,B,C are commits]
Mobile branch: D -> E

Now, suppose commit B made some changes to file a.txt and commit D also made some changes to a.txt. Let us have a look at the impact of each operation of merging now,

现在,假设提交 B 对文件 a.txt 进行了一些更改,提交 D 也对 a.txt 进行了一些更改。现在让我们看看合并的每个操作的影响,

git checkout master //Switches to master branch
git pull // Get the commits you don't have. May be your fellow workers have made them.
git merge mobiledevicesupport // It will try to add D and E in master branch.

Now, there are two types of merging possible

现在,有两种类型的合并可能

  1. Fast forward merge
  2. True merge (Requires manual effort)
  1. 快进合并
  2. 真正的合并(需要手动操作)

Git will first try to make FF merge and if it finds any conflicts are not resolvable by git. It fails the merge and asks you to merge. In this case, a new commit will occur which is responsible for resolving conflicts in a.txt.

Git 将首先尝试进行 FF 合并,如果发现任何冲突无法通过 git 解决。它合并失败并要求您合并。在这种情况下,将发生一个新的提交,负责解决 a.txt 中的冲突。

So Bottom line is Cross merging is not an issue and ultimately you have to do it and that is what syncing means. Make sure you dirty your hands in merging branches before doing anything in production.

所以底线是交叉合并不是问题,最终你必须这样做,这就是同步的意思。在生产中做任何事情之前,确保你在合并分支时弄脏了你的手。

回答by Gob00st

The accepted answer via git merge will get the job done but leaves a messy commit hisotry, correct way should be 'rebase' via the following steps(assuming you want to keep your feature branch in sycn with develop before you do the final push before PR).

通过 git merge 接受的答案将完成工作,但会留下混乱的提交历史,正确的方法应该是通过以下步骤“rebase”(假设您希望在 PR 之前进行最终推送之前将您的功能分支与 develop 保持在 sycn 中)。

1 git fetchfrom your feature branch (make sure the feature branch you are working on is update to date)

1git fetch来自您的功能分支(确保您正在处理的功能分支是最新的)

2 git rebase origin/develop

2 git rebase origin/develop

3 if any conflict shall arise, resolve them one by one

3、如有冲突,一一解决

4 use git rebase --continueonce all conflicts are dealt with

4git rebase --continue处理完所有冲突后使用

5 git push --force

5 git push --force

回答by faisal

You are thinking in the right direction. Merge master with mobiledevicesupport continuously and merge mobiledevicesupport with master when mobiledevicesupport is stable. Each developer will have his own branch and can merge to and from either on master or mobiledevicesupport depending on their role.

您正在朝着正确的方向思考。不断地将 master 与 mobiledevicesupport 合并,并在 mobiledevicesupport 稳定时将 mobiledevicesupport 与 master 合并。每个开发人员都有自己的分支,并且可以根据他们的角色在 master 或 mobiledevicesupport 上合并和合并。