Git 重新定位到上游

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6850284/
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 05:44:52  来源:igfitidea点击:

Git rebasing to upstream

git

提问by Drew

So I forked a project, and made a few changes and pushed those to origin/master. I didn't intend to send these changes back to upstream. All was fine and good until now, but now I have some changes I do want to push upstream.

所以我分叉了一个项目,做了一些更改并将它们推送到 origin/master。我不打算将这些更改发送回上游。到目前为止一切都很好,但现在我有一些改变,我确实想推动上游。

Can I rebase back to upstream in a different branch, and commit to that branch? Can I commit these changes from my branch? Have I horribly mangled my repo?

我可以在不同的分支中重新回到上游,并提交到该分支吗?我可以从我的分支提交这些更改吗?我是否严重破坏了我的回购?

采纳答案by Ryan Stewart

No, no mangling. Just branch from upstream/master, make your commits there, and then you can push (or pull-request) those commits, which fit neatly onto upstream/master.

不,没有破坏。只需从上游/主分支分支,在那里进行提交,然后您就可以推送(或拉取请求)那些与上游/主节点完美契合的提交。

If you have A---B---C, where upstream/master is at A and master is at C, meaning B and C are commits you don't want to send upstream, then:

如果您有 A---B---C,其中上游/主控位于 A,主控位于 C,这意味着 B 和 C 是您不想向上游发送的提交,则:

git checkout -b to-send-upstream A
# work, work, work
# commits
git log A..HEAD # this will be the commits to send upstream

If you have commits you're not going to send back on master, it might be simpler to keep track of things if you move those into a different branch and keep your master in sync with upstream/master:

如果你有提交,你不会在 master 上发回,如果你将它们移动到不同的分支并保持你的 master 与上游/master 同步,那么跟踪事情可能会更简单:

git branch my-stuff-not-sent-upstream
git reset --hard A # will wipe out local changes!
git push origin master -f # can lose history if a shared remote!
git push origin my-stuff-not-sent-upstream

will effectively replace "master" with "my-stuff-not-sent-upstream" and set master and origin/master back to the same commit as upstream/master.

将有效地用“my-stuff-not-sent-upstream”替换“master”,并将 master 和 origin/master 设置回与 upstream/master 相同的提交。

回答by cmcginty

Assuming commits A,B,C are your private changes, and commits 1,2,3 are what you want to push up, your history will look like so:

假设提交 A、B、C 是您的私有更改,而提交 1、2、3 是您要推送的内容,那么您的历史记录将如下所示:

                   (origin/master)
                  /
.... o - A - B - C - 1 - 2 - 3  (master)
      \
       (upstream/master)

Now you want to move 1,2,3 to a new branch from upstream:

现在你想从上游移动 1,2,3 到一个新的分支:

git checkout master
git checkout -b upstream
git rebase origin/master --onto upstream/master

What this does is switch to a new branch called upstreamwhere your current masteris. Then it rebases commits 1,2,3 after commit referenced by upstream/master.

这样做是切换到一个名为upstream当前所在位置的新分支master。然后它在upstream/master.

After the operation you will have:

手术后您将拥有:

         1' - 2' - 3' (upstream)
        /
       /           (origin/master)
      /           /
.... o - A - B - C - 1 - 2 - 3  (master)
      \
       (upstream/master)

Now you will be ready to push upstream branch to the upstream remote.

现在您将准备好将上游分支推送到上游远程。

回答by VonC

First, you need to add upstream as a remote.

首先,您需要将上游添加为远程。

git remote add upstream https://github.com/upstream/repo.git

Meaning you not only have "origin" (which references your own clone on the GitHub side), but also "upstream" (which is the original repo you have forked on the GitHub side).

这意味着您不仅拥有“来源”(在 GitHub 端引用您自己的克隆),而且还有“上游”(这是您在 GitHub 端分叉的原始存储库)。

Then you need to fetch upstream, and checkout from there:

然后你需要获取上游,并从那里结帐:

git checkout -b upstream/master

You can make your commits there, and then push back to origin, allowing you to make a clean pull request from there.

你可以在那里提交,然后推回原点,允许你从那里发出一个干净的拉取请求。