Git 取消本地最后一次 commit

更新日期: 2020-02-26 阅读次数: 5297 字数: 226 分类: Git

背景

早上突然有了个想法,立马修改代码并 Git 提交,但是在 push 时发现没有合并远端最新代码。导致大量的代码冲突 (因为这台电脑很长时间没有更新过这个项目的代码),头一下子大了。。。我都准备删除本地项目,重新拉代码了。但是一想这样太 low 了,还是查查怎么挽救吧,毕竟 git 无所不能。

解决方法

git reset HEAD~

Unstaged changes after reset:
M       some_file.go 

some_file.go 就是我早上修改的造成冲突的文件。

git status

On branch master
Your branch is behind 'origin/master' by 34 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)    

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified:   some_file.go
no changes added to commit (use "git add" and/or "git commit -a")

这时会发现那个失误的提交确实被完美处理了。

参考

https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git

stack overflow 这个答案居然有两万多个赞,看来大家都误操作过 Git 。。。

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式