LoginSignup
12
8

More than 3 years have passed since last update.

【Git】git push 〜引数を省略した時の挙動〜

Last updated at Posted at 2020-12-27

はじめに

僕は、いつもヒヤヒヤしながらgit pushを実行しています。
なぜかというと、git pushは、設定によって挙動が違うからです。
そんな、私の寿命を1秒でも伸ばすために、git pushのオプションの挙動をまとめます。

引数を省略しない(正式なフォーマット)

$ git push origin main

第1引数(origin等) ・・・ リポジトリ名
第2引数(main等) ・・・ ブランチ名

origin(リモートリポジトリ)のmainブランチにローカルブランチがプッシュされます。いちいちorigin masterって打つの面倒くさいですよね〜


※ここは完全に余談

originとは ・・・ リモートリポジトリのURL

$ git remote -v // リモートリポジトリのURLを表示

origin  https://github.com/westhouseK/git_demo.git (fetch)
origin  https://github.com/westhouseK/git_demo.git (push)

もし何も表示されない方は、以下でリモートリポジトリのURLを登録できます。

【SSH登録している人】
$ git remote add origin git@github.com:westhouseK/git_demo.git

【SSH登録していない人】
$ git remote add origin https://github.com/westhouseK/git_demo.git

【本題】 引数を省略する

$ git push

種類

push.default 詳細
nothing 何もプッシュしない
current 現在のブランチを同名リモートブランチとしてプッシュする
upstream 現在のブランチの上流ブランチにプッシュする
simple 現在のブランチを同名の上流ブランチにプッシュする
matching すべてのローカルブランチを同名のブランチにプッシュする

上流ブランチ ・・・ リモートの履歴を追跡するブランチ

Gitのバージョン1.x系ではmatchingが、バージョン2.x系ではsimpleがデフォルトになっています。

現在の設定を確認するには・・・

$ git config --global push.default

設定するためには・・・

$ git config --global push.default simple

★ nothing

$ git push
fatal: You didn't specify any refspecs to push, and push.default is "nothing".

「引数をしていないから、プッシュできない」って怒られますね。ある意味、一番安全ですが、面倒くさいですね。

★ current

$ git push
To github.com:westhouseK/git_demo.git
 * [new branch] current_demo -> current_demo

$ git branch -vv
* current_demo 368c172 first commit
  main         368c172 first commit

現在と同名のブランチをプッシュするようです。
ちなみに、同名のブランチがリモートにあると、ちゃんと怒られるみたい。

error: failed to push some refs to 'git@github.com:westhouseK/git_demo.git'

★ upstream

$ git push
fatal: The current branch upstream_demo has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin upstream_demo


$ git push --set-upstream origin upstream_demo
To github.com:westhouseK/git_demo.git
 * [new branch]      upstream_demo -> upstream_demo
Branch 'upstream_demo' set up to track remote branch 'upstream_demo' from 'origin'.

$ git branch -vv
  main          368c172 first commit
* upstream_demo 368c172 [origin/upstream_demo] first commit
                         ↑上流ブランチ

上流ブランチが設定されていないと、プッシュすることができません。上記のように、プッシュの時に上流ブランチをセットするか、個別に上流ブランチをセットする必要があります。変なことはしなさそうな挙動ですね。

★ simple

【上流ブランチが設定されていない場合】
$ git push
fatal: The current branch simple_demo has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin simple_demo


【設定されている上流ブランチ名と現在のブランチ名が異なる場合】
$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:dev

To push to the branch of the same name on the remote, use

    git push origin HEAD

上流ブランチがセットされていない、上流ブランチがローカルブランチと違うと、プッシュできない模様。Git 2.x系のデフォルトになっているだけあって、安心といえば安心。ただ、simpleではない笑。

★ matching

$ git push
To github.com:westhouseK/git_demo.git
   368c172..3cf3d12  test1 -> test1
   368c172..2b3f77e  test2 -> test2

ローカルにあるすべてのブランチをプッシュします。test1だけをプッシュしたつもりが、test2もプッシュされます。怖すぎです。
ただ、リモートにブランチがない時、以下のようにプッシュされないようです。

$ git push
Everything up-to-date

まとめ

  • Gitのバージョンによって、挙動が違うので注意
  • Git 1.x系ではmatching、2.x系ではsimpleがデフォルト
  • ローカルブランチをリモートと同じ名前で作っていれば、事故らなそう
  • matchingには、しない方が無難!
  • ちなみに、私はcurrent

これで僕も長生きができそうです。間違っていること書いてあったら、言ってください。人の寿命は削りたくないので😥

参考サイト

公式

参考

https://gotohayato.com/content/116/
https://www-creators.com/archives/4931
https://www.yunabe.jp/docs/relearning_git_push_default.html
https://qiita.com/tamata78/items/09cc89ed87f022668d80

12
8
0

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
12
8