トラブルシューティング
push 時に「no upstream branch」エラーが出る
初回 push で発生する upstream 未設定エラーの原因と解決方法
エラーの意味
ローカルブランチが、リモートのどのブランチに対応するか(upstream)が設定されていない状態で git push を実行すると、このエラーが発生します。
新しくブランチを作成した直後や、clone 後に新規ブランチを切った場合に起こります。
Git はどのリモートブランチに push すればいいかわからないため、明示的な指定を求めています。
bash
# このようなエラーが表示される
# fatal: The current branch feature/login has no upstream branch.
# To push the current branch and set the remote as upstream, use
#
# git push --set-upstream origin feature/login-u オプションで upstream を設定する
git push -u(--set-upstream の短縮形)を使うと、push と同時にリモートの対応ブランチを設定できます。
一度設定すれば、以降は git push だけで同じリモートブランチに push されます。
初回 push 時に必ずこのオプションをつける習慣にすると便利です。
bash
# upstream を設定しつつ push
git push -u origin feature/login
# 以降はこれだけで OK
git pushtracking ブランチの確認方法
どのブランチが upstream 設定済みかは git branch -vv で確認できます。
upstream が設定されているブランチには [origin/ブランチ名] の表示がつきます。
設定されていないブランチにはこの表示がありません。
bash
# tracking ブランチの確認
git branch -vv
# 出力例:
# main abc1234 [origin/main] Initial commit
# * feature/login def5678 Add login page ← upstream 未設定自動設定を有効にする
push.autoSetupRemote を true に設定すると、新しいブランチの初回 push 時に自動で upstream が設定されます。
Git 2.37 以降で利用可能です。
チーム全体で設定しておくと、このエラーに悩まされることがなくなります。
bash
# 自動設定を有効にする
git config --global push.autoSetupRemote true
# これで git push だけで初回 push も成功する
Git Ready