如何使用包含许多分支的远程存储库?

时间:2017-11-05 13:09:03

标签: git git-branch git-merge git-push git-pull

让我们假设一个远程(中央)存储库有几个分支,并且在开始时我在本地拥有远程存储库的精确副本。

现在我想在远程存储库的一个分支中更改某些内容。我可以对远程分支的本地副本进行一些更改,然后尝试推送它,但我想在这种情况下我可能会遇到难以解决的合并冲突。所以,我想这需要做以下事情:

  1. 创建远程分支(分支 Available commands: clear-compiled Remove the compiled class file help Displays help for a command list Lists commands migrate Run the database migrations optimize Optimize the framework for better performance serve Serve the application on the PHP development server tinker Interact with your application key key:generate Set the application key make make:command Create a new Artisan command make:controller Create a new controller class make:event Create a new event class make:job Create a new job class make:listener Create a new event listener class make:mail Create a new email class make:middleware Create a new middleware class make:migration Create a new migration file make:model Create a new Eloquent model class make:policy Create a new policy class make:provider Create a new service provider class make:seeder Create a new seeder class make:test Create a new test class )的本地副本(分支C)的本地副本(分支B)。
  2. 对此副本进行更改"副本的副本" (分支A)。
  3. 再次拉动远程分支(分支C)。它将更新远程存储库的本地副本(分支A)。
  4. 在本地合并副本的副本" (分支B,包含您的更改)到远程存储库(分支C的(更新的)本地副本(分支B)中,包含其他人所做的更改。
  5. 现在,您可以将远程存储库的本地副本(分支A)(包含您的更改和更改)推送到远程分支(B)。
  6. 我猜我的描述可能令人困惑。因此,我尝试用不同的词汇总结:将A复制到A,将B复制到B,修改C,更新C使用新状态B(基本上再次将A复制到A),将B合并到C,将B推送到{{1} }}

    这是要走的路吗?

2 个答案:

答案 0 :(得分:1)

Git中使用共享分支的一个非常常见的工作流程(即几个工程师可能在同一时间/几乎同时修改的分支)如下:

git pull origin the_branch
# work work work
git push origin the_branch

正如您正确指出的那样,当您进行推送时可能遇到问题,因为在您推送的那一刻,其他人可能已经在the_branch之上推送了其他提交。这里有两种基本方法触手可及。首先,您可以将远程the_branch合并到本地分支中,然后推出:

git pull origin the_branch
# possibly resolve merge conflicts, then make a merge commit
git push origin the_branch

此方法将在您的本地分支中创建合并提交,因此该提交通常也可能显示为远程分支的历史记录的一部分。如果你不喜欢合并提交,那么另外一个选择就是重新定位:

git pull --rebase origin the_branch
# again, possibly resolve merge conflicts
git push origin the_branch

如果您使用rebase选项,那么您将直接将提交放在远程分支的顶部,就像您的本地分支已经有其他人最近提交的提交一样。

任何一种方法都存在挥之不去的边缘情况。如果在您的合并/ rebase与您推送的时间之间出现新材料会发生什么。如果发生这种情况,那么你必须再次合并/改组。但根据我的经验,这几乎从未发生过,实际上我甚至不记得曾经发生过这种情况。

答案 1 :(得分:1)

您所描述的是标准开发实践 - 本地和远程分支通常在同一个提交中(在您的情况下为a和b),在第三个分支C上进行开发。

在推到遥控器之前,你拉动,然后在结果顶部进行rebase(通常首选合并)C并推回。

这是IMO最安全的选择,也是良好的做法。