1. 概要

Git 作業ディレクトリには、ステージングされたファイル、ステージングされていないファイル、追跡されていないファイルなど、さまざまな種類のファイルを含めることができます。

このチュートリアルでは、インデックスにない作業ディレクトリの変更を破棄する方法を説明します。

2. 作業ディレクトリの状態の分析

この例では、 Gitリポジトリをフォークしてクローンを作成し、作業ディレクトリにいくつかの変更を加えたとします。

作業ディレクトリのステータスを確認しましょう。

$ git status
On branch master
Your branch is up to date with 'origin/master'.

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:   README.md
        modified:   gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        gradle/maven-to-gradle/src/main/java/com/sample/javacode/TimeZones.java

no changes added to commit (use "git add" and/or "git commit -a")

ここでは、変更されてステージングされていないファイルと、追加した新しいファイルを確認できます。

次に、 git add を使用して既存のJavaファイルをステージングし、状態を再度確認します。

$ git add gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java

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:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        gradle/maven-to-gradle/src/main/java/com/sample/javacode/TimeZones.java

ここでは、作業ディレクトリに3つのカテゴリのファイルがあることがわかります。

  • ステージングされたファイル– DisplayTime.java
  • ステージングされていないファイル– README.md
  • 追跡されていないファイル– TimeZones.java

3. 追跡されていないファイルの削除

追跡されていないファイルとは、リポジトリに新しく、バージョン管理に追加されていないファイルです。 これらはcleanコマンドで削除できます。

$ git clean -df

-df オプションを使用すると、削除が強制され、追跡されていないディレクトリも削除に含まれます。 このコマンドを実行すると、削除されたファイルが出力されます。

Removing gradle/maven-to-gradle/src/main/java/com/sample/javacode/TimeZones.java

それでは、ステータスをもう一度確認しましょう。

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java

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:   README.md

cleanコマンドが追跡されていないファイルを作業ディレクトリからどのように削除したかを確認できます。

4. コミット用にステージングされていない変更の削除

追跡されていないファイルを削除したので、作業ディレクトリ内のステージングされたファイルとステージングされていないファイルを処理する必要があります。 「–」オプションを指定したcheckoutコマンドを使用して、コミット用にステージングされていないすべての変更を削除できます。

$ git checkout -- .

コマンドを実行した後、ステータスをもう一度確認しましょう。

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   gradle/maven-to-gradle/src/main/java/com/sample/javacode/DisplayTime.java

作業ディレクトリに段階的な変更のみが含まれていることがわかります。

5. 結論

このチュートリアルでは、現在Gitバージョン管理下にないファイルの中で、作業ディレクトリにステージングされたファイル、ステージングされていないファイル、および追跡されていないファイルを含める方法を確認しました。

また、 git clean-dfおよびgitcheckout —。が、ステージングされていないすべての変更を作業ディレクトリから削除する方法も確認しました。