Skip to content

Instantly share code, notes, and snippets.

@donchan922
Last active July 19, 2020 07:49
Show Gist options
  • Save donchan922/bb2d3c6b4bd6035d9c442cbb9ce8e7c7 to your computer and use it in GitHub Desktop.
Save donchan922/bb2d3c6b4bd6035d9c442cbb9ce8e7c7 to your computer and use it in GitHub Desktop.
# masterブランチで作業している
$ git branch
* master
$ git log --oneline
9b78abe (HEAD -> master) Initial commit
# 中身は空
$ cat README.md
# README.mdを編集する(誤った内容)
$ echo "Hallo World" > README.md
# README.mdをコミット&プッシュする
$ git add README.md
$ git commit -m "modify README.md"
$ git push origin HEAD
# ここで、編集内容が間違っていたことに気づく
$ cat README.md
Hallo World
# 直前のコミットの内容を取り消したい
$ git log --oneline
c371b39 (HEAD -> master, origin/master) modify README.md
9b78abe Initial commit
# 直前のコミットをなかったことにする(変更した内容はそのまま)
$ git reset --soft HEAD^
# コミットがなかったことになっている
$ git log --oneline
9b78abe (HEAD -> master) Initial commit
# 変更した内容は取り消されていない
$ cat README.md
Hallo World
# 歴史を書き換えようとするためエラーが発生する
$ git push origin HEAD
To https://github.com/donchan922/practice2.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/donchan922/practice2.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 強制的にプッシュする
$ git push -f origin HEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment