LoginSignup
7
3

More than 5 years have passed since last update.

[Git] 間違ったブランチで作業してしまった場合の対処

Last updated at Posted at 2018-07-10

何度も忘れるのでメモ用に。

Commitしてしまったら

まず取り消す。

$ git commit -m "うわやっちゃったよこれもうどうすんだよ"
$ git reset HEAD~  

git reset HEAD~によってディスク上のファイルはすべてunstageされる(変更は残ったまま)
stageされたままでいいのなら、git reset --soft HEAD~を使う。

Pushまで行ってしまっていたら

強引なやり方として、一つ前のcommitをforce pushする方法がある。
(もし、これよりも良い対処があったら教えてください)

$ git push -f [一つ前のcommitID]:[現在のブランチ名]

git stashを用いて別ブランチに変更を反映させる

commit前に行う。git stashで一度変更を対比させ、ブランチ移動後にgit stash popで展開する。

$ git stash
$ git checkout [正しいブランチ]
$ git stash pop

Sources

https://stackoverflow.com/questions/1270514/undoing-a-git-push
https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git
https://stackoverflow.com/questions/7217894/moving-changed-files-to-another-branch-for-check-in/7218106

7
3
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
7
3