【Git】ローカルブランチとリモートブランチの対応関係を正しく整えるためのコマンド

はじめに

git branch --set-upstream-to=origin/main main

について解説していこう。

git branch –set-upstream-to=origin/main mainとは

ローカルのmainブランチが、リモートのorigin/mainブランチを追跡するように設定するという意味。

git branch --set-upstream-to=<リモート名>/<リモートブランチ> <ローカルブランチ>

現状

現状、以下のようになっている。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git remote show origin
* remote origin
  Fetch URL: https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
  Push  URL: https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
  HEAD branch: main
  Remote branches:
    main   tracked
    master tracked
  Local branch configured for 'git pull':
    main merges with remote master
  Local ref configured for 'git push':
    main pushes to main (fast-forwardable)

Local branch configured for ‘git pull’に、main merges with remote masterとあるように、git pullのときにローカルのmainブランチがリモートのmasterブランチと紐づいている。

git branch –set-upstream-to=origin/main mainを実行する

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git branch --set-upstream-to=origin/main main 
branch 'main' set up to track 'origin/main'.

git branch –set-upstream-to=origin/main mainを実行後のgit remote show originの結果

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git remote show origin                                       
* remote origin
  Fetch URL: https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
  Push  URL: https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
  HEAD branch: main
  Remote branches:
    main   tracked
    master tracked
  Local branch configured for 'git pull':
    main merges with remote main
  Local ref configured for 'git push':
    main pushes to main (fast-forwardable)

Local branch configured for ‘git pull’の設定が、main merges with remote mainになっていたので、OK。

git pushでローカルのmainブランチと紐づいているリモートのmainブランチにプッシュ可能

git push origin mainとしなくても、git pushのみでリモートのmainブランチにプッシュすることができた。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
   586e053..43ec9cb  main -> main

追跡ブランチのmasterを削除する

以下のようになっているが、masterブランチは不要なので、削除しよう。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git remote show origin                                       
* remote origin
  Fetch URL: https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
  Push  URL: https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
  HEAD branch: main
  Remote branches:
    main   tracked
    master tracked
  Local branch configured for 'git pull':
    main merges with remote main
  Local ref configured for 'git push':
    main pushes to main (fast-forwardable)

削除完了。「ローカルとリモートのブランチを表示→リモートのmasterブランチを削除→リモートの追跡状況の確認→ローカルとリモートのブランチを再表示」という流れで進めた。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です