tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【github】git branch -a(-r)、git branch -vv、git remote show originの違いを確認してみる

はじめに

ブランチ操作(というよりは確認)で使う下記のコマンドがどういう違いがあるのか把握したくなったのでメモ。

  • git branch -a(-r)
  • git branch -vv
  • git remote show origin

また、gitでのローカル、追跡ブランチ、リモートについて知っておいたほうが話しがわかるのでこちらを見てからの方がわかりやすいです。
git branchメモ - ローカルやリモートのブランチ一覧を見る。リモートブランチ(追跡ブランチ)の情報を更新する(オプション-r、-a)

ながれ

  1. 事前準備
  2. git branch -a(-r)git branch -vvgit remote show originの確認
    • 初期状態で確認
    • 特定のブランチをfetchして確認
    • pullしてみる確認
    • git branch --set-upstream-to=origin/ branch_01してみて確認

1. 事前準備

今回の確認のために、hogeさんとfugaさんを自作自演で用意します。

擬似的にhogeさんがリモートですでにブランチをいくつか作った体にして、
最終的にfugaさんで確認します。

そのための事前準備

  • github上で適当なリポジトリ作る
  • ローカルでhogeさんとfugaさん用のディレクトリ作ってそれぞれでcloneする
  • hogeでbranchを2つ切ってそれぞれで修正&commit&ブランチpushする

ここまで準備できたら最後にfugaさんの方に移動して確認って流れですね。

github上で適当なリポジトリ作る

これはリアルに作ります。今回作ったのはこれ
https://github.com/tweeeety/github-branch-test

ローカルでhogeさんとfugaさん用のディレクトリ作ってそれぞれでcloneする

作ったあとの構成はこんな感じ

$ pwd
/Users/hoge/適当なディレクトリ

$ tree
.
├── fuga
│   └── github-branch-test
│       └── README.md
└── hoge
    └── github-branch-test
        └── README.md

hogeでbranchを2つ切ってそれぞれで修正&commit&ブランチpushする

hoge側でbranchを2つ切ってそれぞれで修正&commit&ブランチpushします。

ついでにhoge側で、最初の状態、branch切ってpushした状態で
git branch -a(-r)git branch -vvgit remote show originも確認してみます。

最初の状態 で確認

当然だがmasterに関するものしかない

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

$ git branch -vv
* master bcdc8ee [origin/master] Initial commit

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:tweeeety/github-branch-test.git
  Push  URL: git@github.com:tweeeety/github-branch-test.git
  HEAD branch: master
  Remote branches:
    master    tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)
branch切ってpush で確認

まずはこんな感じで作業

※ branch_01とbranch_02を切る
$ git branch branch_01
$ git branch branch_02
$ git branch
  branch_01
  branch_02
* master

※ branch_01で編集、commit、push
$ git checkout branch_01
$ vim README.md
---- vi追記 ----
modify from branch_01
---------------

$ git add README.md
$ git commit -m 'add memo from branch_01'
$ git push origin branch_01

※ branch_02で編集、commit、push
$ git checkout branch_02
$ vim README.md
---- vi追記 ----
modify from branch_02
---------------

$ git add README.md
$ git commit -m 'add memo from branch_02'
$ git push origin branch_02

そしてこのときにも確認

$ git branch -a
  branch_01
* branch_02
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch_01
  remotes/origin/branch_02
  remotes/origin/master

$ git branch -vv
  branch_01 775d607 add memo from branch_01
* branch_02 ac5503e add memo from branch_02
  master    bcdc8ee [origin/master] Initial commit

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:tweeeety/github-branch-test.git
  Push  URL: git@github.com:tweeeety/github-branch-test.git
  HEAD branch: master
  Remote branches:
    branch_01 tracked
    branch_02 tracked
    master    tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local refs configured for 'git push':
    branch_01 pushes to branch_01 (up to date)
    branch_02 pushes to branch_02 (up to date)
    master    pushes to master    (up to date)

