Git撤回操作之一

  在我这里,我把撤回分两种一种是还没有commit,一种是commit之后。我先介绍第一种。

一、commit之前的撤回

  再分两种情况。

       第一种,只在working directory修改了,但还没有提交到暂存区(index)。

       第二种,修改后,已经提交到暂存区了。

       有三种方法。

       1.checkout 

       这种方法适用于第一种情况。

       以下为操作步骤:

       a.读取2.txt中的内容。

$ cat 2.txt
111

  b.将2.txt中的内容进行更改

$ echo "222" > 2.txt

  c. 查看是否已经更改

$ cat 2.txt
222

  已经更改为“222”。

      d.查看状态

$ git status
# On branch dev
# 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:   2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

  Git提示2.txt已经被修改了,用add操作可以提交到暂存区,待commit。或者使用checkout来放弃这次更改。

      e.进行撤回操作

$ git checkout 2.txt

  f.查看状态

$ git status
# On branch dev
nothing to commit, working directory clean

  g.查看是否取撤销更改

$ cat 2.txt
111

  可以看到,我们已经是将2.txt的操作已经撤回。其中,你修改2.txt是立即生效的。直接在文件夹中打开文档和cat命令打开是一样的。

  同时,需要注意的就是,因为没有进行commit操作,因此也不会产生log。

      2.reset head 方法

      这种方法适用于第二种情况。就是已经添加到了暂存区(index),但是还没有提交到本地仓库(repository)。

      也就是已经add了,但还没有commit操作。

      以下是实验过程。

      a.查看2.txt的内容,

$ cat 2.txt
111

   b.更改2.txt中的内容为“222”

$ echo "222" > 2.txt

   c.查看2.txt中的内容   

$ cat 2.txt
222

  已经被更改为“222”了。

 d. 添加到暂存区(index)

$ git add 2.txt

  e.查看状态

$ git status
warning: LF will be replaced by CRLF in 2.txt.
The file will have its original line endings in your working directory.
# On branch dev
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   2.txt

 Git提示,使用git reset HEAD 命令使文件去除暂存区。

  f.根据提示,进行reset HEAD 操作

$ git reset HEAD 2.txt
warning: LF will be replaced by CRLF in 2.txt.
The file will have its original line endings in your working directory.
Unstaged changes after reset:
M       2.txt

  g.查看2.txt的内容(还是没有更改回来,再查看状态)

$ cat 2.txt
222

  h.查看状态

$ git status
# On branch dev
# 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:   2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

  i.根据提示进行checkout操作,并查看2.txt内容以及状态

$ git checkout 2.txt

$ cat 2.txt
111

$ git status
# On branch dev
nothing to commit, working directory clean

  可以从上面的操作结果看出来,当已经添加到了暂存区之后,需要进行撤回需要有两步操作,先进行reset HEAD操作,然后再进行checkout操作,就可以回到原始状态了,working directory clean。

       3.直接使用checkout head命令

      这种方法适用与第二种情况:已经提交到暂存区了。(已经add了,但还没有commit操作)

      a.修改内容

$ echo "222" > 2.txt

  b.添加到暂存区

$ git add .

  c.查看状态

$ git status
warning: LF will be replaced by CRLF in 2.txt.
The file will have its original line endings in your working directory.
# On branch dev
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   2.txt

  d.进行checkout head撤回操作

$ git checkout head 2.txt

  e.查看内容以及状态

$ cat 2.txt
111

$ git status
# On branch dev
nothing to commit, working directory clean

  可以从以上过程看出,通过checkout  head命令就可以让文件直接从暂存区撤回到最原始未修改状态,且工作working directory 是干净的。其实第三种方法就是第二种的简洁版。更加的便捷,一条命令直接回到原始状态。

      如果在第二种情况下需要撤回,推荐使用第三种方法。

      其实,Git已经非常友好了,只要看的懂英文,上面都是有提示,教你怎么撤回的。鉴于篇幅问题,我这里第一种撤回方式就先介绍到这里,第二种另起一篇进行讲解。

  

原文地址:https://www.cnblogs.com/bocurry/p/7742259.html