0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

gitのローカルブランチを整理したい時にみるTips

0
Posted at

Gitでmerge済みブランチをローカルから削除する手順

目的

GitHubでPull Requestがmerge済みになったブランチを、ローカル環境から削除して整理する。


1. 現在のブランチを確認

git branch

現在作業中のブランチを確認する。
削除対象のブランチにいる場合は、別のブランチへ移動する。


2. mainブランチへ移動

git checkout main

または:

git switch main

3. mainを最新化

リモートの変更を取得する。

git pull origin main

4. merge済みブランチを確認

ローカルでmerge済みのブランチを一覧表示する。

git branch --merged

例:

* main
  feature/1
  feature/2

main以外が削除候補。


5. ローカルブランチを削除

安全な削除

merge済みの場合のみ削除する。

git branch -d feature/1

強制削除

mergeされていないブランチも削除する場合。

git branch -D feature/1

※未mergeの変更も失われるため注意する。


6. GitHub側で削除済みブランチを整理

GitHubでPull Request merge後にブランチを削除した場合、ローカルにはリモート追跡ブランチが残ることがある。

確認:

git branch -r

不要なリモート情報を削除:

git fetch --prune

または:

git remote prune origin

日常的な運用フロー

Pull Requestをmergeした後:

# mainへ移動
git switch main

# 最新化
git pull origin main

# merge済みブランチ確認
git branch --merged

# ローカルブランチ削除
git branch -d feature/xxx

# リモート情報整理
git fetch --prune

ブランチ削除前の確認ポイント

  • Pull Requestがmerge済みである
  • 必要な変更がmainに反映されている
  • 削除対象のブランチで作業中ではない
  • 未pushの変更がない

推奨ブランチ運用

機能追加の場合:

main
 └── feature/1

作業完了後:

feature/inventory-list
        |
        | Pull Request
        ↓
      main

merge後:

main

のみを残し、不要なfeatureブランチは削除する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?