【问题标题】:Automatically update upstream branches自动更新上游分支
【发布时间】:2014-06-23 08:08:42
【问题描述】:

我有一个存储库 (origin),它是从另一个存储库 (upstream) 分叉的(以 GitHub 术语)。我在主题分支中进行所有开发,从不接触上游存储库中存在的分支(masterdevelopement1.x 等)。

这是我存储库中分支的示例

$ git branch -a     # plus hand annotations
  development   [upstream-maintained: should always be == upstream/development]
  feature-1     [mine: should track origin/feature-1]
  feature-2     [mine: should track origin/feature-2]
* master        [upstream-maintained: should always be == upstream/master]
  remotes/origin/HEAD -> origin/master
  remotes/origin/development
  remotes/origin/feature-1
  remotes/origin/feature-2
  remotes/origin/master
  remotes/upstream/development
  remotes/upstream/gh-pages    [I do not care about this..]
  remotes/upstream/master
  remotes/upstream/stable-1.x  [...nor I care about these two..]
  remotes/upstream/stable-2.x  [...stable-* branches]

从上游存储库获取后,我必须完成更新所有 upstream-maintained 分支的繁琐任务:我切换到 master,我 merge --ff-only upstream/master 并推送到原点.对于我关心的每个上游维护的分支,必须重复此操作。请注意,merge --ff-only 始终有效,因为我从不碰那些树枝。

我希望git pull 为我完成所有这些繁琐的更新。

有没有办法让 git 知道上游维护的分支应该被推送到 origin,但从 upstream 拉出和跟踪?

【问题讨论】:

标签: git


【解决方案1】:

如“How can I pull from one remote and push to another with git?”中所述,您可以:

  • 确保推送到原点:

    git config remote.pushdefault origin
    git config push.default matching
    
  • 设置上游分支:

    git fetch upstream
    git branch -u upstream/master foo
    

请注意,您应该将您的 topic 分支重新设置在 upstream/master 之上(不是合并),以便在您的拉取请求中没有合并提交。
这意味着git push -f 用于更新您的 fork 远程分支(这将自动更新拉取请求)

您可以通过以下方式强制变基:

git config autosetuprebase remote
# or 

git branch.foo.rebase true

例如:

C:\Users\vonc\prog\git\so>git clone https://github.com/gioele/offlineimap.git
Cloning into 'offlineimap'...
remote: Counting objects: 9445, done.
remote: Compressing objects: 100% (3701/3701), done.
remote: Total 9445 (delta 4962), reused 9445 (delta 4962)
Receiving objects: 100% (9445/9445), 5.75 MiB | 2.18 MiB/s, done.
Resolving deltas: 100% (4962/4962), done.
Checking connectivity... done.

C:\Users\vonc\prog\git\so>cd offlineimap

C:\Users\vonc\prog\git\so\offlineimap>git remote add upstream https://github.com/offlineimap/offlineimap.git

让我们看看master现在指的是什么:

C:\Users\vonc\prog\git\so\offlineimap>git branch -avvv
* master                  1746676 [origin/master] Make IDLE mode to work again
  remotes/origin/HEAD     -> origin/master

C:\Users\vonc\prog\git\so\offlineimap>git config --local -l
remote.origin.url=https://github.com/gioele/offlineimap.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
remote.upstream.url=https://github.com/offlineimap/offlineimap.git
remote.upstream.fetch=+refs/heads/*:refs/remotes/upstream/*

让我们将 origin 作为推送的目的地:

C:\Users\vonc\prog\git\so\offlineimap>git config push.default matching
C:\Users\vonc\prog\git\so\offlineimap>git config remote.pushdefault origin

让我们更改上游分支:

C:\Users\vonc\prog\git\so\offlineimap>git fetch upstream
remote: Counting objects: 55, done.
remote: Compressing objects: 100% (55/55), done.
remote: Total 55 (delta 25), reused 1 (delta 0)
Unpacking objects: 100% (55/55), done.
From https://github.com/offlineimap/offlineimap
 * [new branch]      master     -> upstream/master

C:\Users\vonc\prog\git\so\offlineimap>git branch -u upstream/master master
Branch master set up to track remote branch master from upstream.

master分支设置为监控upstream/master

C:\Users\vonc\prog\git\so\offlineimap>git br -avvv
* master                    1746676 [upstream/master: behind 10] Make IDLE mode to work again

现在git pull(或更好的git pull --rebase)将从upstream 拉取:

C:\Users\vonc\prog\git\so\offlineimap>git pull --rebase
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 6bd76fed5a7e1e24310517b3510c465929870c08.

(而6bd76fed5a7e1e24310517b3510c465929870c08upstream/master 提交)

git push 仍会推送到原点:

C:\Users\vonc\prog\git\so\offlineimap>git push
remote: Permission to gioele/offlineimap.git denied to VonC.
fatal: unable to access 'https://github.com/gioele/offlineimap.git/': The requested URL returned error: 403

(正常,因为我不是gioele,所以我没有对该仓库的写入权限)

【讨论】:

  • 不应该交换第一个命令中的分支吗? git branch -u upstream/master master
  • 另外,一旦我使用git config branch.master.remote origingit push 拒绝推送任何内容,因为“一切都是最新的”,而上游还有两个提交。
  • @gioele 如果您不先拉动/变基,那么是的,推动将无济于事。
  • 我又试了一次,先拉了一下,还是不行。您可以尝试将github.com/gioele/offlineimap.git 的当前情况用作origin,将github.com/offlineimap/offlineimap.git 用作upstream。分支 masternext 是上游维护的,不会使用您的配置拉/推。
  • @gioele 我已经修复了要应用的命令的确切顺序,并从您的两个存储库中制作了一个完整的示例:它工作得很好。
猜你喜欢
  • 2016-10-24
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 2021-06-27
  • 2020-08-23
  • 2013-03-21
  • 2015-09-30
相关资源
最近更新 更多