Kohei Nozaki's blog 

Using remote branches: Push and Pull


Posted on Sunday Mar 09, 2014 at 04:52PM in Technology


Preparation: make 2 clones at local

kyle-no-MacBook:remotebranchprac kyle$ mkdir local1
kyle-no-MacBook:remotebranchprac kyle$ mkdir local2
kyle-no-MacBook:remotebranchprac kyle$ cd local1
kyle-no-MacBook:local1 kyle$ git clone git@github.com:lbtc-xxx/hello.git
Cloning into 'hello'...
remote: Reusing existing pack: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
Checking connectivity... done
kyle-no-MacBook:local1 kyle$ cd ../local2
kyle-no-MacBook:local2 kyle$ git clone git@github.com:lbtc-xxx/hello.git
Cloning into 'hello'...
remote: Reusing existing pack: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
Checking connectivity... done
kyle-no-MacBook:local2 kyle$ 

Push from local1

  • First, modify and commit
kyle-no-MacBook:hello kyle$ echo hello-iam-local1 >> README.md 
kyle-no-MacBook:hello kyle$ git commit -am 'commit at local1'
[master e4ef489] commit at local1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ 
  • Push
kyle-no-MacBook:hello kyle$ git remote -v
origin  git@github.com:lbtc-xxx/hello.git (fetch)
origin  git@github.com:lbtc-xxx/hello.git (push)
kyle-no-MacBook:hello kyle$ git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   9eb01ce..e4ef489  master -> master
kyle-no-MacBook:hello kyle$ 
  • Now we can see the change from GitHub.

Fetch and merge from local2

kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ git remote -v
origin  git@github.com:lbtc-xxx/hello.git (fetch)
origin  git@github.com:lbtc-xxx/hello.git (push)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ git fetch origin
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:lbtc-xxx/hello
   9eb01ce..e4ef489  master     -> origin/master
kyle-no-MacBook:hello kyle$ 
  • But the file is not updated yet.
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ 
  • We have to merge it manually.
kyle-no-MacBook:hello kyle$ git merge origin/master 
Updating 9eb01ce..e4ef489
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
kyle-no-MacBook:hello kyle$ 
  • Logs are updated too.
kyle-no-MacBook:hello kyle$ git log
commit e4ef4897880924b430721186b36212b32ef5cf12
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:59:00 2014 +0900

    commit at local1

Pull

  • Do some work at local2
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ echo hello-iam-local2 >> README.md 
kyle-no-MacBook:hello kyle$ git commit -am 'commit at local2'
[master 059de38] commit at local2
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 277 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   e4ef489..059de38  master -> master
kyle-no-MacBook:hello kyle$ 
  • Pull at local1
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:lbtc-xxx/hello
   e4ef489..059de38  master     -> origin/master
Updating e4ef489..059de38
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
hello-iam-local2
kyle-no-MacBook:hello kyle$ 

Push a branch from local1

kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ git checkout -b b1
Switched to a new branch 'b1'
kyle-no-MacBook:hello kyle$ git remote -v
origin  git@github.com:lbtc-xxx/hello.git (fetch)
origin  git@github.com:lbtc-xxx/hello.git (push)
kyle-no-MacBook:hello kyle$ git push origin b1
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
 * [new branch]      b1 -> b1
kyle-no-MacBook:hello kyle$ 
  • Not we can see an new branch in GitHub

Pull a branch at local2

kyle-no-MacBook:hello kyle$ cd ../../local2/hello
kyle-no-MacBook:hello kyle$ git branch
* master
kyle-no-MacBook:hello kyle$ git pull
From github.com:lbtc-xxx/hello
 * [new branch]      b1         -> origin/b1
Already up-to-date.
  • But we can't see any branches yet.
kyle-no-MacBook:hello kyle$ git branch
* master
  • This makes it appear.
kyle-no-MacBook:hello kyle$ git checkout -b b1 origin/b1
Branch b1 set up to track remote branch b1 from origin.
Switched to a new branch 'b1'
kyle-no-MacBook:hello kyle$
  • Do some work at b1 branch at local2, then push
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ echo hello-iam-local2-b1 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'local2-b1'
[b1 bdbe00c] local2-b1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git push
warning: push.default is unset; its implicit value is changing in
...
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   059de38..bdbe00c  b1 -> b1
kyle-no-MacBook:hello kyle$ 
  • fetch and merge at local1
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ git fetch origin
kyle-no-MacBook:hello kyle$ git branch
* b1
  master
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
hello-iam-local2
kyle-no-MacBook:hello kyle$ git merge origin/b1
Updating 059de38..bdbe00c
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
hello-iam-local2
hello-iam-local2-b1
kyle-no-MacBook:hello kyle$ 

checkout --remote

Create a branch and push

kyle-no-MacBook:hello kyle$ git branch b4
kyle-no-MacBook:hello kyle$ git checkout b4
Switched to branch 'b4'
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ echo b4-local1 > README.md 
kyle-no-MacBook:hello kyle$ git commit -am 'local1-b4'
[b4 75dc62a] local1-b4
 1 file changed, 1 insertion(+), 6 deletions(-)
kyle-no-MacBook:hello kyle$ git push origin b4
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 503 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
 * [new branch]      b4 -> b4
kyle-no-MacBook:hello kyle$ 

fetch and checkout

kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ git fetch
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 1), reused 6 (delta 1)
Unpacking objects: 100% (6/6), done.
From github.com:lbtc-xxx/hello
 * [new branch]      b4         -> origin/b4
kyle-no-MacBook:hello kyle$ git checkout --track origin/b4
Branch b4 set up to track remote branch b4 from origin.
Switched to a new branch 'b4'
kyle-no-MacBook:hello kyle$ cat README.md 
b4-local1
kyle-no-MacBook:hello kyle$ 

Listing remote branches

kyle-no-MacBook:hello kyle$ git branch -r
  origin/HEAD -> origin/master
  origin/b1
  origin/b2
  origin/b3
  origin/b4
  origin/master
kyle-no-MacBook:hello kyle$ 



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed