LoginSignup
0
2

More than 5 years have passed since last update.

Gitで誤ったcommitをしたときの解決方法

Last updated at Posted at 2018-07-06

前提

リモートリポジトリを以下のコマンドで作成
git --bare init —share 

-shared オプション付きで作った場合、
none-fast-forward な push が断固拒否される設定になり、revertやresetを実行しても取り消すことができない。

取り消しできない例

・ローカルでcommitを取り消し、そのあとのpushが実行できない。
例)
1,ローカルのブランチで直前のコミットを取り消し
git reset --hard HEAD^
2,1をリモートリポジトリにpush
git push -f
error: denying non-fast-forward refs/heads/master (you should pull first)

解決方法

デフォルトのブランチ(master)へのcommitの取り消し

1,リモートサーバにログインし、リモートリポジトリのパスまで移動
2,現在のブランチの確認
git branch
3,git logで取り消すcommitを確認
git log
4,取り消し
 例)直前のcommitの場合
git reset HEAD~ —soft
5,再度、git logでcommitが取り消されたことを確認

デフォルト以外のブランチへのcommitを取り消し

1,対象となるブランチからバックアップブランチ作成
git checkout -b bk_{対象となるブランチ名}
2.ローカルブランチでコミットの取り消し
3.リモートのブランチを削除
git push origin :{対象となるブランチ名}
4,削除したブランチ名で2をpush
git push -u origin {対象となるブランチ名}
この方法だと、defaultのブランチに設定されているブランチは削除できない。

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
2018/07/09 ご指摘いただきcomit→commitに変更

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