【Git】リモートのブランチをローカルから削除する方法

はじめに

現状、リモートに不要なmasterブランチがある。

git branch -aで、ローカルとリモートのブランチが確認できる。

hiroki@shibatahiroshitakanoMacBook-Air python % git branch -a
* main
  remotes/origin/main
  remotes/origin/master

リモートのブランチをローカルから削除する方法

git push origin –delete masterで、リモートにある不要なmasterブランチを削除することができる。

リモート側でdefaultブランチとして設定されている場合

以下のエラーが起こる。

hiroki@shibatahiroshitakanoMacBook-Air python % git push origin --delete master
To https://github.com/ki-hi-ro/python.git
 ! [remote rejected] master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to 'https://github.com/ki-hi-ro/python.git'

原因は、リモートのgithubで、masterブランチがdefaultになっているため。

Settings > General > Default branch > Switch to another branchで、masterからmainに切り替える。

リモートでdefaultブランチでなければ削除可能

defaultブランチをmasterからmainに変更した後だったので、git push origin –delete masterで削除することができた。git branch -aで確認すると、remotes/origin/masterが削除されていたことが確認できた。

hiroki@shibatahiroshitakanoMacBook-Air python % git push origin --delete master
To https://github.com/ki-hi-ro/python.git
 - [deleted]         master
hiroki@shibatahiroshitakanoMacBook-Air python % git branch -a                  
* main
  remotes/origin/main

コメントを残す

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