简体   繁体   English

Git(位桶)将两个用户的更改合并到同一分支

[英]Git (Bit Bucket) merge changes from two users to same branch

I have created a branch in Git (bit bucket). 我已经在Git(位存储桶)中创建了一个分支。 Two persons are modifying same file. 两个人正在修改同一文件。 One person committed the file. 一个人提交了文件。 Other person also has local changes in the same file. 其他人在同一文件中也有本地更改。 The second person should be able to merge his code without losing his contents and also the earlier commits should also be intact. 第二个人应该能够合并他的代码而不会丢失其内容,并且较早的提交也应该是完整的。 I searched many options, but could find a clear reply. 我搜索了许多选项,但可以找到明确的答复。 Please help me on this. 请帮我。

Git won't allow you (unless you --force it) to push a set of commits to a remote branch unless the HEAD of that remote branch is an ancestor of that set of commits. Git不允许您(除非您--force it)将一组提交推送到远程分支,除非该远程分支的HEAD是该提交集合的祖先。

In other words, if you push, and the other person pushes after you, the other person's push will fail because their commits aren't descendants of your commits. 换句话说,如果您推送,而另一个人在您之后推送,则另一个人的推送将失败,因为他们的提交不是您提交的后代。 They'll have to fetch your change, rebase their commits on top of that change (or merge it, as an alternative to rebasing) and only then will they be able to push (provided that the remote repo didn't change even more while they were rebasing). 他们将不得不fetch你的变化, rebase上变化的顶部的提交(或merge了,作为替代垫底),只有这样,他们能够推动(前提是远程回购并没有改变甚至更多,而他们正在重新基地)。

Yes, second person will be able to commit his changes. 是的,第二个人将能够提交他的更改。 But before commit he will have to pull the changes of the first person. 但是在提交之前,他将不得不pull第一个人的更改。 After pulling the changes if there is merge conflict, the second person has to fix the those conflict and then commit. 如果存在合并冲突,则在拉出更改后,第二个人必须修复那些冲突然后提交。 Tips: Working on a same file at the same time is not a good idea. 提示:同时处理同一文件不是一个好主意。 Almost always it creates conflict. 几乎总是会造成冲突。

Well, you problem is independent of which git provider use (github, bitbucket, codebase, etc.) 好吧,您的问题与git提供程序的使用无关(github,bitbucket,代码库等)

A possible workflow to your case is the following. 您的情况可能的工作流程如下。

There are two developers, Alice and Bob . 有两个开发人员, AliceBob Both modified the parameters.yml file with next content. 两者都用下一个内容修改了parameters.yml文件。

# parameters.yml
database_user: 'root'
database_password: 'root'

Alice adds a parameter to turn on/off the cache. 爱丽丝添加了一个参数来打开/关闭缓存。

# parameters.yml
database_user: 'root'
database_password: 'root'
cache: true

While Bob adds other parameter to turn on/off the debug mode in the app. Bob添加其他参数以在应用程序中打开/关闭调试模式时。

# parameters.yml
database_user: 'root'
database_password: 'root'
debug: false

The Alice pushes hers changes to super-cache branch. 爱丽丝将她的更改推送到super-cache分支。 After, Bob pushes his changes to super-cache branch, also. 之后, Bob还将其更改也推送到super-cache分支。 However his push fail because, as @PSkocik said, the Bob 's commits are not descendants of Alice 's commits. 但是,他的推送失败,因为正如@PSkocik所说, Bob的提交不是Alice提交的后代。

Bob have to fetch the Alise 's commits before he pushes his commits. 鲍勃必须先推送Alise的提交,然后再推动其提交。 He choices to do a pull with rebase git pull --rebase origin/super-cache 他选择使用rebase进行一次pull git pull --rebase origin/super-cache

Bob will have a conflict in parameters.yml . 鲍勃parameters.yml会有冲突。 The file after resolve the conflict will be 解决冲突后的文件将是

# parameters.yml
database_user: 'root'
database_password: 'root'
debug: false
cache: true

Now, Bob can continue with the merge with git rebase --continue 现在, Bob可以继续使用git rebase --continue合并

After this, Bob have all commits of super-cache branch plus his commits. 此后, Bob拥有super-cache分支的所有提交以及他的提交。

Finally, Bob can push his commits. 最后, 鲍勃可以推动他的提交。

You can see: Git rebase of Atlassian 您可以看到: Atlassian的Git重新定位

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

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