码迷,mamicode.com
首页 > 其他好文 > 详细

git的使用(四)

时间:2016-03-27 13:57:41      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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 commitgit 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.cnblogs.com/horizonli/p/5325350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!