如何“git remote add”并跟踪同一文件系统中的分支

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

How to 'git remote add' and track a branch in the same filesystem

git

提问by mit

I have 2 local git archives in /a and in /b which were cloned from remotes/origin.

我在 /a 和 /b 中有 2 个本地 git 档案,它们是从 remotes/origin 克隆的。

There is a new branch z on /b

/b 上有一个新分支 z

How can I track and fetch branch z from archive /a ?

如何从存档 /a 跟踪和获取分支 z ?

I tried this:

我试过这个:

cd /a
git remote add b /b

This creates 2 config entries, but I did not manage to fetch something or to list remote branches on /a that would show the branches on /b

这会创建 2 个配置条目,但我没有设法获取某些内容或在 /a 上列出远程分支来显示 /b 上的分支



After trying different things I found the following that works:

在尝试了不同的事情后,我发现以下方法有效:

1) git remote show blists all the remote branches in b

1)git remote show b列出b中的所有远程分支

2) I can fetch using this syntax:

2)我可以使用以下语法获取:

git fetch file:///a/ z

git fetch file:///a/ z



Other things that also work:

其他也有效的东西:

$ cd /b
$ git checkout -b z
Switched to a new branch 'z'
$ git pull b z


But those commands still dont work and I cannot understand why:

但是这些命令仍然不起作用,我不明白为什么:

git branch -a 

does not list the remote branches in b (onlz the ones in origin are shown)

不列出 b 中的远程分支(onlz 显示了原始分支)

git checkout -t b/z

Does not checkout anything but returns an error message

不结帐但返回错误消息

回答by idbrii

So far you've only added b as a remote. You can try git branch -ato list your remote branches after you've fetched them.

到目前为止,您只添加了 b 作为遥控器。git branch -a获取远程分支后,您可以尝试列出它们。

Here's the commands to checkout the z branch from b:

这是从 b 检出 z 分支的命令:

git remote add b /b              # you've already done
git fetch b                      # get it so we can see it
git checkout -t b/z              # check out a local tracking branch

The -t(or --track) creates a tracking branch, otherwise you'll be in detached head state.

-t(或--track)创建了一个跟踪分支,否则你会在分离的头的状态。

Then you should see:

然后你应该看到:

/a$ git branch
  master
* z


For anyone unclear on the steps involved, here's what I did:

对于不清楚所涉及步骤的任何人,这就是我所做的:

create origin

创建原点

$ mkdir origin
$ cd origin/
/origin$ git init --bare
Initialized empty Git repository in /origin/
/origin$ cd ..

clone 'a' and add some content

克隆 'a' 并添加一些内容

$ git clone origin/ a
Initialized empty Git repository in /a/.git/
warning: You appear to have cloned an empty repository.
$ cd a
/a$ echo hi there > hello
/a$ git add hello
/a$ git ci -m'first commit'
[master (root-commit) 0867b93] first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello
/a$ git push origin master 
Counting objects: 3, done.
Writing objects: 100% (3/3), 210 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /origin/
 * [new branch]      master -> master

clone 'b' and add more content on new branch

克隆 'b' 并在新分支上添加更多内容

/a$ cd ..
$ git clone origin/ b
Initialized empty Git repository in /b/.git/
$ cd b
/b$ git checkout -b z
Switched to a new branch 'z'
/b$ echo new guy reporting in >> hello 
/b$ git ci -am "new recruits"
[z 81044ee] new recruits
 1 files changed, 1 insertions(+), 0 deletions(-)

Add 'b' as a remote to 'a'

将“b”作为远程添加到“a”

/b$ cd ../a
/a$ git remote add b ../b
/a$ git fetch b
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../b
 * [new branch]      master     -> b/master
 * [new branch]      z          -> b/z
/a$ git br
* master
/a$ git checkout -t b/z
Branch z set up to track remote branch z from b.
Switched to a new branch 'z'
/a$ git br
  master
* z

I've put the above commands into a scriptso you can test it out yourself.

我已将上述命令放入脚本中,以便您可以自行测试。

回答by Ben

I don't think you can do that.

我认为你不能那样做。

I think you'll need to push branch z to origin in b and fetch it from a.

我认为您需要将分支 z 推送到 b 中的原点并从 a 中获取它。

cd /b
git push origin z

That last command pushes local branch z to remote (so pushing z -> origin/z)

最后一个命令将本地分支 z 推送到远程(因此推送 z -> origin/z)

then you can track it locally in repo a:

然后您可以在 repo a 中本地跟踪它:

cd /a
git checkout -b z origin/z

That last command creates (and checks out) a localbranch z that tracks origin/z

最后一个命令创建(并检出)一个跟踪 origin/z的本地分支 z