Git:ブランチの削除(ローカル/リモート)
タダノメモ。ブランチって増えていくのがGitの宿命。増えたものは整理(削除)しよう。
目次
本文
1. ローカルブランチの削除
ローカルブランチの削除は以下のコマンドで行える。
git branch -d もしくは-D branch_name
-dと-Dの違いはリファレンス参照。
-d
--delete
Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with --track or --set-upstream-to.-D
Shortcut for --delete --force.
2. リモートブランチの削除
2.1 リモートブランチの削除方法
リモートリポジトリを削除するコマンドは下記の通り。
git push --delete origin new_branch
実行例は下記の通り。
MINGW64 ~/Git/sandbox (develop) $ git push --delete origin f/#01 <- 削除(1) To https://git-hosting.service/xxx/sandbox.git - [deleted] f/#01 MINGW64 ~/Git/sandbox (develop) $ git push --delete origin f/#02 <- 削除(2) To https://git-hosting.service/xxx/sandbox.git - [deleted] f/#02 MINGW64 ~/Git/sandbox (develop) $ git push --delete origin f/#03 <- 削除(3) To https://git-hosting.service/xxx/sandbox.git - [deleted] f/#03 MINGW64 ~/Git/sandbox (develop) $ git branch -a * develop master remotes/origin/HEAD -> origin/master remotes/origin/develop remotes/origin/master
2.2 削除の同期(for チームメンバー)
リモートブランチを削除しても、他のメンバーの環境で追跡ブランチの表示が残り続ける。その場合は、以下のコマンドで【削除の同期】をしてもらう。
> git fetch -p MINGW64 ~/Git/sand2/sandbox (develop) $ git fetch -p From https://git-hosting.service/xxx/sandbox - [deleted] (none) -> origin/f/#01 - [deleted] (none) -> origin/f/#02 - [deleted] (none) -> origin/f/#03