简体   繁体   English

如何在 GitHub 中将更改从远程分支 A 拉到远程分支 B

[英]How to pull changes from Remote Branch A to Remote Branch B in GitHub

I am working on a project where we have 2 different branches branched out from Master branch.我正在做一个项目,我们有 2 个从 Master 分支分支出来的不同分支。 One is Branch A, where the Backend Team works, and Branch B where Frontend Team(Me) works.一个是后端团队工作的分支 A,前端团队(我)工作的分支 B。 Now the Branch A has some new additions that needed to be included in Branch B, how do I do get the new changes from Branch A to be reflected in Branch B without having conflicts?现在分支 A 有一些新增内容需要包含在分支 B 中,我如何才能让分支 A 的新更改反映在分支 B 中而不会发生冲突?

Master Branch --> Branch A (Branch A has new changes which Branch B needs to have)
            |--> Branch B (How to pull the changes from Branch A to B remotely in GitHub?

and what are the other ways, I can use to achieve the same objective?还有哪些其他方法可以用来实现相同的目标?

Pull Request拉取请求

As @Shamim mentioned in the comments, you coukd create a pull request from branch A to B. The pull request would then allow you to fix potential conflicts and then merge it.正如@Shamim在评论中提到的那样,您可以创建一个从分支 A 到 B 的拉取请求。拉取请求将允许您修复潜在的冲突,然后将其合并。

CLI merge CLI 合并

Another possibility would be to merge it via the command line.另一种可能性是通过命令行合并它。 For doing so, you first need to fetch your remote so you have the latest state of branch A and pull branch B so you have the latest changes there as well.为此,您首先需要获取您的遥控器,以便您拥有分支 A 的最新状态并拉动分支 B,以便您在那里也拥有最新的更改。 Then, you can merge branch A to branch B. This creates a commit to branch B containing all previous changes to branch A. Finally, you need to push the changes.然后,您可以将分支 A 合并到分支 B。这将创建对分支 B 的提交,其中包含对分支 A 的所有先前更改。最后,您需要推送更改。

git checkout B # make sure you are in branch B
git fetch # download all changes/commits
git pull # update branch B
git merge A # merge A into B
git push # push B

avoiding and fixing conflicts避免和解决冲突

(Merge) Conflicts occur when the same part of the same file is changed in multiple branches and those are then merged. (合并)当同一文件的同一部分在多个分支中更改并合并时会发生冲突。 You shouldn't have any conflicts if the backed and fronted files are clearly seperated and different branches are used only for changes to either the frontend or backend.如果后端文件和前端文件明确分开并且不同的分支仅用于对前端或后端的更改,则不应有任何冲突。

If there are conflicts in a CLI merge (the git merge command ), git tells you files that conflict, lets you to fix the conflicts manually, run git add for staging the resolved conflicts and git commit for committing the merge.如果 CLI 合并中存在冲突( git merge命令),git 会告诉您冲突的文件,让您手动修复冲突,运行git add以暂存已解决的冲突,并运行git commit以提交合并。

The GitHub UI also allows you to fix simple merge conflicts but in some cases, you would still need to merge using the CLI even though you have created a Pull Request. GitHub UI 还允许您修复简单的合并冲突,但在某些情况下,即使您创建了拉取请求,您仍需要使用 CLI 进行合并。

However, creating Pull Requests is (often) a best practice as it provides documentation on the history of the repository and allows you to review changes/etc.但是,创建拉取请求(通常)是一种最佳实践,因为它提供了关于存储库历史的文档,并允许您查看更改/等。

You can do this via pull requests or the command line.您可以通过拉取请求或命令行来执行此操作。 It's up to you, really.这取决于你,真的。 From the CLI, you can do the following:在 CLI 中,您可以执行以下操作:

git checkout B
git pull origin A      # A pull is a fetch and a merge in one.
git push               # Assuming no conflicts

If there are conflicts, resolve them manually and then add them via git add .如果存在冲突,请手动解决它们,然后通过git add添加它们。 You can also check to make sure you didn't miss any conflict markers with git diff --check (add --cached if you already added the files to the index).您还可以使用git diff --check检查以确保没有遗漏任何冲突标记(如果您已经将文件添加到索引中,请添加--cached )。 When everything resolved you can do git merge --continue to complete the merge and push afterwards.当一切都解决后,您可以执行git merge --continue以完成合并并随后推送。

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

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