LoginSignup
0
0

More than 3 years have passed since last update.

Gitで間違えたブランチにAddやCommitしてしまった時に使うコマンド

Last updated at Posted at 2020-11-12

Gitの詳しい記事はいくらでもあるので初心者向けの内容です。
最近良く使うのでまとめてみました。

feature/bに変更を入れたいのに、実はaddをfeature/aでやってしまっていた!!

これでaddを取り消しできます。

git reset HEAD

1ファイルだけの場合は、ファイル名をつけましょう。

git reset HEAD test.txt

その後、stashで変更を保存し、

git stash

ブランチをfeature/bに変更

git checkout feature/b

これで、feature/b保存した変更を反映

git stash pop

後は、改めて、addし直すだけです。

feature/bに変更を入れたいのに、実はcommitをfeature/aでやってしまっていた!!

これでcommitを取り消しできます。
HEAD^でローカルブランチのHEADを1つ手前に移動、しかし、--softなのでローカルのファイルはいじらない、というコマンドです)

git reset --soft HEAD^

その後の流れはaddのときと一緒。stashで変更を保存し、

git stash

ブランチをfeature/bに変更

git checkout feature/b

これで、feature/b保存した変更を反映

git stash pop
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