2014-06-23 62 views
2

我有一個已從另一個存儲庫(upstream)分叉(以GitHub條款)的存儲庫(origin)。我在主題分支中進行所有開發,並且不會觸及上游存儲庫中存在的分支(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] 

後,我從上游存儲庫中取出,我不得不通過更新所有上游保持分支的繁瑣任務的例子:我切換到master,我merge --ff-only upstream/master和推到原點。對於我關心的每個上游維護的分支都必須重複這一點。請注意,merge --ff-only總是有效,因爲我從不碰那些分支。我想git pull爲我做所有這些乏味的更新。

有沒有辦法教git上游維護的分支應該推到origin,但從upstream拉和跟蹤?

+0

這兩個線程有​​什麼用處嗎? http://stackoverflow.com/questions/4577874/git-automatically-fast-forward-all-tracking-branches-on-pull和http://stackoverflow.com/questions/4318161/can-git-pull-all-更新,所有我的本地分支。乾杯! – MBlanc

+0

@MBLANC,不是。這些問題涉及一個遙控器,而在這裏我有兩個不同的遙控器:一個遙控器和一個遙控器。儘管如此,感謝您的鏈接,git-up工具看起來不錯。 – gioele

回答

3

正如在 「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更新你的叉子遠程分支(將自動更新拉入請求)

可以強制變基既可以通過:

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 

讓我們看看現在主人是指什麼:

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/* 

讓我們原點爲目的地的推動:

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 

現在a git pull(或b埃特,一個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,我沒有該回購的寫入權限)

+0

第一個命令中的分支不應該交換嗎? 'git branch -u upstream/master master' – gioele

+0

此外,一旦我使用'git config branch.master.remote origin','git push'拒絕推送任何內容,因爲「一切都是每天一次」,而上游有兩個提交。 – gioele

+0

@gioele如果你不先拉/ rebase,那麼是的,一推就什麼都不會做。 – VonC