简体   繁体   English

Git 从不同的分支合并恢复代码

[英]Git merge from a different branch reverts code

I have added an image that describes the flow of our git commits.我添加了一张图片来描述我们的 git 提交的流程。 Basically, two users checkout a develop branch.基本上,两个用户签出一个开发分支。 Then they create their own feature branches and make changes to one file (different parts of the file, so no conflicts).然后他们创建自己的功能分支并对一个文件进行更改(文件的不同部分,因此没有冲突)。 First user changes the file and merges to the develop branch early.第一个用户更改文件并提前合并到开发分支。 Later second user who has been working on the earlier copy of the file, makes their new changes, merges to develop as well.后来,一直在处理文件的早期副本的第二个用户进行了新的更改,并合并以进行开发。 Merge from the second user merges without conflict but reverts changes made by the first user.来自第二个用户的合并不会发生冲突,但会还原第一个用户所做的更改。 How do we prevent this?我们如何防止这种情况发生? Sometimes minor details are missed when reviewing pull requests that do not have any merge conflicts.在审查没有任何合并冲突的拉取请求时,有时会遗漏一些小细节。

在此处输入图像描述

Please ask second user to pull the changes from develop before merging.请让第二个用户在合并之前从开发中提取更改。

git pull --rebase

Try to do a rebase and fix the conflicts that may arise:尝试做一个 rebase 并修复可能出现的冲突:

git rebase origin/BRANCH

Then if there is conflict try to fix it first.然后,如果存在冲突,请先尝试修复它。

This will make sure their changes gets to you first and you can deal with them, before commit / push.这将确保他们的更改首先到达您,并且您可以在提交/推送之前处理它们。 It is always best to rebase so changes you made are still in the commit.最好是变基,这样您所做的更改仍在提交中。

Here is the below solution: Two user and two feature branch这是以下解决方案:两个用户和两个功能分支

1.First user merge changes of featureBranch1 to develop 2.Second user do changes in featureBranch2, but second user has to do below steps to incorporate changes done by first user into develop branch after merge 1.第一个用户合并 featureBranch1 的更改以开发 2.第二个用户在 featureBranch2 中进行更改,但第二个用户必须执行以下步骤才能在合并后将第一个用户所做的更改合并到开发分支

git rebase develop (This moves the entire featureBranch2 to begin on the tip of the develop branch, effectively incorporating all of the new commits in develop) git rebase development (这将整个 featureBranch2 移动到开发分支的顶端,有效地将所有新提交合并到开发中)

Sorry about the delayed response.很抱歉延迟回复。 The solution was easier than using rebase.该解决方案比使用变基更容易。 The second user has to simply call git pull origin development while staying in their feature branch before merging the pull request.第二个用户必须简单地调用git pull origin development ,同时在合并拉取请求之前留在他们的功能分支。 If there are conflicts while pulling, the user has to resolve conflicts and push to their branch.如果拉取时发生冲突,用户必须解决冲突并推送到他们的分支。 Then the reviewer should review the pull request again and approve.然后审查者应该再次审查拉取请求并批准。

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

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