Git How to undo the last commit?

摘要: In Git, we can use git reset --soft HEAD~1 to undo the last commit in local. (The committed files haven’t pushed to the remote git server)

In Git, we can use git reset --soft HEAD~1 to undo the last commit in local. (The committed files haven’t pushed to the remote git server)

1. Case Study

git commit and find out some unwanted target/* files are committed accidentally, I haven’t issue the git push, any idea how to undo the commit?

Terminal
$ git commit -m "test uncommit"
[master f5f3fa6] test uncommit
 3 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

2. Solution

This git command git reset --soft HEAD~1 will undo the last commit, 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

Done.

Note
You may interest in this article : list all committed files

References

  1. Git – How to remove files from staging
  2. Git – How to list committed files that are going to push?
  3. git reset documentation

上一篇: Java 8 Stream.iterate examples
下一篇: Git How to remove files from staging (Changes to be committed)
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号