0
1

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でブランチ削除 & Untrackedファイル削除のまとめ

Posted at

Gitを使っていると、不要になったブランチや未追跡ファイル(Untracked files)を整理したくなることがあります。この記事では、以下の3点について簡潔にまとめます。

  • ローカルブランチの削除方法
  • リモートブランチの削除方法
  • Untrackedファイルの削除方法

🧹 1. ローカルブランチを削除する

通常削除(マージ済みのブランチのみ)

git branch -d branch_name

強制削除(マージされていないブランチも削除)

git branch -D branch_name

🔄 事前にチェックアウトしておこう
削除対象のブランチに現在いると削除できません。先に main など他のブランチに切り替えてから実行します。

git checkout main

🌐 2. リモートブランチを削除する

🚀 コマンド

git push origin --delete branch_name
✅ **例:**
```bash
git push origin --delete feature/xyz
📌 **注意点:**
- リモートに削除の権限(push権限)が必要です。
- チームで共有している場合、削除前に確認を取りましょう。
- 削除後は、以下でローカルのリモート追跡情報も整理できます:
```bash
git fetch --prune

🗑️ 3. Untrackedファイル(未追跡ファイル)を削除する

🎯 個別に削除する

rm filename

🧼 Gitのクリーンアップコマンドで削除する
すべての未追跡ファイルを削除(強制)

git clean -f

対話モードで削除対象を選ぶ

git clean -i

特定のファイルだけ削除する

git clean -f path/to/untracked_file

⚠️ 注意:
git clean は復元できません。削除前に git status で確認しましょう。

-n オプションを使えば、何が削除されるか事前に確認できます:

git clean -n

📚 まとめ

操作内容 コマンド例
ローカルブランチ削除(安全) git branch -d branch_name
ローカルブランチ削除(強制) git branch -D branch_name
リモートブランチ削除 git push origin --delete branch_name
未追跡ファイルの確認 git status
未追跡ファイルの削除 git clean -f or rm filename

✅ 最後に

ブランチやファイルの整理は、プロジェクトの健全性を保つ上で重要です。削除操作は慎重に、必要ならバックアップを取ってから実行しましょう!

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?