简体   繁体   English

如果其他分支已合并到主分支,如何将我的分支合并到主分支

[英]How to merge my branch to master if some other branch has been merged into the master

I'm new to using git with teammates and I don't understand the workflow very well yet.我不熟悉与队友一起使用 git,我还不太了解工作流程。

Let's say master branch is ABC .假设 master 分支是ABC

  1. I branch it into dev1 and develop on it and it was now ABCD .我将它分支到 dev1 并在其上进行开发,现在它是ABCD
  2. Someone branch it into dev2 and his final value was ACCDE .有人将它分支到 dev2 中,他的最终值为ACCDE
  3. Then dev2 merged into master and master branch final value is ACCDE .然后 dev2 合并到 master 和 master 分支最终值为ACCDE

Since dev1 was working on ABC version of master so, how to continue development on dev1 which depends on previous version of master when master value was changed from ABC to ACCDE.由于 dev1 正在开发 master 的 ABC 版本,所以当 master 值从 ABC 更改为 ACCDE 时,如何在 dev1 上继续开发取决于之前的 master 版本。

Added info: I was still working on dev1 so I can't merge dev1 to master yet, and then dev2 which was edited and added with new features was merged into master.补充信息:我还在开发 dev1,所以我还不能将 dev1 合并到 master,然后将经过编辑和添加新功能的 dev2 合并到 master。 The problem is the feature I was working on on dev1 depends on unedited version of master and now the master was changed and I no longer can work on my development on dev1 Thanks问题是我在 dev1 上开发的功能取决于未经编辑的 master 版本,现在 master 已更改,我不再可以在 dev1 上进行开发谢谢

You have to merge dev1 into master and resolve conflicts.您必须将 dev1 合并到 master 并解决冲突。

You need to rebase your dev1 branch on master (and resolve any potential conflicts).您需要在master上重新设置dev1分支(并解决任何潜在的冲突)。

git checkout dev1
git rebase master

See Git Branching - Rebasing .请参阅Git 分支 - 变基

So, you have three branches:所以,你有三个分支:

  • master掌握
  • branch1分支1
  • branch2分支2

There were commits done inside branch1 and branch2.在 branch1 和 branch2 内完成了提交。 Let's assume that you work on branch2 and branch1 is already merged into master.假设您在 branch2 上工作,并且 branch1 已经合并到 master 中。 You could do the following您可以执行以下操作

git checkout master
git merge branch2

However, you may have conflicts if both branches worked on the same files.但是,如果两个分支都处理相同的文件,您可能会遇到冲突。 If you have such a conflict, then you can run如果你有这样的冲突,那么你可以运行

git status

in order to see what the conflicting files are.为了查看冲突的文件是什么。 Opening each and every file you will see sections like打开每个文件,您将看到以下部分

<<<<<<<
=======
>>>>>>>

The first line above signifies the first branch and the last line signifies the end of the last branch.上面的第一行表示第一个分支,最后一行表示最后一个分支的结束。 Between the lines you will find the different versions.在这两行之间,您会发现不同的版本。 You will need to figure out which are the correct lines from each and perform modifications until the code works.您将需要找出每行中哪些是正确的行并执行修改,直到代码正常工作。 Finally, remove the lines above along with the content in-between.最后,删除上面的行以及中间的内容。

However, you may want to avoid solving the conflicts inside master, because that's central and it is better to localize problems in my opinion, so what I actually do, assuming that I'm on branch2:但是,您可能希望避免解决 master 内部的冲突,因为这是中心问题,并且在我看来最好将问题本地化,所以假设我在 branch2 上,我实际上会做什么:

git status
git add myfile1
git add myfile2
git commit
git pull
git push
git checkout master
git pull
git checkout branch2
git merge master

If there are conflicts, then solve them and then如果有冲突,然后解决它们,然后

git checkout master
git merge branch2

EDIT:编辑:

Apparently there was an earlier version of master which differs from branch2, yet, branch1 would add commits that are not desired at this point.显然,master 的早期版本与 branch2 不同,但是,branch1 会添加此时不需要的提交。 So solve it, first:所以先解决它:

git checkout master

to view what's in master.查看 master 中的内容。 Then see what the commits into master were via然后查看提交到 master 是通过什么

git log

You will see a list of commits.您将看到提交列表。 Find the newest commit that's still good and you will see its hash, which is a long alphanumeric string.找到仍然不错的最新提交,您将看到它的 hash,它是一个长字母数字字符串。 Let's call id for now, but you will need its actual value.我们现在调用 id ,但您需要它的实际值。 Now switch back to your branch:现在切换回您的分支:

git checkout branch2

and merge that commit into your branch并将该提交合并到您的分支中

git merge <hash>

Another option is to merge the current version of master into your branch.另一种选择是将当前版本的 master 合并到您的分支中。 You will have to live with those commits eventually, so you might as well merge them into your branch sooner rather than later, to make the merge easier.您最终将不得不忍受这些提交,因此您最好尽快将它们合并到您的分支中,以使合并更容易。

Your working branch is dev1.你的工作分支是 dev1。 Here is the process I use.这是我使用的过程。

Get the latest version of master获取最新版本的master

git checkout master git 结账主控

git pull origin master (I like to be specific about what I pull. It keeps my head on straight) git 拉原点大师(我喜欢具体说明我拉的东西。它让我保持头脑清醒)

Now that your local copy of master is up to date, merge master into your branch.现在您的本地 master 副本是最新的,将 master 合并到您的分支中。 (This is different than merging your branch into master. You only do that when you are completely done with your branch.) (这与将分支合并到 master 不同。只有在完全完成分支时才这样做。)

git checkout dev1 git 结帐 dev1

git merge master (If there are conflicts, resolve them as described by Lajos) git 合并主控(如果有冲突,按照Lajos的描述解决)

If other developers continue to work on master, then you may need to do this periodically.如果其他开发人员继续在 master 上工作,那么您可能需要定期执行此操作。 I prefer to merge master into my dev branch frequently so that at the end of developing my branch I don't have a lot of merge conflicts, which can happen if a lot of development has been done on master.我更喜欢经常将 master 合并到我的 dev 分支中,这样在开发分支结束时我就不会遇到很多合并冲突,如果在 master 上进行了大量开发,就会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM