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

后端 未结 2 2115
小蘑菇
小蘑菇 2021-02-18 18:18

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

2条回答
  •  渐次进展
    2021-02-18 18:51

    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.

提交回复
热议问题