git remote show originだとRemote branchesにtrackedされてるのがわかります。

2. git branch -a(-r)git branch -vvgit remote show originの確認

ここからはfugaディレクトリに移動して確認

$ pwd
/Users/hoge/適当なディレクトリ/fuga

初期状態で確認

fuga的には何もしてないのでgit branch -agit branch -vv
masterに関してのみ情報が表示されますが、
git remote show originだとbranch_01、branch_02があるよってことを教えてくれます。

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

$ git branch -vv
* master bcdc8ee [origin/master] Initial commit

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:tweeeety/github-branch-test.git
  Push  URL: git@github.com:tweeeety/github-branch-test.git
  HEAD branch: master
  Remote branches:
    branch_01 new (next fetch will store in remotes/origin)
    branch_02 new (next fetch will store in remotes/origin)
    master    tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

特定のブランチをfetchして確認

branch_01だけfetchしてみる

$ git fetch origin branch_01:branch_01
そして確認
$ git branch -a
  branch_01
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch_01
  remotes/origin/master

$ git branch -vv
  branch_01 775d607 add memo from branch_01
* master    bcdc8ee [origin/master] Initial commit

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:tweeeety/github-branch-test.git
  Push  URL: git@github.com:tweeeety/github-branch-test.git
  HEAD branch: master
  Remote branches:
    branch_01 tracked
    branch_02 new (next fetch will store in remotes/origin)
    master    tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local refs configured for 'git push':
    branch_01 pushes to branch_01 (up to date)
    master    pushes to master    (up to date)

Remote branches:にはbranch_01がtrackedされて、
Local refs configured for 'git push':ではmasterしかなかったのが
branch_01についての情報が増えました。

そして注目したいのは下記の部分

 Local branch configured for 'git pull':
  master merges with remote master
 Local refs configured for 'git push':
  branch_01 pushes to branch_01 (up to date)
  master pushes to master (up to date)

これはbranch_01ブランチがpushの準備はできてるがpullの準備ができてないことを表してます

pullしてみる確認

ここでおもむろにpullしてみます。

$ git branch
* branch_01
  master

$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:tweeeety/github-branch-test
   775d607..4693c9b  branch_01  -> origin/branch_01
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> branch_01

hoge側で修正があったとしてもpullできないよって言われてます。
ヒントを教えてくれてますが、下記のどちらかを行うことで
pullについてリモートのどのブランチに対して行うか教えてあげる必要があります。

git branch --set-upstream-to=origin/ branch_01してみて確認

上記でヒントを出してくれたのでそのまま打ってみてから確認してみます。

$ git branch --set-upstream-to=origin/branch_01 branch_01
Branch branch_01 set up to track remote branch branch_01 from origin.
そして確認
$ git branch -a
* branch_01
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch_01
  remotes/origin/branch_02
  remotes/origin/master

※ オリジンと紐づいた
$ git branch -vv
* branch_01 775d607 [origin/branch_01: behind 1] add memo from branch_01
  master    bcdc8ee [origin/master] Initial commit

※ pullもremoteの branch_01と紐づいた
$ git remote show origin
* remote origin
  Fetch URL: git@github.com:tweeeety/github-branch-test.git
  Push  URL: git@github.com:tweeeety/github-branch-test.git
  HEAD branch: master
  Remote branches:
    branch_01 tracked
    branch_02 tracked
    master    tracked
  Local branches configured for 'git pull':
    branch_01 merges with remote branch_01
    master    merges with remote master
  Local refs configured for 'git push':
    branch_01 pushes to branch_01 (local out of date)
    master    pushes to master    (up to date)

※ これで正常にpullできる
$ git pull
Updating 775d607..4693c9b
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

まとめ

git系のコマンドはたくさんあって、特にブランチ操作系はいろいろみることが多いです。
こんな感じで流れで見て行くと覚えやすいですね\(^o^)/