Git の変更を元に戻す
-
git logコマンドを使用して、Git のコミットの履歴を確認する -
git cleanコマンドを使用して Git の変更を元に戻する -
git revertコマンドを使用して Git の変更を元に戻する -
git resetコマンドを使用して Git の変更を元に戻する
このチュートリアルでは、ローカルおよびリモートリポジトリへの変更を元に戻すためのさまざまなコマンドについて説明します。これらのコマンドは次のとおりです。
git cleangit resetgit revert
git log コマンドを使用して、Git のコミットの履歴を確認する
まず、コミットの履歴を確認する方法を見てみましょう。git log コマンドを使用してこれを確認します。
pc@JOHN MINGW64 ~/Git (main)
$ git log
commit 195e5c362975354d62ebc469da2e3cd276c7da03 (HEAD -> main)
Author: John <wachirajohnie11@gmail.com>
Date: Mon Feb 21 12:11:12 2022 +0300
commit 7b19db4b35c1ca15e5ecb8df1f805d44aad62e5f
Author: John <wachirajohnie11@gmail.com>
Date: Mon Feb 21 10:09:31 2022 +0300
first commit
このコマンドは、作業ブランチの履歴のみを表示します。すべてのブランチを確認するには、以下のコマンドを使用します。
git log --branches=*
以下に示すように、git checkout コマンドを使用して特定のコミットを表示します。

これで、プロジェクトの現在の状態を改ざんすることなくファイルにアクセスできます。git checkout main コマンドを使用して、現在のプロジェクトワークスペースに戻ります。
git clean コマンドを使用して Git の変更を元に戻する
git clean コマンドを使用して、追跡されていないファイルの変更を元に戻します。git clean コマンドを元に戻すことはできません。
-
git clean -nオプションは、tryout コマンドとして機能します。追跡されていないファイルのみが表示され、削除されません。例:
pc@JOHN MINGW64 ~/Git (main) $ git clean -n Would remove Hello world.txt Would remove Test1.txt -
git clean --forceコマンドは、作業ディレクトリ内の追跡されていないすべてのファイルとフォルダを削除します。.gitignoreコマンドを使用して例外をスローできます。例:
pc@JOHN MINGW64 ~/Git (main) $ git clean --force Removing Hello world.txt Removing Test1.txtファイルがディレクトリにあるかどうかを確認しましょう。
```bash
pc@JOHN MINGW64 ~/Git (main)
$ git status
On branch main
nothing to commit, working tree clean
```
出力はクリーンなブランチを示しています。コマンドはファイルを正常に削除しました。
git revert コマンドを使用して Git の変更を元に戻する
git revert コマンドは、コミットによって行われた変更に作用し、それを上書きします。これを使用して、ある時点に戻って変更を加えます。
コマンドを使用して、最初のコミットを変更しましょう。
git revert first commit
#[first commit [7b19db4] Revert 'first commit'
#1 file changed, 1 deletion(-)
コミット参照を渡す必要があります。
git reset コマンドを使用して Git の変更を元に戻する
最後に取り上げるコマンドは、git reset です。このコマンドは HEAD で動作します。
git reset --hardオプションは、指定されたコミットを変更します。ただし、ステージングインデックスと作業ディレクトリの変更は、変更と一致します。段階的なコミットはすべて、その過程で失われます。
例:
```bash
$ git reset --hard
HEAD is now at 78129a6 Revert "$first commit"
```
このコマンドは、`HEAD` へのハードリセットを実行します。
-
git reset --mixedコマンドは、ステージングインデックスからの変更を作業ディレクトリに保存します。例:
$ git reset --mixed Unstaged changes after reset: M .bash_history M text.txt.txt M text.txt.txt.bakこのコマンドは、元に戻された変更を出力から作業ディレクトリに保存しました。支店の状況を確認しましょう。
```bash
$ git status
On branch 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: .bash_history
modified: text.txt.txt
modified: text.txt.txt.bak
Untracked files:
(use "git add <file>..." to include in what will be committed)
.bash_history.bak
no changes added to commit (use "git add" and/or "git commit -a")
```
-
git reset --softコマンドは、コミット履歴のみを変更します。デフォルトのポインタは常にHEADです。例:
pc@JOHN MINGW64 ~/Git (main) $ git reset --soft pc@JOHN MINGW64 ~/Git (main) $ git status On branch 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: .bash_history modified: text.txt.txt modified: text.txt.txt.bak Untracked files: (use "git add <file>..." to include in what will be committed) .bash_history.bak no changes added to commit (use "git add" and/or "git commit -a")
上記の出力から、作業ディレクトリとステージングインデックスは変更されていません。コミットのハッシュを使用して、特定のコミットをリセットします。
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn