概述
大家在使用git的时候,多多少少会遇到一些需要 “撤销” 的操作,比如:
- 修改了一个不需要修改的文件,需要撤销
- add了一个不需要add的文件,需要撤销
- commit了一条不需要的commit记录,需要撤销
- push了一次不需要push的操作,需要撤销
下面,就这几个问题,分别作出 “撤销” 的相应操作
工作区的代码想要撤销(对应问题1)
操作流程
$ cat hello.txt // 先查看一下文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
$ vi hello.txt // 修改文件内容
$ cat hello.txt // 查看修改后的文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
this is new line
$ 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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
撤销操作
$ git checkout hello.txt // 使用 checkout 恢复单个文件
$ cat hello.txt // 查看文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
$ git status // 查看git状态
On branch master
nothing to commit, working tree clean
add到暂存区的代码想撤销(对应问题2)
操作流程
$ cat hello.txt // 先查看一下文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
$ vi hello.txt // 修改文件内容
$ cat hello.txt // 查看修改后的文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
this is new line
$ 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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add hello.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
撤销操作
$ git reset HEAD // 将暂存区的代码撤销到工作区
Unstaged changes after reset:
M hello.txt
$ git checkout hello.txt // 根据是否需要恢复文件来决定是否执行该命令
# User: GaoYongFu
# Date: 2019-08-12
hello world!
$ git status // 查看git状态
On branch master
nothing to commit, working tree clean
提交到本地仓库的代码想撤销(对应问题3)
操作流程
$ cat hello.txt // 先查看一下文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
$ vi hello.txt // 修改文件内容
$ cat hello.txt // 查看修改后的文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
this is new line
$ 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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add hello.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
$ git commit -m 'add new line'
$ git --no-pager log --oneline
d16d80f (HEAD -> master) add new line
41b3e21 add comment
9a4158d add hello.txt
撤销操作
$ git reset --hard HEAD^ // 向前回退一个版本
HEAD is now at 41b3e21 add comment
$ git --no-pager log --oneline
41b3e21 (HEAD -> master) add comment
9a4158d add hello.txt
说明
1.可以使用HEAD^ 来描述版本,一个^ 表示前一个版本,两个^^表示前两个版本,以此类推。
2.也可以使用数字来代替^,比如说前100个版本可以写作HEAD~100。
3.也可以直接写版本号,表示跳转到某一个版本处。我们每次提交成功后,都会生成一个哈希码作为版本号,所以这里我们也可以直接填版本号,哈希码很长,但是我们不用全部输入,只需要输入前面几个字符即可,就能识别出来。
这里使用了 --hard,也可以使用–soft或者–mixed来操作,具体有什么区别,大家可以去看下文档
提交到远程仓库的代码想撤销(对应问题4)
操作流程
$ cat hello.txt // 先查看一下文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
$ vi hello.txt // 修改文件内容
$ cat hello.txt // 查看修改后的文件内容
# User: GaoYongFu
# Date: 2019-08-12
hello world!
this is new line
$ 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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add hello.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
$ git commit -m 'add new line'
$ git --no-pager log --oneline
d16d80f (HEAD -> master) add new line
41b3e21 add comment
9a4158d add hello.txt
$ git push origin master
撤销操作
$ git reset --hard HEAD^ // 向前回退一个版本
HEAD is now at 41b3e21 add comment
$ git --no-pager log --oneline
41b3e21 (HEAD -> master) add comment
9a4158d add hello.txt
$ git push origin master -f // 强制提交到远程仓库
说明
-f 参数代表强制覆盖远程仓库,这里的操作一定要确保没有问题后再去push,不然很容易丢失记录(慎重)
以上,就是比较常用的撤销方式,当然了,还有很多种方式去更便捷更安全的去撤销。