Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 858
mit Avatar asked Mar 01 '11 02:03

mit


People also ask

How do you set a remote to track a branch?

When you're publishing a local branch. You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".

How do you push and create a remote branch?

Push a new Git branch to a remote repoClone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.

Can remote repository be local file system?

The main purpose of a remote repository is to place the repository to a central location so that it can be accessed by multiple developers. A remote repository can be accessed via http, ssh or even local (file-system) protocols.

How do I pull one branch from a remote?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.


1 Answers

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.

like image 91
idbrii Avatar answered Sep 20 '22 00:09

idbrii