撤销修改
例如:
你在readme.txt中添加了一行:
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
你猛然发现了stupid boss可能会让你丢掉这个月的奖金
既然错误发现得很及时,就可以很容易地纠正它。你可以删掉最后一行,手动把文件恢复到上一个版本的状态。
如果用git status查看一下:
$ 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: readme.txt
no changes added to commit (use \"git add\" and/or \"git commit -a\")
在Git中,git checkout -- file可以丢弃工作区的修改:
$ git checkout -- readme.txt
命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态。
现在,看看readme.txt的文件内容:
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
文件复原了。
git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout命令。
现在假定是凌晨3点,你不但写了一些胡话,还git add到暂存区了:
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
$ git add readme.txt
庆幸的是,在commit之前,你发现了这个问题。用git status查看一下,修改只是添加到了暂存区,还没有提交:
$ git status
On branch master
Changes to be committed:
(use \"git reset HEAD <file>...\" to unstage)
modified: readme.txt
用命令git reset HEAD <file>可以把暂存区的修改撤销掉(unstage),重新放回工作区:
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。
再用git status查看一下,现在暂存区是干净的,工作区有修改:
$ 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: readme.txt
丢弃工作区的修改:
$ git checkout -- readme.txt
$ git status
On branch master
nothing to commit, working tree clean
世界和平。
(以上摘抄自廖雪峰的Git教程,仅作学习记录并标为转载)
继续阅读与本文标签相同的文章
单片机的烧写原理,三种方式了解一下
-
猎户星空CEO傅盛:现在是AI发展最好时期,家庭服务机器人前景可期
2026-05-14栏目: 教程
-
5G远程驾驶和微公交首秀互联网大会
2026-05-14栏目: 教程
-
学宏程序编程,这些知识必不可少!
2026-05-14栏目: 教程
-
华为准备卖出“落后”的5G,多家美企极力竞争!任正非格局太大!
2026-05-14栏目: 教程
-
百度:飞桨深度学习平台已累计服务150多万开发者
2026-05-14栏目: 教程
