0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【git】コミットのやり直し(pushしていない場合)

Last updated at Posted at 2020-05-22

前提

さっきコミットしたファイルの記述を一部間違えたので訂正してコミットをやり直したい時。
pushしていないことが前提。

A:直前のコミットをやり直す

① (ファイルを修正する)
② % git add .
③ % git commit --amend
(vim立ち上がりコメントを修正)
  • 上記が終わったら、念のためgit --oneline log -p -n 3などでうまくいってるか確認。

  • 【vimの使い方】
    iと打つと入力できるようになる→コメント入力やEscキーで切り替え→:wqと入力。

  • コメントも修正したい場合はここで修正できる。

B:何個か前のコミットをやり直す

① % git rebase -i コミットID
② ファイル修正
③ % git add ファイル名
④ % git commit --amend
(vim立ち上がりコメントを修正)
⑤ % git rebase --continue

①について

例えば、直前3つのコミットをやり直すときは、

% git rebase -i HEAD~3

すると下記のようにvimが立ち上がる。
799445A1-AF73-4FAF-8AE2-86383EA73DC2_4_5005_c.jpeg
例えば、この3つのファイルのうちfirst.htmlを修正したいならpickeditに変更しEscキーで切り替え→:wqで閉じる。変更したくないファイルはpickのままにする。

②について

エディタで修正したいファイル(ここではfirst.html)を開き、正しく修正し保存。

③、④のコマンドを入力

【vimの使い方】
iと打つと入力できるようになる→コメント入力やEscキーで切り替え→:wqと入力。

コメントも修正したい場合はここで修正できる。

これでfirst.htmlの修正は終了。

⑤について

次のコミット修正に進むためにgit rebase --continue入力。

①の時点で3つのファイルのうち他のsocond.htmlthird.htmlpickにしてたのでこのまま終了となる。もし他のファイルもeditにしていた場合はvimが立ち上がり同じ手順を繰り返す。

念のためgit --oneline log -p -n 3などでうまくいってるか確認。

参考文献:Udemy もう怖くないGit!チーム開発で必要なGitを完全マスター (山浦 清透)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?