LoginSignup
356
316

More than 5 years have passed since last update.

gitのpush.defaultに関するノウハウ

Last updated at Posted at 2013-10-30

gitのpush.defaultの設定に関して、他人のを設定してあげることもあり、毎回迷うのでまとめました。

push.defaultは、今まではmatchingというのがデフォルトでしたが、Git2.0からsimpleっていうのがデフォルトになります。なので、何らかの設定をしないと、以下の様な警告が出たりします。

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

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

ちなみに、設定できるオプションの詳しい説明についてはgit help configに書いてあります。

Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

  • nothing - do not push anything.

  • matching - push all branches having the same name in both ends. This is for those who prepare all the branches into a publishable shape and then push them out with a single command. It is not appropriate for pushing into a repository shared by multiple users, since locally stalled branches will attempt a non-fast forward push if other users updated the branch. + This is currently the default, but Git 2.0 will change the default to simple.

  • upstream - push the current branch to its upstream branch. With this, git push will update the same remote ref as the one which is merged by git pull, making push and pull symmetrical. See "branch.<name>.merge" for how to configure the upstream branch.

  • simple - like upstream, but refuses to push if the upstream branch's name is different from the local one. This is the safest option and is well-suited for beginners. It will become the default in Git 2.0.

  • current - push the current branch to a branch of the same name.

The simple, current and upstream modes are for those who want to push out a single branch after finishing work, even when the other branches are not yet ready to be pushed out. If you are working with other people to push into the same shared repository, you would want to use one of these.

性格別おすすめpush.default設定

ただ訳してもつまらないので、どういう人が使うべきか解釈してみました。

nothing

何もしない。git pushなんて省略形使わないで常にgit push origin masterみたいに指定するぜ!という堅実なあなた向き。

matching

今までのデフォルト。ローカルとリモートで同一の名前のリポジトリがあれば全てpushする。gitがgithubのような使い方じゃなくてP2Pのように完全分散型バージョン管理を目指した頃の、古き良き時代を愛すあなた向き。

upstream

現在のブランチにupstreamが設定されている場合、そこにpushする。これを指定するとgit pushが完全にgit pullと逆の意味を表すようになる。規律や規則性を大事にするあなた向き。

simple

これからのデフォルト。upstreamが設定されていて、それが同名のブランチ名であるときのみpushする。初心者でも安心して使える。初心を忘れないあなたと、デフォルトを愛するあなた向き。

current

現在のブランチをリモートに同名でpushする。例えupstreamを設定していなくてもpushされるので、めんどくさがりで効率重視なあなた向き。

まとめ

結局どれ使えばいいかわからないという人は、次世代のデフォルトのsimpleか、currentを使うといいと思います。currentはかなり楽なので、僕はこれを使っています。

おまけ1: upstreamの設定

上の記述に何回か登場したupstreamは以下の2つの方法で設定できます。

pushするときについでに設定

git pushする際に-uオプションを付けると、push時に同時に設定できます。

git push -u origin master

明示的に設定

今いるブランチのupstreamを設定したい時:

git branch -u origin/current-branch-name

別ブランチの設定したい時:

git branch -u origin/different-branch-name different-branch-name

 

なお、設定の確認は

git branch -vv

で出来ます。

おまけ2: git push のおさらい

git pushは略さないで書くと以下のように指定することになっています。

git push <リモートのレポジトリ名> <ローカルのブランチ名>:<リモートのブランチ名>
  • <リモートのレポジトリ名>: git remote/git remote -vで確認可能。
  • <ローカルのブランチ名>: git branchで確認可能。
  • <リモートのブランチ名>: git branch -rで確認可能。

 
例えば、ステージング用のHerokuheroku_stagingに、新たに機能を追加したブランチexperimentをデプロイしたい時は以下のようにします。
(Herokuはmasterにpushすればデプロイされます)

git push heroku_staging experiment:master

 
リモートのブランチ名がローカルと同じ場合は省略できます。例えば、よく使う

git push origin master

は、以下のように指定したのと同じ意味になります。

git push origin master:master

 
さらにもっと省略して、

  • リモートリポジトリ名だけを指定
git push origin
  • 全て省略
git push

と記述しても、何らかの操作を行うことが可能で、その挙動を指定するのが今回のpush.defaultの設定です。

おまけ3: git pushの挙動を実験したい

いろいろ実験して確認したい人は、git push-n(--dry-run)オプションを付けることで、何が起こるかだけを出力することができます。
お試しあれ。

356
316
2

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
356
316