git 您如何从分叉的 github 存储库中合并非主分支上的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2014914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:00:25  来源:igfitidea点击:

How do you merge changes on non-master branches from a forked github repository?

gitgithubforkgit-branch

提问by Matthew Rankin

In both of the following StackOverflow questions, the accepted answer describes how to merge changes from a forked repository in the situation where you fork a repo, the original repo is modified, and then you want to merge the changes made to the master branch back into your forked repo.

在以下两个 StackOverflow 问题中,接受的答案描述了如何在您分叉存储库、修改原始存储库、然后您想将对主分支所做的更改合并回分叉存储库的情况下合并更改你的分叉回购。

However, I'm not clear on how you keep up to date on the non-master branches in the original repo that you forked. For instance, when I originally forked bitprophet's fabric repository, it contained the following branches:

但是,我不清楚您如何在分叉的原始存储库中的非主分支上保持最新状态。例如,当我最初分叉bitprophet 的结构存储库时,它包含以下分支:

  • master
  • 0.9
  • 0.9-doc-rewrite (no longer exists)
  • path-and-#24 (no longer exists)
  • 掌握
  • 0.9
  • 0.9-doc-rewrite(不再存在)
  • 路径和#24(不再存在)

The last two branches no longer exist, and now there is a new branch flexible-task-declarations. I have fetched, merged, and pushed my master branch, so that master, origin/master, and upstream/master all have the same SHA1 hash and point to the same git snapshot. However, I'm not sure how to remove the branches that no longer exist and update the new branches so that my fork is up to date. Do I need to track each upstream branch and then fetch, merge, and push each branch individually, or is there a better way?

最后两个分支不再存在,现在有一个新分支flexible-task-declarations。我已经获取、合并并推送了我的 master 分支,因此 master、origin/master 和 upstream/master 都具有相同的 SHA1 哈希并指向相同的 git 快照。但是,我不确定如何删除不再存在的分支并更新新分支,以便我的 fork 是最新的。我是否需要跟踪每个上游分支,然后单独获取、合并和推送每个分支,还是有更好的方法?

回答by Matthew Rankin

Scenario 1: Deleting the branches that no longer exist

场景一:删除不再存在的分支

To delete the branches that no longer exist, I followed the instructions in the answer to StackOverflow question How do I delete a Git branch both locally and in Github?by issuing the following commands:

为了删除不再存在的分支,我按照 StackOverflow 问题的答案中的说明操作如何在本地和 Github 中删除 Git 分支?通过发出以下命令:

$ git push origin :0.9-doc-rewrite
$ git push origin :path-and-#24

Scenario 2: Merging changes in an existing non-master branch

场景 2:合并现有非主分支中的更改

To get the upstream/0.9 branch up to date, I did the following:

为了使上游/0.9 分支保持最新状态,我执行了以下操作:

$ git checkout --track origin/0.9
$ git fetch upstream
$ git merge upstream/0.9
$ git push

Scenario 3: Tracking the new non-master branches

场景 3:跟踪新的非主分支

Not sure that this is the best way to handle, but here's what I did:

不确定这是最好的处理方式,但这是我所做的:

$ git branch flexible-task-declarations upstream/flexible-task-declarations
Branch flexible-task-declarations set up to track remote branch flexible-task-declarations from upstream.
$ git checkout flexible-task-declarations
$ git push origin flexible-task-declarations

To confirm that all branches are at the same commit:

要确认所有分支都在同一个提交中:

$ git branch -av

This will show all branches—local and remote—and show the most recent commit message and SHA1 hash.

这将显示所有分支——本地和远程——并显示最近的提交消息和 SHA1 哈希。

Web research that may shed light on a better method for handling scenario 3

可能揭示处理场景 3 的更好方法的网络研究

A key difference between a Git fork, when compared to either a simple Git clone, or an SVN checkout, is that your fork will never keep itself up to date with the master repo unless you do it. Fortunately, there is a simple tool to help you do this. Your fork is separate and equal to the master in Git terms so if you want to track changes to the master you can create a tracking branch in your forked repo and merge those changes into your fork's master branch whenever you want to commit something. I highly recommend the ‘GitHub' gem which is a tool you can install to help you easily track changes in any other repository related to yours. See the README text at the bottom of this page for installation and usage: http://github.com/defunkt/github-gem/tree/master

与简单的 Git 克隆或 SVN 检出相比,Git fork 之间的主要区别在于,除非您这样做,否则您的 fork 永远不会与主存储库保持同步。幸运的是,有一个简单的工具可以帮助您做到这一点。在 Git 术语中,您的 fork 是独立的并且等同于 master,因此如果您想跟踪对 master 的更改,您可以在您的 fork repo 中创建一个跟踪分支,并在您想要提交某些内容时将这些更改合并到您的 fork 的 master 分支中。我强烈推荐“GitHub” gem,它是一个可以安装的工具,可帮助您轻松跟踪与您的任何其他存储库中的更改。安装和使用见本页底部README文本:http: //github.com/defunkt/github-gem/tree/master

