ブランチ(名)を変更・削除する方法を解説します。
ブランチ名を変更する方法(git branch -m)
以下のコマンドを実行し、ブランチを一覧表示します。
git branch
以下のコマンドを実行し、ブランチ名を変更したいブランチに移動します。
git checkout ブランチ名
![](https://ootkblog.com/wp-content/uploads/2024/03/git-branch-m-d-1.png)
以下のコマンドを実行し、ブランチ名を変更します。
git branch -m ブランチ名
![](https://ootkblog.com/wp-content/uploads/2024/03/git-branch-m-d-2.png)
以下のコマンドを実行し、ブランチを一覧表示します。
git branch
![](https://ootkblog.com/wp-content/uploads/2024/03/git-branch-m-d-3.png)
ブランチ名が「test」→「new_test」に変更されたことを確認できました。
ブランチを削除する方法(git branch -d)
以下のコマンドを実行し、ブランチを削除します。
git branch -d ブランチ名
![](https://ootkblog.com/wp-content/uploads/2024/03/git-branch-m-d-4.png)
ブランチを削除できない場合(1):削除するブランチにいる
削除するブランチにいると、ブランチを削除できません。
![](https://ootkblog.com/wp-content/uploads/2024/03/git-branch-m-d-5.png)
git checkoutでほかのブランチに移動しましょう。
ブランチを削除できない場合(2):マージされていない変更がある
マージされていない変更がある場合は削除できません。
![](https://ootkblog.com/wp-content/uploads/2024/03/git-branch-m-d-6.png)
強制削除する場合は「-d」でなく「-D」を使います。
git branch -D ブランチ名
![](https://ootkblog.com/wp-content/uploads/2024/03/git-branch-m-d-7.png)