1
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のcommit(コミット)のよく使う「取り消し」「修正」方法まとめ

Last updated at Posted at 2023-06-18

① 直近のコミットを取り消す(作業は残す)

git reset --soft HEAD^

コミットだけ取り消し、作業内容はそのまま

② 1コミット進めたい(戻しすぎた時)

git reset --soft HEAD@{1}

resetをやりすぎた時に1回分だけ戻す(HEADの直前へ)

③ 5個前のコミットまで完全に戻す(作業も削除)

git reset --hard HEAD~5

5コミット前の状態へ。作業内容も消えるので注意

④ 直前のコミットを上書き修正

git commit --amend -m "修正版のメッセージ"

直前のコミット内容やメッセージを上書き

番外編:間違ってpushしてしまった時

git push -f origin master

ローカル履歴でリモートを強制上書き

チーム開発ではこちらが安全

git push --force-with-lease origin master

✅ まとめ

やりたいこと コマンド 注意点
コミットだけ取り消す git reset --soft HEAD^ 作業は残る
戻しすぎを戻す git reset --soft HEAD@{1} resetの取り消し
全部取り消す git reset --hard HEAD~5 作業も消える
直前のコミット修正 git commit --amend push前限定
間違ってpushした git push -f / --force-with-lease 慎重に!
1
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
1
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?