Git – How to remove files from staging (Changes to be committed)

In Git, we can use git reset HEAD -- 'files/folders/patterns' to remove files or folders from the staging area (Changes to be committed).

1. Case Study

git add some unwanted target/*.java files, still in the staging, haven’t committed yet.

Terminal

$ git add project/*/*.java

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md
        new file:   pom.xml
        new file:   src/main/java/com/mkyong/benchmark/BenchmarkLoop.java
        new file:   src/main/java/com/mkyong/benchmark/bk/LoopSimpleTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_jmhType.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_jmhType_B1.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_jmhType_B2.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_jmhType_B3.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_loopForEach_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_loopForLoop_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_loopFor_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_loopIterator_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_loopWhile_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_loopWhite_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_sum_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/Main_jmhType.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/Main_jmhType_B1.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/Main_jmhType_B2.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/Main_jmhType_B3.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/Main_sum_jmhTest.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .idea/
        dependency-reduced-pom.xml
        jmh-benchmark.iml
        target/benchmarks.jar
        target/classes/
        target/jmh-benchmark-1.0.jar
        target/maven-archiver/
        target/maven-status/
        target/original-benchmarks.jar
        target/original-jmh-benchmark-1.0.jar

2. Solution

To remove those unwanted target/*.java files from the staging, uses this git reset HEAD -- target/*

Terminal

$ git reset HEAD -- target/*

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md
        new file:   pom.xml
        new file:   src/main/java/com/mkyong/benchmark/BenchmarkLoop.java
        new file:   src/main/java/com/mkyong/benchmark/bk/LoopSimpleTest.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .idea/
        dependency-reduced-pom.xml
        jmh-benchmark.iml
        target/

References

  1. git-reset documentation
  2. What does ‘stage’ mean in git?

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Carol
7 years ago

Thanks