Jex’s Note

使用grb(git_remote_branch)實例操作

安裝

gem install git_remote_branch

[1] 在github開一個 practice_grb.git

[2] 使用grb create 在 practice_grb.git 建立一個遠端 master

隨便建立一個資料夾並且初始化

jex_lin@devm3 { ~/public_html  }$ mkdir test
jex_lin@devm3 { ~/public_html  }$ cd test
jex_lin@devm3 { ~/public_html/test  }$ git init
Initialized empty Git repository in /home/jex_lin/public_html/test/.git/

增加remote

jex_lin@devm3 { ~/public_html/test  }$ git remote add origin git@github.com:jex-lin/practice_grb.git

因為資料夾才剛初始化完,如果執行grb create master會出現error

jex_lin@devm3 { ~/public_html/test  }$ grb create master
git_remote_branch version 0.3.6

git push origin :refs/heads/master
remote: warning: Deleting a non-existent ref.
To git@github.com:jex-lin/practice_grb.git
 - [deleted]         master

git fetch origin

git branch --track master origin/master
fatal: Not a valid object name: 'origin/master'.

git checkout master
error: pathspec 'master' did not match any file(s) known to git.

所以要先新增一筆資料並且add、commit

jex_lin@devm3 { ~/public_html/test  }$ vim test
jex_lin@devm3 { ~/public_html/test  }$ git add test
jex_lin@devm3 { ~/public_html/test  }$ git commit -m "I am master v1.0"
[master (root-commit) c39ac4a] I am master v1.0
 1 file changed, 1 insertion(+)
 create mode 100644 test

再一次建立遠端主機的master。建立完後可以到github看到master這個主線了

jex_lin@devm3 { ~/public_html/test (master) }$ grb create master
git_remote_branch version 0.3.6

git push origin master:refs/heads/master
Counting objects: 3, done.
Writing objects: 100% (3/3), 226 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:jex-lin/practice_grb.git
 * [new branch]      master -> master

git fetch origin

git branch --track master origin/master
fatal: A branch named 'master' already exists.

git checkout master
Already on 'master'

[3] 模擬另一個開發者(test2)

jex_lin@devm3 { ~/public_html  }$ mkdir test2
jex_lin@devm3 { ~/public_html  }$ cd test2
jex_lin@devm3 { ~/public_html/test2  }$ git init
Initialized empty Git repository in /home/jex_lin/public_html/test2/.git/
jex_lin@devm3 { ~/public_html/test2  }$ git remote add origin git@github.com:jex-lin/practice_grb.git

將遠端主機的master pull下來

jex_lin@devm3 { ~/public_html/test2  }$ git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:jex-lin/practice_grb
 * branch            master     -> FETCH_HEAD

修改檔案、add、commit

jex_lin@devm3 { ~/public_html/test2 (master) }$ vim test
jex_lin@devm3 { ~/public_html/test2 (master) }$ git add test
jex_lin@devm3 { ~/public_html/test2 (master) }$ git commit -m "I am test, master v2.0"
[master c2d0b59] I am test, master v2.0
 1 file changed, 1 insertion(+)

push 到github的master

jex_lin@devm3 { ~/public_html/test2 (master) }$ git push
Counting objects: 5, done.
Writing objects: 100% (3/3), 271 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:jex-lin/practice_grb.git
   c39ac4a..c2d0b59  master -> master

就會看到 github 上就會有新的commit了

當然也可以在遠端建立develop分支

jex_lin@devm3 { ~/public_html/test2 (master) }$ grb create develop
git_remote_branch version 0.3.6

git push origin master:refs/heads/develop
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:jex-lin/practice_grb.git
 * [new branch]      master -> develop

git fetch origin

git branch --track develop origin/develop

git checkout develop
Switched to branch 'develop'

修改檔案、add、commit

jex_lin@devm3 { ~/public_html/test2 (develop) }$ vim test
jex_lin@devm3 { ~/public_html/test2 (develop) }$ git add test
jex_lin@devm3 { ~/public_html/test2 (develop) }$ git commit -m "I am test2, develop v1.0"
[develop 43d1ca6] I am test2, develop v1.0
 1 file changed, 1 insertion(+)

push到github的develop

jex_lin@devm3 { ~/public_html/test2 (develop) }$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:jex-lin/practice_grb.git
   c2d0b59..43d1ca6  develop -> develop

在github會看到develop就會多一個commit了

查看test2的遠端分支

jex_lin@devm3 { ~/public_html/test2 (develop) }$ git branch -r
  origin/develop
  origin/master

回test查看的遠端分支

jex_lin@devm3 { ~/public_html/test2 (develop) }$ cd ..
jex_lin@devm3 { ~/public_html  }$ cd test
jex_lin@devm3 { ~/public_html/test (master) }$ git branch -r
  origin/master

可以發現到test2比test多了develop分支

[4] 將develop分支加入到test

jex_lin@devm3 { ~/public_html/test (master) }$ grb track develop
git_remote_branch version 0.3.6

git fetch origin
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0)
Unpacking objects: 100% (6/6), done.
From github.com:jex-lin/practice_grb
 * [new branch]      develop    -> origin/develop
   c39ac4a..c2d0b59  master     -> origin/master

git branch --set-upstream develop origin/develop
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to

再查看test的遠端分支就有develop了

jex_lin@devm3 { ~/public_html/test (master) }$ git branch -r
  origin/develop
  origin/master

[5] 疑難排解

發現如果在test2修改並且push,切回test時pull失敗

jex_lin@devm3 { ~/public_html/test (master) }$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

解法: 重新將本機的master與遠端的master綁定,就可以pull了

jex_lin@devm3 { ~/public_html/test (master) }$ git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.
jex_lin@devm3 { ~/public_html/test (master) }$ git pull
Updating c39ac4a..4375ac9
Fast-forward
 qq   | 1 +
 test | 2 ++
 2 files changed, 3 insertions(+)
 create mode 100644 qq

develop 也是一樣的情況,步驟一樣,先綁定遠端的develop,再pull

jex_lin@devm3 { ~/public_html/test (develop) }$ git branch --set-upstream-to=origin/develop develop
Branch develop set up to track remote branch develop from origin.
jex_lin@devm3 { ~/public_html/test (develop) }$ git pull
Updating 43d1ca6..c625ce4
Fast-forward
 test | 1 +
 1 file changed, 1 insertion(+)

Comments