Git 基本操作2--撤销修改

其实当我们选择了IT行业的那一刻起,就跟加班联系在了一起,我相信大部分人都有决战到凌晨的经历,我刚毕业曾经加班到凌晨一点,回家路上,拖着疲惫的双腿,走在城中村的小路上,任凭细雨轻抚脸庞,那时候很享受那种感觉,而实际加班的过程当中我们有时候思路是不清晰的,这时候难免会出现一些误差,我见过一个很知名的app还有test的字样,而我们在版本控制中如果遇到这种情况,我们怎么撤销修改呢,今天就重点讲解讲解:

需要注意的是我们意识到自己错误的情况的时间点,有好几种情况,理论不多说,直接实际操作:

1.如果误修改尚未add 到暂存区。可以使用一下命令git checkout -- file名称

情节分析:

zxdeMacBook-Pro:work zs$ cat addme.txt

GIT records your name and email address

I love you , cangjingkong!


使用git status 查看出状态

zxdeMacBook-Pro:work zs$ git status
On branch master
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:   addme.txt

no changes added to commit (use "git add" and/or "git commit -a")

通过查看,可以看到并没有add,同时有提示,
use "git checkout -- <file>..." to discard changes in working directory

使用 git  checkout -- 放弃修改。这时你庆幸还好没有被发布出去,这时候你想修改,之后查看,确实已经撤销了修改。

zxdeMacBook-Pro:work zs$ git checkout -- addme.txt
zxdeMacBook-Pro:work zs$ cat addme.txt

GIT records your name and email address


说明:git checkout --file 的意思就是撤销工作区的修改。


2.如果误修改已经add到暂存区,但没有commit到分支。使用 git reset HEAD file名称

   有时候,我们操作过程中,并不是每次都意识到有误操作,比如改完就顺手add到索引区了,这时候突然想到哦,刚才有条测试语句   没有删除 ,这时候怎么办?  我们查看下状态:

zxdeMacBook-Pro:work zs$ cat addme.txt
GIT records your name and email address
I love you, cangjingkong!
zxdeMacBook-Pro:work zs$ git add addme.txt
zxdeMacBook-Pro:work zs$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   addme.txt

执行git reset HEAD addme.txt,查看状态:

zxdeMacBook-Pro:work zs$ git reset HEAD addme.txt
Unstaged changes after reset:
M	addme.txt
zxdeMacBook-Pro:work zs$ git status
On branch master
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:   addme.txt

no changes added to commit (use "git add" and/or "git commit -a")
又回到了第一种情况,在重复操作1即可。

说明:git reset HEAD file 是将修改从索引区移除(unstage),回退到工作区。


如果已经提交到版本库了,这时候怎么办?如果又push到远程仓库,这时候又怎么办?就是下边情况,我想放到版本回退里去讲解

3.如果误修改已经commit到分支。

4.如果误修改已经push到了远程仓库。



知识点总结:撤销修改:1.未add ,使用git checkout -- file名称。

2.add,未commit,使用 git reset HEAD file,回到第一种情况。

     3.已经提交的,请看下一篇版本回退。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值