gitの基本コマンド2 checkout, -- reset, commi --amend
ファイルの変更を取り消すgit checkout --
index.htmlに以下を追加
<p>この素晴らしい世界に祝福を!</p>
git statusで変更を確認すると変更部分が確認できます。
$ git status On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: index.html
ではgit checkout --で変更部分を取り消しましょう
% git checkout -- index.html
そして
% git status On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
変更部分を取り消すことができました。
git checkout --はワークツリーの状態をステージに反映しています。
git reset git addでステージに追加しちゃったけどやっぱり追加したくないとき
git reset HEAD <ファイル名>
git reset HEAD <ディレクトリ名>
全変更を取り消したい場合は git reset HEAD .
このコマンドはステージに追加したものだけ削除します。git checkout --のようにローカルの変更分は変わらないので注意です。
git commit --amend 直前のコミットをやり直したい
index.htmlに以下を追記してgit add . とgit commitをします。
<p>この素晴らしい世界に祝福を</p>
ここでgit pushしようとするが、index.html内を変更したいと思った時にgit commit --amendを使うことができます。
index.htmlを書き直します。
<p>このすば<p>
そして再度git add .をします。そしてgit commit --amendをしてあげましょう。
git commit --amend -m 'このすばを追記'
git logで確認するとgit commitが反映されていることがわかります。
% git log commit b6596078a33963f7787169c6b0f21c8a367da1c6 (HEAD -> main) Author: Date: Wed Nov 17 20:12:03 2021 +0900 このすばを追記
注意することが一点リモートリポジトリにプッシュしたコミットはやり直したらダメ!