Gitでブランチをpush/pullしてみる
Gitでブランチをpush/pullしてみる。
現状
現状、sample.txtのファイルが1つある状態。
$ cat sample.txt 111 222 333
$ git status # On branch master nothing to commit (working directory clean)
リモートにブランチをpush
まず、ローカルでブランチを作成する。
$ git checkout -b test Switched to a new branch 'test'
git push origin ブランチ名
でリモートにブランチを作成できる。
$ git push origin test Password: Total 0 (delta 0), reused 0 (delta 0) To https://yk5656@github.com/yk5656/sample.git * [new branch] test -> test
ブランチがリモートに作成された状態なら、
あとはgit pushするだけでリモートのブランチに修正を反映できる。
$ vi sample.txt 111 222 333 444 $ git commit -a -m '444を追加' [test adfda42] 444を追加 1 files changed, 1 insertions(+), 0 deletions(-)
$ git push Password: Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 305 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To https://yk5656@github.com/yk5656/sample.git 5ee426c..adfda42 test -> test
リモートのブランチをclone
ディレクトリ名を変えてもう一つcloneする。
$ git clone https://github.com/yk5656/sample.git sample2 $ vi .git/config
ブランチを確認するが、上記で作成したtestブランチは無い。
$ cd sample2
$ git branch
* master
git branch ブランチ名 origin/ブランチ名
でリモートのブランチを作成できる。
$ git branch test origin/test Branch test set up to track remote branch test from origin.
$ git branch
* master
test
あとは、checkoutでブランチを切り替えればOK。
$ git checkout test Switched to branch 'test'
$ git branch
master
* test
git checkout -b ブランチ名 origin/ブランチ名
でブランチの作成と切り替えを同時に行うこともできる。
リモートのブランチをpull
上記のsample2(2つ目のclone)のところから、ファイルを修正してpushする。
$ vi sample.txt 111 222 333 444 555
$ git commit -a -m '555を追加' [test 917d0f1] 555を追加 1 files changed, 1 insertions(+), 0 deletions(-)
$ git push Password: Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 309 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To https://yk5656@github.com/yk5656/sample.git adfda42..917d0f1 test -> test
1つ目のcloneに戻る。
$ cd ../sample $ git branch master * test $ cat sample.txt 111 222 333 444
git pullすると2つ目のcloneでpushした内容が反映される
かと思ったらエラーがでた。
$ git pull Password: remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0) Unpacking objects: 100% (3/3), done. From https://github.com/yk5656/sample adfda42..917d0f1 test -> origin/test You asked me to pull without telling me which branch you want to merge with, and 'branch.test.merge' in your configuration file does not tell me, either. Please specify which branch you want to use on the command line and try again (e.g. 'git pull <repository> <refspec>'). See git-pull(1) for details. If you often merge with the same branch, you may want to use something like the following in your configuration file: [branch "test"] remote = <nickname> merge = <remote-ref> [remote "<nickname>"] url = <url> fetch = <refspec> See git-config(1) for details.
git pull origin ブランチ名
としてやるとうまくいく。
$ git pull origin test Password: From https://github.com/yk5656/sample * branch test -> FETCH_HEAD Updating adfda42..917d0f1 Fast-forward sample.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
変更が反映されている。
$ cat sample.txt 111 222 333 444 555
毎回引数を指定するのが面倒な場合は「-u」を付けて一度pushするといいみたい。
$ git push -u origin test Password: Branch test set up to track remote branch test from origin. Everything up-to-date
すると、git pullでエラーがでなくなった。
$ git pull Password: Already up-to-date.
configにもなんか追加されている。
$ cat .git/config ・・・ [branch "test"] remote = origin merge = refs/heads/test
【参考】 git pullやpushでリモートのブランチ名を指定するのが面倒 - ましめも http://mashi.hatenablog.com/entry/2012/09/17/170023