How to 'git remote add' and track a branch in the same filesystem
I have 2 local git archives in /a and in /b which were cloned from remotes/origin.
There is a new branch z on /b
How can I track and fetch branch z from archive /a ?
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
After trying different things I found the following that works:
1) git remote show b
lists all the remote branches in b
2) I can fetch using this syntax:
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)
git checkout -t b/z
Does not checkout anything but returns an error message
So far you've only added b as a remote. You can try git branch -a
to list your remote branches after you've fetched them.
Here's the commands to checkout the z branch from b:
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.
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
$ 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
/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$ 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 script so you can test it out yourself.
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.
cd /b
git push origin z
That last command pushes local branch z to remote (so pushing z -> origin/z)
then you can track it locally in repo a:
cd /a
git checkout -b z origin/z
That last command creates (and checks out) a local branch z that tracks origin/z
精彩评论