Git撤销和回退
如果你的文本里面有一个readme.txt文件在里面增加了一行test txt
test readme.txt
test txt
如果你想恢复到之前的版本,你可以将最后一行删除了,如果你不知道修改了哪些内容,或者修改的内容太多了,你可以用git status
查看有一下。git会告诉你用 git checkout -- <file>
放弃工作区的内容
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
命令 git checkout -- readme.txt
意思就是,把readme.txt文件在工作区的修改全部撤销,有2种情况
1、readme.txt自修改后还没有被放到暂存区,现在要修改就回到和版本库一模一样的状态
2、readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态
总之,就是让这个文件回到最近一次 git commit或 git add时的状态
$ cat readme.txt
test readme.txt
test txt
$ git checkout -- readme.txt
$ cat readme.txt
test readme.txt
如果现在不仅仅修改工作区里面的文件,并且git add了
$ cat readme.txt
test readme.txt
test txt
$ git add readme.txt
如果在commit之前,用 git status查看一下,修改只是添加到了暂存区,还没有提交
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
git 告诉我们,用命令 git reset HEAD <file>
可以把暂存区的修改撤销掉,将版本库里面的内容撤销到暂存区里面
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
这个时候在用git status 查看 一下,现在暂存区是干净的,工作区有修改
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: readme.txt
这个时候有用git checkout -- readme.txt
$ git checkout -- readme.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean