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】ブランチ名を間違えて作成したときの正しい対処手順

0
Posted at

想定シーン

git switch -c post-store のように
ブランチ名を間違えて作成してしまった

・まだ開発は始めていない/消して作り直したい

結論

ブランチを削除して、正しい名前で作り直す のが最も安全。

手順①:別のブランチに移動する(必須)

削除対象のブランチ上では削除できないため、
まず main など別のブランチへ移動する。

git switch main

※ develop などでも可

手順②:間違えて作成したブランチを削除する

通常削除(おすすめ)

git branch -d post-store

・マージ済み or 問題ない場合はこれで削除される

・Gitが安全チェックを行ってくれる

削除できない場合(未マージ時)

以下のようなエラーが出る場合:
error: The branch 'post-store' is not fully merged.

その場合は 強制削除 を行う。

git branch -D post-store

未コミット・未マージの変更は完全に削除される
(作った直後なら問題なし)

手順③:ブランチが削除されたか確認

git branch

一覧に post-store が表示されていなければOK。

(補足)リモートに push 済みだった場合

もし誤ったブランチを すでに push していた場合 は、
リモートブランチも削除する。

git push origin --delete post-store

手順④:正しいブランチ名で作り直す

例:投稿の保存処理の場合

git switch -c feature/post-store

まとめ

・ブランチ名を間違えたら 削除して作り直すのが最短

・削除前に 必ず別ブランチへ移動

・push 済みかどうかで対応が変わる点に注意

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?