Gitでブランチの名前を変える方法と消す方法
ブランチ名の打ち間違いを直すときは git branch -m、マージし終わって要らなくなったブランチを片付けるときは git branch -d を使います。-d はマージしていないブランチだと削除を止めますが、-D はマージしたかどうかに関係なく消します。プッシュ済みのブランチは、手元で名前を変えたり消したりしてもリモートには伝わらないので、別に git push が要ります。
検証環境は Ubuntu 24.04、Git 2.43.0 です。作者の名前とメールアドレスには例示用の値(Neko <neko@example.com>)を設定し、出力を毎回同じにするため、コミットの日時を環境変数で固定しました。作業用のフォルダーは /path/to という名前で作ったので、出力に出るパスもこの値です。リモートは同じ環境に作ったベアリポジトリ(../remote.git)です。GitHub などのホスティングサービスでの操作(既定のブランチの変更など)と、Windows・macOS は試していません。
試したリポジトリ
main のほかに、3つのブランチがあるリポジトリです。git branch -vv を使うと、ブランチごとの最新のコミットが出て、追いかけているリモートのブランチ(上流のブランチ)は角かっこの中に出ます(git branch の公式リファレンス)。
$ git branch -vv
experiment ebb517a キャッシュを試す
feature/lgoin a5cc578 [origin/feature/lgoin] ログイン画面を追加
fix-typo 72ea9ab READMEの誤字を直す
* main aaab4fc [origin/main] fix-typo をマージ
feature/lgoin は、login を打ち間違えた名前のままプッシュしたブランチです。fix-typo は main にマージ済みで、experiment は試しに作っただけでマージしていません。
git branch -m で名前を変える
ほかのブランチの名前を変える
git branch -m 今の名前 新しい名前 で変えます。main にいたまま、feature/lgoin を feature/login に変えました。成功しても何も出ません。
$ git branch -m feature/lgoin feature/login
$ git branch -vv
experiment ebb517a キャッシュを試す
feature/login a5cc578 [origin/feature/lgoin] ログイン画面を追加
fix-typo 72ea9ab READMEの誤字を直す
* main aaab4fc [origin/main] fix-typo をマージ
手元のブランチの名前は変わりましたが、上流のブランチは origin/feature/lgoin のままです。公式リファレンスには、-m はブランチの設定と reflog も一緒に移すと書かれています。上流のブランチの設定も、古い名前のまま新しいブランチに移っていました。
$ git config --get-regexp "^branch\\.feature"
branch.feature/login.remote origin
branch.feature/login.merge refs/heads/feature/lgoin
リモートのブランチの名前もそろえる
リモートのブランチの名前は、新しい名前でプッシュしてから、古い名前のブランチをリモートから消すことでそろえます(git push の公式リファレンス)。-u を付けてプッシュすると、上流のブランチも新しい名前に付け替わります。
$ git push -u origin feature/login
To ../remote.git
* [new branch] feature/login -> feature/login
branch 'feature/login' set up to track 'origin/feature/login'.
$ git push origin --delete feature/lgoin
To ../remote.git
- [deleted] feature/lgoin
$ git branch -vv
experiment ebb517a キャッシュを試す
feature/login a5cc578 [origin/feature/login] ログイン画面を追加
fix-typo 72ea9ab READMEの誤字を直す
* main aaab4fc [origin/main] fix-typo をマージ
$ git ls-remote --heads origin
a5cc578bae648ee74cc7f9aa1d735203cb8fce69 refs/heads/feature/login
aaab4fc73e6d34b88b0e473c85bb4733ea697786 refs/heads/main
リモートにあるブランチも、feature/login と main の2つになりました。
いまいるブランチの名前を変える
今の名前を省いて git branch -m 新しい名前 とすると、いまいるブランチの名前が変わります。experiment に移ってから、try-cache に変えました。
$ git switch -q experiment
$ git branch
* experiment
feature/login
fix-typo
main
$ git branch -m try-cache
$ git branch
feature/login
fix-typo
main
* try-cache
$ git status | head -1
On branch try-cache
すでにある名前には変えられない
ほかのブランチと同じ名前に変えようとすると、エラーで止まります。
$ git branch -m main
fatal: a branch named 'main' already exists
すでにある名前に変えたいときに使う -M は、記事の最後で試しています。
git branch -d で消す
マージ済みのブランチを消す
先に git branch --merged で、いまのブランチ(HEAD)にマージ済みのブランチを確かめておくと安心です。--no-merged を付けると、まだマージしていないブランチが出ます。
$ git switch -q main
$ git branch --merged
fix-typo
* main
$ git branch --no-merged
feature/login
try-cache
$ git branch -d fix-typo
Deleted branch fix-typo (was 72ea9ab).
fix-typo は消えました。消したブランチが最後に指していたコミットが、was の後ろに出ます。
マージしていないブランチは -d で消せない
main にマージしていない try-cache を -d で消そうとすると、止められます。本当に消すなら -D を使うよう、エラーの文に書かれています。
$ git branch -d try-cache
error: the branch 'try-cache' is not fully merged.
If you are sure you want to delete it, run 'git branch -D try-cache'
$ git branch -D try-cache
Deleted branch try-cache (was ebb517a).
$ git branch
feature/login
* main
公式リファレンスでは、-D は --delete --force の短い書き方で、マージしたかどうかに関係なく消すと説明されています。
間違えて消したブランチを作り直す
-D で消したあとでも、was の後ろに出たハッシュを渡せば、同じコミットを指すブランチを作り直せます。
$ git branch try-cache ebb517a
$ git log --oneline -1 try-cache
ebb517a キャッシュを試す
公式リファレンスによると、ブランチを消すと、そのブランチの reflog も一緒に消えます。消したブランチのハッシュは、-D を実行したときの was の後ろに出ています。
いまいるブランチは消せない
いまいるブランチは、-D でも消せません。
$ git switch -q try-cache
$ git branch -D try-cache
error: cannot delete branch 'try-cache' used by worktree at '/path/to/app'
$ git switch -q main
$ git branch -D try-cache
Deleted branch try-cache (was ebb517a).
ほかのブランチに移ってからなら消せました。
上流のブランチがあると、-d の判断が変わる
-d がマージ済みかどうかを確かめる相手は、いつも HEAD とは限りません。公式リファレンスには、上流のブランチがあればそのブランチに、なければ HEAD にマージ済みであることが条件だと書かれています。
feature/login は main にマージしていませんが、上流の origin/feature/login にはプッシュ済みです。main にいるまま -d で消すと、警告は出たものの、消えました。
$ git branch --no-merged
feature/login
$ git branch -d feature/login
warning: deleting branch 'feature/login' that has been merged to
'refs/remotes/origin/feature/login', but not yet merged to HEAD
Deleted branch feature/login (was a5cc578).
コミットはリモートに残っているので、git switch でブランチの名前を指定すると、origin/feature/login から手元のブランチが作り直されました。
$ git switch feature/login
Switched to a new branch 'feature/login'
branch 'feature/login' set up to track 'origin/feature/login'.
$ git switch -q main
リモートのブランチを消す
リモートのブランチは git push origin --delete ブランチ名 で消します。消したあとの git branch -vv では、手元の feature/login の上流に gone が付きました。
$ git push origin --delete feature/login
To ../remote.git
- [deleted] feature/login
$ git branch -r
origin/main
$ git branch -vv
feature/login a5cc578 [origin/feature/login: gone] ログイン画面を追加
* main aaab4fc [origin/main] fix-typo をマージ
手元のブランチは、リモートのブランチを消しても残ります。上流のブランチがなくなったあとは、-d で止められました。main にマージしていないコミットがあるためです。要らないブランチなら、-D で消せます。
$ git branch -d feature/login
error: the branch 'feature/login' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/login'
$ git branch -D feature/login
Deleted branch feature/login (was a5cc578).
ほかのクローンには git fetch –prune で伝える
リモートのブランチを消す前に、別のフォルダー(../clone)にもクローンしておきました。そちらでは、ふつうの git fetch のあとも origin/feature/login が残りました。
$ git -C ../clone fetch
$ git -C ../clone branch -r
origin/HEAD -> origin/main
origin/feature/login
origin/main
--prune を付けると、リモートにもうないブランチを指す origin/… のブランチ(リモート追跡ブランチ)が、手元からも消えます(git fetch の公式リファレンス)。
$ git -C ../clone fetch --prune
From /path/to/app/../remote
- [deleted] (none) -> origin/feature/login
$ git -C ../clone branch -r
origin/HEAD -> origin/main
origin/main
-M で、すでにある名前のブランチを上書きする
公式リファレンスによると、-M(-m --force と同じ)を使うと、新しい名前のブランチがすでにあっても名前を変えます。ほかのブランチを片付けたあとで、main と同じ aaab4fc を指す keep と、ebb517a を指す other を新しく作って試しました。
$ git branch -v
keep aaab4fc fix-typo をマージ
* main aaab4fc fix-typo をマージ
other ebb517a キャッシュを試す
$ git branch -m other keep
fatal: a branch named 'keep' already exists
$ git branch -M other keep
$ git branch -v
keep ebb517a キャッシュを試す
* main aaab4fc fix-typo をマージ
-M で既にある名前に変えると、もとの keep は確認なしに消え、keep は other が指していた ebb517a を指すようになりました。





