Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying git local branch between machines?

Tags:

git

OK so I'm tracking a remote repo on two machines so both have the master branch. I also made a local branch on my laptop called development. Is there a way for me to copy that local branch over to my desktop computer? I'd use my laptop but for some reason I'm having trouble with gdb and emacs...

Edit: This worked for me

git remote add laptop [username]@hostname:/path/to/repo
git fetch laptop
git checkout --track -b development laptop/development
git pull
like image 270
gct Avatar asked Sep 14 '09 16:09

gct


2 Answers

Let's assume that you have both repository on 'desktop' machine and repository on 'laptop' machine set up in such way that you can fetch on 'desktop' from 'laptop' and vice versa (e.g. via SSH, or via git:// protocol i.e. via git-daemon, or perhaps via simple HTTP).

Both 'desktop' and 'laptop' machine have 'master' branch, and additionally 'laptop' machine has 'development' branch.

Let first set up remote shortcut, and remote-tracking branches using git remote:

desktop$ git remote add laptop user@laptop:/path/to/repo

This would set up remote named 'laptop' and remote-tracking branches: 'laptop/master' (or to be more exact 'refs/remotes/laptop/master') which follows branch 'master' on 'laptop', and 'laptop/development' which follows branch 'development' on 'laptop'.

Then you need to create local branch 'development' on 'desktop', which would follow (track) remote-tracking branch 'laptop/development' (as you can't develop on remote-tracking branches):

desktop$ git checkout -b development --track laptop/development

or just simply (with modern git)

desktop$ git checkout --track laptop/development

See git-branch manpage for details.

This setup allows to use simply "git pull" when on branch 'development' while on 'desktop', and git would automatically fetch all changes fro repository at 'laptop' into 'laptop/master' and 'laptop/development' remote-tracking branches, and then try to merge 'laptop/development' into 'development' (current branch) -- which would usually result in fast-forward, i.e. simply moving 'development' branch.

Then you can create similar setup on 'laptop' (although setting up existing branch 'development' on 'laptop' to track 'desktop/development' might require editing config file directly or via "git config").

like image 108
Jakub Narębski Avatar answered Oct 15 '22 08:10

Jakub Narębski


Can you not just git pull the remote branch from your laptop? Git will pull over http, ssh - many protocols. My git syntax is rather rusty, but something like…

Edit: Digging through memory and manuals, you may need to initially add the new remote with

git remote add ssh://remotestuff/

then you can

git checkout --track -b localBranchName remote/remoteBranch

to track the branch.

like image 22
Adam Wright Avatar answered Oct 15 '22 07:10

Adam Wright