Just do IT

思うは招く

git push.default の意味と設定方法

  • 安全にGitHubへpushしたい
  • 開発チームに迷惑をかけたくない
  • 怒られんの絶対イヤ
  • 理解してないまま使うの怖い

といった思いで、gitのpush.defaultについて設定を必死で調べた。

GitやGitHub関連は本当に気をつけないと、ひとつのコマンドでおじゃんになってしまうこともあるという怖い話を読んだので…

環境(執筆時)

$ git --version
git version 2.20.1 (Apple Git-117)

結論

  • push.default はgit pushを引数なしで実行したときの挙動の設定
  • Git 2.0 以降はもっとも安全な設定になっているので初心者は変えないほうがいい

push.default ってなんだ?

git pushと、引数をつけずにコマンドを実行したときの設定のこと。

つまり、単純にgit pushをしても、設定によっては動作が変わるということになる。なにそれ怖い。

自分の設定を見るには、

$ git config -l

をして、

push.default=simple

などを見つける。

push.default の設定種類

以下のコマンドで設定についてのヘルプを表示できる。

git help config

たくさんの説明が表示されるが、アルファベット順で並んでいるため、「push.default」の文字を見つけたらOK。

こんな表記になっている。

push.default

~省略~

Possible values are:

nothing - 

do not push anything (error out) unless a refspec is explicitly given. This is primarily meant for people who want to avoid mistakes by always being explicit.

current -

push the current branch to update a branch with the same name on the receiving end. Works in
both central and non-central workflows.

upstream - 

push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).

simple -

in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch's name is different from the local one.

When pushing to a remote that is different from the remote you normally pull from, work as current.
This is the safest option and is suited for beginners.This mode has become the default in Git 2.0.

matching - 

push all branches having the same name on both ends. This makes the repository you are pushing to remember the set of branches that will be pushed out (e.g. if you always push maint and master there and no other branches, the repository you push to will have these two branches, and your local maint and master will be pushed there).

設定の種類は5つに分けられる。

nothing

git pushをしても何もしない。 ちゃんとgit push origin ブランチ名などとしてpushしないと動作しない。 ミスを100%回避したい人向けの設定。

current

現在いるブランチと同じ名前のブランチ(リモートリポジトリ)に対してpushする。

たとえば、今いるブランチ名がhogeで、git pushと打つと自動でリモートリポジトリのhogeブランチにpushする。

せっかちな人、慣れている人、効率を極めたい人向け。

upstream

自分が一度upstreamとして設定したブランチに対してpushする。 最初は少し面倒だが、あとで楽になれる。

なおupstreamを設定する方法としては、upstreamを設定したいローカルブランチへチェックアウトし、--set-upstreamオプションをつける。

git push --set-upstream origin ブランチ名

# 例えばhogeブランチをpushしてupstreamに設定したいなら
git push --set-upstream origin hoge

具体的には、上記を実行するとGitHub側にhogeブランチが作成され、ローカルのhogeブランチと連携する。以降はgit pushをするだけでhogeブランチに変更を送ることができる。

simple

Git 2.0からのデフォルト設定がsimple。 説明文にもあるとおり、初心者にもっとも安全な方法。

upstreamと少し似ていて、upstreamを設定したブランチに対してpushする。 違いとしては、simpleの場合、upstreamを設定していて、かつローカルブランチとリモートブランチの名前が同じ場合にのみpushする。

upstreamを設定していない場合にgit pushをしても、「ちょ待て、upstreamブランチしてないぞ!」と注意されてpushできない仕様になっている。素晴らしい!

matching

Git 2.0以前までのデフォルト。

ローカルブランチとリモートブランチの名前が同じだった場合、すべてpushする。 すこし怖いかも。

結局どれを使えばいいのか?

Gitはやらかしたら本当に大変なことになるので、Git公式が「もっとも安全」といっているように、simpleにしておくといい。 Git 2.0 以降はデフォルト設定になっているので、何も変える必要はない。

push.default の設定方法

git config --global push.default 設定名

# 例
git config --global push.default simple

変更したい場合は、simpleの部分をcurrentなど他の設定名にすればOK。

確認方法

$ git config -l

upstream を設定する方法

オーソドックスな方法

まず上記で紹介した方法を復習。

git push --set-upstream origin ブランチ名

# 例えばhogeブランチをpushしてupstreamに設定したいなら
git push --set-upstream origin hoge

シンプルにこうも書ける。

git push -u origin hoge

ローカルブランチ作成時にupstreamを設定

git checkout -t origin hoge

git checkout --track origin hoge

git pushをしてwarning: push.default is unsetの警告が出た場合

Git 2.0 以降を使っていれば以下の警告は出ないはずだが、念の為の回避法。

warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

単純に「push.defaultが設定されてないから、設定したほうがいいんじゃない?」と言われている。

よって、自分で好きな設定をすればいい。

git config --global push.default simple

関連

参照