HOWTO · Git

Git の変更を元に戻す

このチュートリアルでは、ローカルおよびリモートリポジトリへの変更を元に戻すために使用するさまざまなコマンドについて説明します。これらのコマンドには、git clean、git reset、gitrevert が含まれます。

このページの内容

このチュートリアルでは、ローカルおよびリモートリポジトリへの変更を元に戻すためのさまざまなコマンドについて説明します。これらのコマンドは次のとおりです。

  1. git clean
  2. git reset
  3. git 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

これで、プロジェクトの現在の状態を改ざんすることなくファイルにアクセスできます。git checkout main コマンドを使用して、現在のプロジェクトワークスペースに戻ります。

git clean コマンドを使用して Git の変更を元に戻する

git clean コマンドを使用して、追跡されていないファイルの変更を元に戻します。git clean コマンドを元に戻すことはできません。

  1. git clean -n オプションは、tryout コマンドとして機能します。追跡されていないファイルのみが表示され、削除されません。

    例:

    pc@JOHN MINGW64 ~/Git (main)
    $ git clean -n
    Would remove Hello world.txt
    Would remove Test1.txt
    
  2. git clean --force コマンドは、作業ディレクトリ内の追跡されていないすべてのファイルとフォルダを削除します。 .gitignore コマンドを使用して例外をスローできます。

    例:

    pc@JOHN MINGW64 ~/Git (main)
    $ git clean --force
    Removing Hello world.txt
    Removing Test1.txt
    

    ファイルがディレクトリにあるかどうかを確認しましょう。

    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 で動作します。

  1. git reset --hard オプションは、指定されたコミットを変更します。ただし、ステージングインデックスと作業ディレクトリの変更は、変更と一致します。段階的なコミットはすべて、その過程で失われます。

    例:

    $ git reset --hard
    HEAD is now at 78129a6 Revert "$first commit"
    

    このコマンドは、HEAD へのハードリセットを実行します。

  2. git reset --mixed コマンドは、ステージングインデックスからの変更を作業ディレクトリに保存します。

    例:

    $ git reset --mixed
    Unstaged changes after reset:
    M       .bash_history
    M       text.txt.txt
    M       text.txt.txt.bak
    

    このコマンドは、元に戻された変更を出力から作業ディレクトリに保存しました。支店の状況を確認しましょう。

    $ 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")
    
  3. 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")
    

    上記の出力から、作業ディレクトリとステージングインデックスは変更されていません。コミットのハッシュを使用して、特定のコミットをリセットします。