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?

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Carol Recent comment authors
newest oldest most voted
Carol
Guest
Carol

Thanks