Qiitaとかでよくある説明。
simple
upstreamが設定されていて、それが同名のブランチ名であるときのみpushする。
$ git remote add origin git@github.com:unarist/test.git
$ git push origin
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
これは期待通り。
$ git remote add hoge git@github.com:unarist/test.git
$ git push hoge
Counting objects: 3, done.
Writing objects: 100% (3/3), 197 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:unarist/test.git
* [new branch] master -> master
え?upstream設定してないのに、なんでrefspecなしでpushできてるの?
要約
- ブランチのupstreamとpush先が違うremoteの時、
push.default=current
と同じになる- つまり「カレントブランチと同名のブランチにpushする」
- ブランチのupstreamが設定されていない場合は
origin
- ただしupstreamが無効なremoteの場合は
push.default=upstream
互換のまま- 例えば、ブランチのupstreamが設定されておらず、
origin
以外のremoteしか作ってない時。
- 例えば、ブランチのupstreamが設定されておらず、
むかーしむかし、あるところに
Triangular workflow というのがあります。「普段はupstreamからpullしたいけど、pushは別のところにしたい」というパターン。Gitにはこれをサポートする機能として、remote.pushdefault
branch.<name>.pushremote
という設定があり、例えばブランチのupstreamがrepoAになっていても(オプションなしで)repoBに対してpushする、ということができます。
これらの設定はあくまでデフォルトのpush先を指定するものであり、「push時はそれがupstreamになる」というわけではありません。git push <repo>
相当ですね。
push.default=upstream
は「refspecなしのpushは常にupstreamに対して行われるべき」という設定なので、Triangular workflowとは相性が悪いのですが、これまでは push.default=matching
がデフォルトでしたし、マニュアルにも一応注意書きがあります。
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).
さて、最近になって push.default=simple
が現れ、勧められるようになりました。今の最新版ではこれがデフォルト設定です。そうなると、Triangular workflow をしていた人は(これまでデフォルト設定で動いていたのに)設定を変えないといけません。
「面倒だな。よし、Triangular workflow っぽい時は push.default=simple
からupstreamの縛りを除いた、push.default=current
をデフォルトにしよう」
push: change simple
to accommodate triangular workflows · git/git@ed2b182
その基準が「upstreamとpush先のrepo(remote)が異なる時」なので、 git push piyo
としても push.default=current
相当になるというわけです。めでたしめでたし。
おまけ:simple vs current
matching
よりマシってぐらいの理由で simple
を使ってましたが、 current
もよさそうですね。普段同じremoteにpushすることが多いなら、 upstream設定してなくても git push
が使えて便利な気もします。origin
以外をデフォルトにしたいなら remote.pushdefault
設定すればいいですし。
逆にこういう場合は current
はやめたほうがよさそうです。
- ローカルとは違う名前のブランチにpushしたい(することがある)
- upstream設定していなければ叱ってほしい
-
git push <not-upstream-repo>
したら叱ってほしい(これはsimple
でもだめですが)