2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitで毎回`git push --set-upstream origin master`を求められるのを防ぐ方法

Last updated at Posted at 2025-02-02

みなさんgit操作をコマンドで行っていますか?コマンド勢の方ならpush --set-upstreamを一度は見たことありませんか?.gitで新しいブランチを作成してgit pushを実行すると,次のようなメッセージが表示されることがある.

git push fatal: The current branch feature/sample has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin feature/sample

これは,現在のブランチに対応するリモートブランチが存在しないため,手動でgit push --set-upstream origin <branch>を実行してローカルブランチに対応するリモートブランチを設定するように促している.

一度git push --set-upstream origin <branch>をすれば,次回以降のgit pushは通常通り動作するが,新しいブランチを作成するたびに同じ手順を踏むのは面倒だ.そこで,この設定を自動化する方法を紹介する.

gitがわからない方は以下の記事を参照してください.

push.defaultの設定を確認する

Gitには,デフォルトのpush動作を制御するpush.defaultという設定がある.これを変更することで,git push --set-upstreamを手動で実行する必要がなくなる.

まず,現在の設定を確認する.

git config --global push.default

この状態はsimpleと呼ばれる状態で,リモート追跡ブランチが設定されていることを前提とし,リモートのブランチ名とローカルのブランチ名が異なる場合に警告を出す設定だ.

push.defaultの設定を変更する

push.defaultcurrentに設定する.

git config --global push.default current

この設定を行うことで,git pushを実行した際に現在のローカルブランチと同じ名前のブランチがリモートに存在すれば,そのブランチにpushする.もし存在しなければ,新しく作成してpushする.

push.defaultの設定値の種類

push.defaultにはいくつかのモードがある.それぞれの動作は次の通り.

  • nothing
    デフォルトではpushを行わず,明示的に指定されたブランチのみをpushする

  • current
    現在のローカルブランチをリモートブランチにpushする(リモートに存在しなければ作成する)

  • upstream
    現在のローカルブランチのupstream(追跡)ブランチが設定されていれば,そのブランチにpushする

  • simple(Git 2.0以降のデフォルト)
    upstreamに似ているが,現在のブランチ名とリモートのブランチ名が一致する場合のみpushを許可する

  • matching
    ローカルとリモートで同じ名前のブランチすべてをpushする

この中でも,currentが最も直感的で便利な設定だ.特に個人での開発では,新しいブランチを作成してそのままpushすることが多いため,git push --set-upstreamを省略できるのは大きな利点になる.

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?