撤销修改 情况⼀:对于⼯作区的代码,还没有 add
撤销修改 情况⼀:对于⼯作区的代码,还没有 add
你当然可以直接删掉你⽬前在⼯作区新增的代码,像这样:
root@hcss-ecs-a74f:~# cd gitcode
root@hcss-ecs-a74f:~/gitcode# vim ReadMe
root@hcss-ecs-a74f:~/gitcode# cat ReadMe
hello git
hello world
xxx code
⾟亏我们⼯作效率不⾼,才写了⼀⾏代码就发现不⾏了,要是你写了3天,⼀直都没有提交,该怎么删掉呢?你⾃⼰都忘了⾃⼰新增过哪些,有同学说,我可以 git diff xxx ⼀下,看看差别在删啊,
那你肯定⼜要花3天时间删代码了,并且很⼤的概率还会改出bug。⼀周过去了,你怎么向你的⽼板交代呢?
Git 其实还为我们提供了更好的⽅式,我们可以使⽤ git checkout – [file] 命令让⼯作区的⽂件回到最近⼀次 add 或 commit 时的状态。 要注意 git checkout – [file] 命令中的
– 很重要,切记不要省略,⼀旦省略,该命令就变为其他意思了,后⾯我们再说。⽰例如下:
root@hcss-ecs-a74f:~/gitcode# git diff ReadMe
diff --git a/ReadMe b/ReadMe
index 8d0e412..685d464 100644
--- a/ReadMe
+++ b/ReadMe
@@ -1 +1,3 @@hello git
+hello world
+xxx code
root@hcss-ecs-a74f:~/gitcode# git checkout -- ReadMe
root@hcss-ecs-a74f:~/gitcode# cat ReadMe
hello git