如何添加fork作为分支?

时间:2016-03-05 09:53:53

标签: git github caffe git-fork remote-branch

Caffe有很多分叉,例如MS有自己的Caffe fork

我想要做的是比较原始Caffe的一些fork到master分支。

我知道使用git我可以compare 2 branches,但我的问题是如何将fork添加为分支。

我尝试了this

git clone https://github.com/BVLC/caffe.git
Cloning into 'caffe'...
remote: Counting objects: 32907, done.
remote: Total 32907 (delta 0), reused 0 (delta 0), pack-reused 32907
Receiving objects: 100% (32907/32907), 43.55 MiB | 571 KiB/s, done.
Resolving deltas: 100% (21962/21962), done.

cd caffe_BVLC/

git branch -l
* master

git remote add caffe_MS https://github.com/Microsoft/caffe.git

git remote -v
caffe_MS    https://github.com/Microsoft/caffe.git (fetch)
caffe_MS    https://github.com/Microsoft/caffe.git (push)
origin  https://github.com/BVLC/caffe.git (fetch)
origin  https://github.com/BVLC/caffe.git (push)

git fetch caffe_MS
remote: Counting objects: 409, done.
remote: Total 409 (delta 177), reused 177 (delta 177), pack-reused 232
Receiving objects: 100% (409/409), 136.98 KiB, done.
Resolving deltas: 100% (279/279), completed with 89 local objects.
From https://github.com/Microsoft/caffe
 * [new branch]      bvlc_merge_2016_03_04 -> caffe_MS/bvlc_merge_2016_03_04
 * [new branch]      bvlc_windows_matlab -> caffe_MS/bvlc_windows_matlab
 * [new branch]      master     -> caffe_MS/master
From https://github.com/Microsoft/caffe
 * [new tag]         0.1w       -> 0.1w

git branch -l
* master

git checkout --track caffe_MS/MS
fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'caffe_MS/MS' which can not be resolved as commit?

我做错了什么,我应该指定哪个分支名称?

更新 @ hek2mgl解决方案的结果

git clone https://github.com/BVLC/caffe.git
Cloning into 'caffe'...
remote: Counting objects: 33290, done.
remote: Total 33290 (delta 0), reused 0 (delta 0), pack-reused 33290
Receiving objects: 100% (33290/33290), 43.93 MiB | 484 KiB/s, done.
Resolving deltas: 100% (22252/22252), done.
cd caffe
git remote add caffe_MS https://github.com/Microsoft/caffe.git
git fetch caffe_MS master
remote: Counting objects: 379, done.
remote: Total 379 (delta 111), reused 111 (delta 111), pack-reused 268
Receiving objects: 100% (379/379), 130.06 KiB, done.
Resolving deltas: 100% (246/246), completed with 62 local objects.
From https://github.com/Microsoft/caffe
 * branch            master     -> FETCH_HEAD
git diff caffe_MS/master
fatal: ambiguous argument 'caffe_MS/master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

2 个答案:

答案 0 :(得分:3)

<强>更新

您正在使用古老版本的git。更新到git >= 2以使以下命令有效。我使用2.4.10

以下命令可以解决问题:

# Clone repo
git clone https://github.com/BVLC/caffe.git
cd caffe

# Add remote
git remote add caffe_MS https://github.com/Microsoft/caffe.git

# Fetch remote master from fork
git fetch caffe_MS master

# Compare to local master
# We are currently working in the local master that's why it can be omitted
git diff caffe_MS/master

答案 1 :(得分:1)

  

我知道使用git我可以比较2个分支,但我的问题是如何将fork添加为分支。

当你说知道如何比较两个分支时,我认为你真的意味着你知道如何比较本地分支,你错过的是如何比较远程分支

首先,您可以看到远程分支列表:

git branch -r

您可以通过为远程名称添加前缀来比较远程分支,例如:

git diff origin/master caffe_MS/master

至于最后一个错误,我们不清楚你要对git checkout --track caffe_MS/MS做些什么。您的解释是关于比较分支和添加分支,您从不提及跟踪。如果要从fork中检出分支,可以使用与checkout原始分支时相同的语法:

git checkout -b caffe_MS_master caffe_MS/master