Ignore the Github Fork QueueIt's evil! The fork queue is a tool for maintainers who like to pick single commits from contributors but don't wish to merge in their whole branch. If you play around with the fork queue you will corrupt your fork (it can be fixed though, read Something went wrong). Many newbies on github feel like they should do something with the fork queue because there are a lot of possibly conflicting changes in there and they don't know what is the supposed way of keeping one's fork up-to-date. Read Keeping your fork up to date and find out!

忽略 Github Fork 队列这是邪恶的!fork 队列是为喜欢从贡献者那里挑选单个提交但不希望在整个分支中合并的维护者的工具。如果您使用 fork 队列,您将损坏您的 fork(虽然它可以修复,请阅读出错了)。github 上的许多新手都觉得他们应该对 fork 队列做一些事情,因为那里有很多可能相互冲突的更改,而且他们不知道保持自己的 fork 最新的假设方法是什么。阅读让您的前叉保持最新并找出答案!

Django's Github Workflow

Django 的 Github 工作流程

The Django project has instructions on how to Collaborate on Githubwhich uses what appears to be the standard way to handle forking and pulling in upstream changes.

Django 项目有关于如何在 Github协作的说明,它使用似乎是处理分叉和拉入上游更改的标准方法。

Different Initial Fork Configuration

不同的初始叉配置

Long Nguyen's guest post entitled Setting up your Git repositories for open source projects at GitHubon Michael Hartl's blog describes an interesting method to setup a Github repository that you've forked. The goals of this method according to the article are to:

Long Nguyen在 Michael Hartl 的博客上题为“在 GitHub为开源项目设置 Git 存储库”的客座文章描述了一种设置您已分叉的 Github 存储库的有趣方法。根据文章,此方法的目标是:

  • Keep the repositories in sync so that each contains the full “official” repository
  • Allow developers to pull in official updates
  • Encourage working on branches other than master
  • 保持存储库同步,以便每个都包含完整的“官方”存储库
  • 允许开发者拉取官方更新
  • 鼓励在 master 以外的分支上工作

回答by VonC

Basically, you have 3 remote Git repo ton consider:

基本上,您有 3 个远程 Git 存储库,请考虑:

  • local: your current git repo on your workstation.
  • origin: which is your forked version of fabric: cumulusware
  • upstream: bitprophet / fabric, the one at the origin of all the other forked repo
  • 本地:您工作站上当前的 git 存储库。
  • 来源:这是你的分叉版结构:cumulusware
  • 上游:bitprophet / fabric,所有其他分叉回购的起源

You could add upstream as a remote repo to your local.

您可以将上游作为远程存储库添加到本地。

 git remote add upstream http://github.com/bitprophet/fabric.git

look at the remote branches

查看远程分支

 git branch -r 

and push (delete) the ones which exists at origin but no longer exist at upstream

并推送(删除)在原点存在但在上游不再存在的那些

 git push origin :anOldBranch

Then

然后

 git remote prune origin

to clear the branches on your local repo (which do not exist anymore on origin, since you just deleted them)

清除本地存储库上的分支(它们在原点上不再存在,因为您刚刚删除了它们)

The branches which do exist both in origin and upstream need to be synchronized as well (rebasing your local branches on top of those from upstream before pushing your work to origin can be a good way to make very easy pull request to upstream: bitprophet would only have fast-forward merge to do to include your work)

源和上游中确实存在的分支也需要同步(在将您的工作推送到源之前,将您的本地分支重新建立在上游分支的基础上可以是向上游发出非常简单的拉取请求的好方法:bitprophet 只会进行快进合并以包含您的工作)

I do not know if you could have a more simple process/command/script to synchronize two distant repositories.

我不知道您是否可以有一个更简单的过程/命令/脚本来同步两个远程存储库。

回答by Jason Ozias

I came across this answer while looking how to do Scenario 3 in the accepted answer cleanly. After some digging around, with git 1.8 you can do the following:

我在寻找如何在已接受的答案中干净地执行场景 3 时遇到了这个答案。经过一番挖掘,使用 git 1.8,您可以执行以下操作:

git fetch upstream                      ;make sure you have all the upstream changes
git checkout --no-track upstream/0.9    ;grab the new branch but don't track it
git branch --set-upstream-to=origin/0.9 ;set the upstream repository to your origin
git push                                ;push your new branch up to origin