LoginSignup
20

More than 5 years have passed since last update.

git push引数省略時のデフォルト動作設定

Last updated at Posted at 2015-12-24

先日、vimの設定ファイルをgithubに上げるべく
コミットした内容をおもむろに以下のコマンドでpushしました。

$ git push

するとなにやら怪しげなメッセージが。

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

  git config --global push.default matching

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

  git config --global push.default simple
・
・
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

Git2.0からpush.defaultの設定がmatchingからsimpleが変わりました。今までの振る舞いでいきたいならmatching。新しい振る舞いで行きたいならsimpleと設定してね。

push.defaultってなんですか。そしてgit pushの引数省略時って
何をどこにpushしているんだろう。。ちゃんと理解していなかった。

git pushの引数省略時にどのように振る舞うのかの設定がpush.defaultだそうです。
その設定値について整理してみたいと思います。

push.defaultの設定

設定値 説明
nothing デフォルト設定なし。push先とpush対象ブランチを必ず指定しなければならない
matching リモートブランチと同名のローカルのブランチをすべてpush(カレントブランチ以外も)
upstream カレントブランチへ追跡ブランチ設定されている場合に追跡ブランチにpush
simple 現行バージョンのデフォルト。カレントブランチへ追跡ブランチ設定されているかつ、ローカルとリモートのブランチ名が同じである場合に、追跡ブランチに対して push
current カレントブランチと同名のリモートブランチがある場合、そこへpush

Git2.0のデフォルト設定であるsimpleが一番安全。

ただし、追跡ブランチを設定しておかないと本記事冒頭のsimpleモードの場合、追跡ブランチを設定してくださいメッセージが出力される。

追跡ブランチとは?

リモートブランチと直接のつながりを持つローカルブランチのこと。

追跡ブランチを設定すると何が嬉しいのか?

  • push.defaultの'upstream'、'simple'においてpushできる
  • git statusしたときにahead, behind(push先からコミットが進んでいるか、後退しているか)を数えてくれる
  • git push で引数を省略できる

追跡ブランチが作成されるタイミング

  • リモートブランチからローカルブランチにチェックアウト
  • リポジトリをクローン時

具体的には以下

# ローカルブランチ作成時(リモートブランチからローカルブランチにチェックアウト)
$ git branch -b [branch]

# リポジトリをクローン時
$ git clone path/to/shared_repo.git work

# リモートブランチ作成時 
$ git push -u origin hogehoge

# 既に存在するブランチに設定(カレントブランチに設定する)
$ git branch -u origin/hogehoge

# 既に存在するブランチに設定(カレントブランチ以外に設定する)
$ git branch -u origin/hogehoge hogehoge

まとめ

  • git push引数省略時のデフォルト設定は push.defaultで決まる。
  • git 2.0ではsimpleがデフォルト設定。
  • simpleは同名ブランチが存在し、かつ追跡ブランチがある場合にpushできる。安全!
  • 追跡ブランチはローカルブランチをリモートブランチに紐づけるローカルブランチ

参考URL

git の push.default 設定を理解する
git push -u オプションの意味
3.5 Git のブランチ機能 - リモートブランチ

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
20