假设您可以放弃“镜像”要求,并让“本地(裸)repo $X 还使用 refs/heads/upstream/$branch 复制上游 repo $UX 以将上游分支命名为 refs/heads/$X”,使用第二种方法,但改为这样做:
$ cd /tmp; mkdir tt; cd tt; git clone --bare ssh://$upstream_host/tmp/t
$ cd t.git
$ git config remote.origin.fetch '+refs/heads/*:refs/heads/upstream/*'
$ git fetch -p # accidentally omitted this step from cut/paste earlier
这假设您不会upstream/master
自己使用任何分支名称。(您也可以/改为执行以下操作:
git config remote.origin.fetch '+refs/*:refs/upstream/*'
但是refs/upstream/*
引用不会被 normal git clone
,git fetch
等复制,所以这对“普通” git 用户来说更痛苦。)
让我们也克隆一个--bare
repo,看看我们继续进行时会发生什么。(作为参考,$upstream_host
我有/tmp/t
一个常规的 git 存储库。我有一个$local_host
不完全镜像的机器,/tmp/tt/t.git
一个--bare
执行上游跟踪的存储库。我实际上对两者使用相同的主机,但原则适用。 ..)
$ cd /tmp; mkdir xt; cd xt; git clone ssh://$local_host/tmp/tt/t.git
Cloning into 't'...
remote: Counting objects: 96, done.
remote: Compressing objects: 100% (54/54), done.
remote: Total 96 (delta 33), reused 96 (delta 33)
Receiving objects: 100% (96/96), 17.11 KiB | 0 bytes/s, done.
Resolving deltas: 100% (33/33), done.
Checking connectivity... done
$upstream_host
现在我对in进行了更改/tmp/t
,并提交了它。回到$local_host
:
$ cd /tmp/tt/t.git; git fetch -p origin # -p will prune deleted upstream/foo's
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From ssh://$host/tmp/t
+ c10e54c...5e01371 master -> upstream/master (forced update)
因此,上游所做的更改将出现在您的“某种镜像但不完全”的裸 git repo 中,作为对upstream/master
而不是master
,或更一般地,upstream/$branch
对于任何$branch
. 如果要合并它们,则必须手动进行。我下面的示例有点混乱,因为我所做的更改$upstream_host
是历史重写(因此所有的forced update
东西),最终通过克隆在这里暴露出来。如果您不希望它暴露,您必须注意哪些更新是历史重写,并且(实际上)手动将它们复制到您自己的非完全镜像,然后复制到任何克隆。我将继续进行真正的合并。
所以,现在我们去非裸回购$local_host
,在/tmp/xt/t
:
$ cd /tmp/xt/t
$ git fetch
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 1), reused 1 (delta 0)
Unpacking objects: 100% (4/4), done.
From ssh://$local_host/tmp/tt/t
+ c10e54c...5e01371 upstream/master -> origin/upstream/master (forced update)
$ git status
# On branch master
nothing to commit, working directory clean
$ git log --oneline --decorate --graph
* 5e01371 (origin/upstream/master) add ast example
| * c10e54c (HEAD, origin/master, origin/HEAD, master) add ast example
|/
* 309b36c add like_min.py
... [snipped]
$ git merge origin/upstream/master
Merge remote-tracking branch 'origin/upstream/master'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
...
$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
...
Counting objects: 1, done.
Writing objects: 100% (1/1), 244 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To ssh://$local_host/tmp/tt/t.git
c10e54c..e571182 master -> master
我现在已经通过非裸克隆更新了--bare
克隆 ( $local_host
, /tmp/tt/t.git
) 以将上游工作合并到我的本地非完全镜像中。修订是我的HEAD
合并,HEAD^1
是曾经origin/upstream/master
(在所有“强制更新”之前)的原始(损坏)更新,并且HEAD^2
是现在origin/upstream/master
(之后)的更正更新:
$ git rev-parse HEAD^2 origin/upstream/master
5e013711f5d6eb3f643ef562d49a131852aa4aa1
5e013711f5d6eb3f643ef562d49a131852aa4aa1
(名称只是upstream/master
在--bare
克隆中,所以git rev-parse
以上来自/tmp/xt/t
not /tmp/tt/t.git
。)