用git提交到同一分支 [英] committing to the same branch with git

查看:77
本文介绍了用git提交到同一分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有两个人在git分支上工作,他们同时签出,但其中一个先提交,然后另一个提交.最新的提交是否仍会与先前的提交合并,或者多个人可以同时在同一个分支上工作吗?

Let's say that there are two people working on a git branch, they check out at the same time but one of them commits first and then the other commits after. Will the newest commit still be merged with the former commit or can multiple people work on the same branch simultaneously?

推荐答案

好吧,当您克隆git存储库时(这就是您所说的签出"的意思吗?),您实际上是在创建一个新分支.Git分支对于每个存储库都是本地的,而不是全局的.话虽如此,您已经有了一个协议,该协议规定了如何在存储库之间传输分支的更新-例如,当您从远程提取时,默认情况下,远程的"master"分支会合并到您的"master"分支中.而且,当您按下按钮时,您的"master"分支可以被追加到遥控器的master分支.因此,您的主服务器和远程主服务器(如果需要,则为起源/主服务器")是不同的分支,但按照惯例是相关的.

Well, when you clone a git repository (is that what you meant by "check out"?), you're effectively creating a new branch. Git branches are local to each repository, not global. Having said that, you have a protocol for how updates to branches are transmitted between repositories-- when you pull from a remote, by default the remote's "master" branch gets merged into your "master" branch, for instance. And when you push, your "master" branch can be appended to the remote's master branch. So your master and remote's master ("origin/master", if you will) are different branches, but related by convention.

回到重点---您注意到我说您将主分支推送到遥控器时可以附加.如果两个人复制了原点/原件并进行了独立更改(请记住,就像在本地两个分支上进行更改一样),那么一旦一个人推送了他们的更改,另一个人的更改就不是简单地附加到原点/掌握更多-他们必须合并.这在推入时不会发生,只有在拉入时才发生(令人困惑的是,"pull"与"push"并非完全相反:"fetch"与推入是相反的-pull是先获取后是合并(或重新定位)).

Getting back to the point--- you notice I said your master branch can be appended when you push to a remote. If two people have taken a copy of origin/master and made independent changes (remember this is just like making changes on two branches locally), once one person has pushed their changes, the other's person's changes aren't a simple append onto origin/master any more--- they have to be merged. This can't happen when you push, only when you pull (confusingly, "pull" isn't quite the opposite of "push": "fetch" is the opposite of push- a pull is a fetch followed by a merge (or a rebase)).

因此,如果您处在这种情况下,谁先尝试推送其更改,则需要从更新的源/主服务器撤回,合并​​或重新设置其主服务器的版本,然后再进行推送.默认情况下,您无法删除某人对分支的更改并将其替换为您自己的更改:您至少需要执行"git push -f"才能做到,并且远程存储库可以具有设置或钩子,以使其变得相当困难.

So if you're in this situation, whoever is trying to push their changes first needs to pull back from the updated origin/master, merge or rebase their version of master, and then push. By default you can't remove someone's changes to a branch and replace them with your own: you need to at least do "git push -f" to do that, and the remote repository can have settings or hooks to make it considerably harder.

或者他们两个可以事先合作:其中一个拉出另一个的更改,进行合并,然后推送结果.如果更改可能重叠或相互影响,那么这可能是一件好事.记住版本控制系统的第一定律: VCS不能代替通信.

Or the two of them could cooperate beforehand: one of them pull the other's changes, do the merge, and then push the result. This can be a good thing to do if it's likely the changes are going to overlap or affect each other. Remember the First Law of version control systems: a VCS is not a replacement for communication.

这篇关于用git提交到同一分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