[原]git的使用(四)---撤销修改
8.撤销修改
$ 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. #把在工作区readme.txt文件中增加了一行
查看工作区readme.txt文档状态和修改内容是否正确
$ 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 diff readme.txt #查看文件内容是否是自己需要的
diff --git a/readme.txt b/readme.txt
index d5e8dad..921e20a 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,5 @@
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.
+Git tracks changes of files.
+My stupid boss still prefers SVN. #发现这一内容是不能提交的(对boss不满,boss会记住的,你懂的
怎么才能撤销刚才对readme的修改呢?(这个问题还有个答案就是:直接更改文件内容就行,比较添加了什么,就删除什么。)
或者万一已经将第一次修改的文件提交到了中转站(暂存区),然后又第二次修改了文件,怎么办呢?
看!有提示(use "git checkout -- <file>..." to discard changes in working directory)。注意!!!
其中“--” 很重要,不能遗漏。
-----------------------------------------------------------------------------------------------------
命令git checkout -- readme.txt
意思就是,把readme.txt
文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit
或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. #这是刚才添加的一行 未提交到暂存区(中转站)
(由于未add到暂存区)删除这一行有两种方式:手动vim删除,还有一种就是下面这种使用 git checkout -- <file>
$ git checkout -- readme.txt #撤销更改,即撤销添加的My stupid boss still prefers SVN. 这一行 $ 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.
再来实践,如果第一次修改后已经提交,再次修改文本文件,需要撤销第二次的修改:
$ 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 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 add readme.txt #
解决了工作区中的撤销修改,再来看看图中暂存区的撤销修改:
git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD
时,表示最新的版本
[ 实践出真知]
$ git status #查看当前状态 On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) #提示:如果要将暂存区的文本版本回退到工作区,使用git reset HEAD <file> modified: readme.txt #第一次修改文本并add到暂存区
由于第一次修改并add到了暂存区,现在需要回退到工作区:
$ git reset HEAD readme.txt #将暂存区的文本版本回退到工作区 即回退到add之前的状态,add之前的状态是进行的第一次修改 Unstaged changes after reset: M readme.txt $ 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 diff #验证是不是停留在了第一次修改的位置。第一次修改添加了一行信息:My stupid boss still prefers SVN.
diff --git a/readme.txt b/readme.txt
index d5e8dad..a60e297 100644
--- a/readme.txt
+++ b/readme.txt
@@ -2,3 +2,4 @@ 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.
+My stupid boss still prefers SVN
撤销第一次的修改应该用哪个方法呢???如果还不清楚,就再从头看一遍此文章。
下面是答案:
$ git checkout -- readme.txt #撤销第一次的修改 $ git status #文本文件的版本已经恢复到最开始的版本,工作区干净,暂存区也是干净的 On branch master nothing to commit, working directory clean
$ 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.
参考git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
出处:http://www.cnblogs.com/horizonli/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
支付宝(alipay) 二维码打赏 |
微信(wechat) 二维码打赏 JUST LI(**波) |
微信公众号: 木子李的菜田 |