撤销回退 情况⼆:已经 add ,但没有 commit
add 后还是保存到了暂存区呢?怎么撤销呢?
1 # 向ReadMe中新增⼀⾏代码
2 hyb@139-159-150-152:~/gitcode$ vim ReadMe
3 hyb@139-159-150-152:~/gitcode$ cat ReadMe
4 hello bit
5 hello git
6 hello world
7 hello version1
8 hello version2
9 hello version3
10 This piece of code is like shit #新增代码
11
12 # add 存⼊暂存区
13 hyb@139-159-150-152:~/gitcode$ git add ReadMe
14 hyb@139-159-150-152:~/gitcode$ git status
15 On branch master
16 Changes to be committed:
17 (use "git restore --staged <file>..." to unstage)
18 modified: ReadMe
让我们来回忆⼀下学过的 git reset 回退命令,该命令如果使⽤ --mixed 参数,可以将暂存区的内容退回为指定的版本内容,但⼯作区⽂件保持不变。那我们就可以回退下暂存区的内容了!!!
⽰例如下:
1 # --mixed 是默认参数,使⽤时可以省略
2 hyb@139-159-150-152:~/gitcode$ git reset HEAD ReadMe
3 Unstaged changes after reset:
4 M ReadMe
⽤ git status 查看⼀下,发现现在暂存区是⼲净的,⼯作区有修改。
1 hyb@139-159-150-152:~/gitcode$ git status
2 On branch master
3 Changes not staged for commit:
4 (use "git add <file>..." to update what will be committed)
5 (use "git restore <file>..." to discard changes in working directory)
6 modified: ReadMe
7
8 no changes added to commit (use "git add" and/or "git commit -a")
还记得如何丢弃⼯作区的修改吗?
1 hyb@139-159-150-152:~/gitcode$ git checkout -- ReadMe
2 hyb@139-159-150-152:~/gitcode$ git status
3 On branch master
4 nothing to commit, working tree clean
5 hyb@139-159-150-152:~/gitcode$ cat ReadMe
6 hello bit
7 hello git
8 hello world
9 hello version1
10 hello version2
11 hello version3
恢复了!