Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

A Git working directory can contain different types of files including staged files, unstaged files, and untracked files.

In this tutorial, we'll see how to discard changes in our working directory that are not in the index.

2. Analyzing the State of a Working Directory

For our example, let's say that we've forked and cloned a Git Repository and then made some changes to our working directory.

Let's check the status of our working directory:

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

Here, we can see some files that are modified and unstaged, along with a new file we added.

Now, let's stage the existing Java file by using git add and check the state again:

$ 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

Here, we can see that we have three categories of files in our working directory:

  • staged files – DisplayTime.java
  • unstaged files – README.md
  • untracked files – TimeZones.java

3. Removing the Untracked Files

Untracked files are those which are new to the repository and haven't been added to version control. We can remove these with the clean command:

$ git clean -df

The -df option ensures that removal is forced and that untracked directories are also included for removal. Running this command will output which files were removed:

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

Now, let's check the status again:

$ 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

We can see how the clean command has removed the untracked file from our working directory.

4. Removing the Changes Not Staged for Commit

Now that we've removed the untracked files, we're left to deal with the staged and unstaged files inside our working directory. We can use the checkout command with the “–” option to remove all the changes that are not staged for commit:

$ git checkout -- .

Let's check the status again after running the command:

$ 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

We can see that our working directory now contains only the staged changes.

5. Conclusion

In this tutorial, we saw how our working directory can contain staged, unstaged, and untracked files among the files that aren't currently under Git version control.

We also saw how git clean -df and git checkout — . can remove all unstaged changes from our working directory.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!