git commit 之后,如何撤销commit

写完代码或是添加修改文件后,通过git status 命令查看

命令:git status

admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$ git status  ./
On branch develop
Your branch is up to date with 'origin/develop'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        ReadMe.md

nothing added to commit but untracked files present (use "git add" to track)
admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$

把修改好的文件,通过git add 命令进行添加

命令:git add file //添加修改文件

admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$ git add ReadMe.md

再次使用git status 命令查看file的状态

admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$ git status  ./   
On branch develop
Your branch is up to date with 'origin/develop'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   ReadMe.md

admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$

在执行git add 之后,需要执行git commit命令提交消息

命令:git commit -m "备注消息描述"

admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$ git commit -m "closes #100,add file ReadMe.md"    
===================msg================

closes #100,add file ReadMe.md

===================msg================
had input redmine issue #ID
[develop d1824c068] closes #100,add file ReadMe.md
 1 file changed, 17 insertions(+)
 create mode 100644 qcom/proprietary/commonsys/fastmmi/ReadMe.md
admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi/$

倘若执行完commit后,想撤回commit,怎么办?可以通过如下命令进行撤销

命令:git reset --soft HEAD^

admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$git reset --soft HEAD^

其中,

--soft  

不删除工作空间改动代码,撤销commit,不撤销git add . 

HEAD^的意思是上一个版本,也可以写成HEAD~1

如果你进行了2次commit,想都撤回,可以使用HEAD~2

如果commit注释写错了,只是想改一下注释,只需要:

命令:git commit --amend

此时会进入默认vim编辑器,修改注释完毕后wq保存就好了。

再次使用git status 命令查看file的状态

admin@software:~/workspace/sourcecode/vendor/qcom/proprietar/commonsys/fastmmi$ git status  ./   
On branch develop
Your branch is up to date with 'origin/develop'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   ReadMe.md

admin@software:~/workspace/sourcecode/vendor/qcom/proprietary/commonsys/fastmmi$

ReadMe.md 文件的状态又回到git add 之后的状态,这样就成功的撤销了你的commit

注意:仅仅是撤回commit操作,您写的代码仍然保留。
 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值