在Git中,我们可以使用git reset HEAD -- 'files/folders/patterns'
从临时区域中删除文件或文件夹(要提交的更改)。
1.个案研究
git add
一些不需要的target/*.java
文件,这些文件仍在暂存中,尚未提交。
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.解决方案
要从登台中删除那些不需要的target/*.java
文件,请使用此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/
参考文献
翻译自: https://mkyong.com/git/git-how-to-remove-files-from-staging-changes-to-be-committed/