LoginSignup
54
55

More than 5 years have passed since last update.

【Git基礎】git push origin masterのmasterはローカル/リモートどっちのこと?

Last updated at Posted at 2016-04-27

git push origin master のコマンドにてmasterブランチはローカル側のやつかリモート側のやつかどっちことだかわからなかったので調査した。

はじめに結論

git push/fetch origin mastermasterソース元を意味している。
なので
git push origin mastermasterローカル側のmasterブランチを指していて、
git fetch origin mastermasterリモート側のmasterブランチを指している。

理屈

そもそも

git pushとかgit fetchのヘルプを見ると
git push/fetch [<repository> [<refspec>...]]
とある
つまりorigin<respository>にあたり、master<refspec>にあたる

<refspec>ってなんですか

git pushとかgit fetchとかのヘルプで<refspec>の部分を探すと共通して書いてあるのが、

The format of a <refspec> parameter is an optional plus +, followed by the source ref <src>, followed by a colon :, followed by the destination ref <dst>. The colon can be omitted when <dst> is empty.

<refspec>の形式は+オプション<src>:<dst>らしい。

これでわかるのが、<refspec>はソース元と提供先を示すためのもの(+オプションをつけることでfast-forwarded状態でなくても提供先のものを更新する)
でも普段は:を使って分けたりしないし(他の人はしてるのかな)、結局git push/fetch origin masterのmasterはソース元なのか提供先なのか。
git push、git fetchのヘルプを見てみると共通して書いてあるのが、

:<dst> part can be omitted

つまりgit push/fetch origin mastermasterでは:<dst>の部分を省略していた!
ので、master<src>意味していて、冒頭の結論ということになる。

補足(使い方など)

git push

プルリクエスト送信

The <src> is often the name of the branch you would want to push, but it can be any arbitrary

とあり、任意のブランチをリモート先にpushすることができ、リモート先のレポジトリに名前がない場合は作成される。これはGitHubではこれはコマンドライン上でプルリクエストを送ることを意味する。

リモートブランチ削除
git push origin :branch2

では<src>が空のものがリモート先の<dst>へプッシュするので、リモート先のbranch2ブランチをコマンドライン上で削除することを意味する。

git fetch

git fetchでもローカル側のリモートトラックに<src>が無い場合は、新しく作成される。

masterも省略するとき

masterも省略するとき、remote.<repository>.fetchという変数を<refspec>としてgitが参照する。
デフォルトでこの変数は以下の様になっている。(git config --listとかでも見れる)

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*

git fetchのヘルプによると

The example above will fetch all branches that exist in the origin (i.e. any ref that matches the left-hand side of the value, refs/heads/) and update the corresponding remote-tracking branches in the refs/remotes/origin/ hierarchy.

リモート上の全てのブランチ(refs/heads/*)分を、ローカルのリモートトラックブランチ(refs/remotes/origin/*)に対して更新するということ。
つまり

git fetch origin

とすると、ローカル上のリモートトラックブランチがすべて更新される

54
55
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
55