Git – How to list committed files that are going to push ?

In Git, we can use git show commit_id --name-only to list all the committed files that are going to push to the remote repository.

P.S The git status didn’t show the committed files.

1. Case Study

Below case, git add and committed too many unwanted target/* files accidentally, how to remove the committed files in local? (Haven’t push to a remote git server)

Terminal

$ git commit -m "test uncommit"

[master f5f3fa6] test uncommit
 7 files changed, 3603 insertions(+)
 create mode 100644 src/main/java/com/mkyong/benchmark/BenchmarkMap.java
 create mode 100644 target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_forwardLoop_jmhTest.java
 create mode 100644 target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType.java
 create mode 100644 target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B1.java
 create mode 100644 target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B2.java
 create mode 100644 target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B3.java
 create mode 100644 target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_reverseLoop_jmhTest.java

2. Solution

2.1 git log to display all the commit_id, the first one is the last commit_id, copy it.

Terminal

$ git log
commit f5f3fa6e012adf5d2caf2e1310421ad8258950d3 (HEAD -> master)
Author: mkyong <-@gmail.com>
Date:   Mon Nov 26 16:02:00 2018 +0800

    test uncommit

commit 3250f725962ba12b6695c083895dd4c3533545a2 (origin/master)
Author: mkyong <-@gmail.com>
Date:   Mon Nov 25 15:00:17 2018 +0800

    init

commit b28f3199e10d2aed5426342f6ed172ba7f710483
Author: mkyong <-@gmail.com>
Date:   Fri Nov 23 17:27:25 2018 +0800

    update readme

2.2 git show commit_id --name-only to display all the files committed in the specified commit_id.

Terminal

$ git show f5f3fa6e012adf5d2caf2e1310421ad8258950d3 --name-only

commit f5f3fa6e012adf5d2caf2e1310421ad8258950d3 (HEAD -> master)
Author: mkyong <-@gmail.com>
Date:   Mon Nov 26 16:02:00 2018 +0800

    test uncommit

src/main/java/com/mkyong/benchmark/BenchmarkMap.java
target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_forwardLoop_jmhTest.java
target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType.java
target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B1.java
target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B2.java
target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B3.java
target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_reverseLoop_jmhTest.java

2.3 Undo the last commit with git reset --soft HEAD~1, move the mistakenly committed files back to the staging area.

Terminal

$ git reset --soft HEAD~1

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

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   src/main/java/com/mkyong/benchmark/BenchmarkMap.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_forwardLoop_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B1.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B2.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_jmhType_B3.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkForwardReverseLoop_reverseLoop_jmhTest.java
        new file:   target/generated-sources/annotations/com/mkyong/benchmark/generated/BenchmarkLoop_jmhType.java
        
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .idea/

References

  1. git log documentation
  2. git show documentation

About the Author

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

Comments

avatar