LoginSignup
0
0

ブランチを削除してやり直したい時のコマンド手順

Last updated at Posted at 2024-05-18

はじめに

よく使うブランチ関係のコマンドをアウトプットしています
初心者なので、誤りありましたら教えていただけると嬉しいです😃
統合ブランチをdevelop、作業ブランチをfeatureとしています

ローカルブランチの削除方法

リモートにプッシュしていない場合、ローカルブランチを削除するだけでやり直すことができます

手順

現在作業ブランチにいる場合、作業ブランチを削除することができません
そのため、一旦削除対象以外のブランチに切り替えます
(mainでもdevelopでもいいです)

・ブランチの切り替え

$ git checkout develop

・作業ブランチを削除

$ git branch -D ブランチ名

作業ブランチでコミットをしていなかった場合に、
ブランチを削除したにも関わらず作業した内容が残っている場合があります
そういう時には、削除対象以外のブランチに切り替えた時にターミナルに以下が出力されているかと思います

ec2-user:~/environment/アプリ名 (feature/作業名) $ git checkout develop
M app/controllers/admin/confirmations_controller.rb
M app/controllers/admin/customers_controller.rb
M app/controllers/admin/genres_controller.rb
︙
Switched to branch 'develop'

この表示は作業ブランチで修正された箇所を引き継いでdevelopブランチで作業を続けられることを意味しています
しかし、作業をやり直したいわけですから余計なお世話ですよね

なので、まっさらな状態で新しく作業を始めたい場合は、以下のコマンドを入力します

$ git reset --hard HEAD

これで直前のコミット時点の状態に戻り、変更が全て破棄されます

注意
上記のコマンドで修正状態を元に戻ししても、
新しく作成したviewファイルなどのは削除されず、コードの中身もそのままです
ステータスを確認しておきましょう!

$ git status

以上のコマンドでやり直すことができます!

最新の統合リポジトリを取得する癖をつけましょう
コンフリクトが起きる可能性があります!

・リモートの(統合ブランチの)最新版をローカルに反映させる

$ git pull origin develop

ブランチの切り方はこちら

リモートブランチの削除方法

・リモートブランチを削除

$ git push origin --delete ブランチ名

すでにそのリモートブランチが削除されている場合、以下のエラーが発生する可能性があります

error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'
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