shell > cat passwd
root:x:0:0:root:/root:/bin/python
bin:x:1:1:bin:/bin:/sbin/login
daemon:x:2:2:daemon:/sbin:/sbin/login
adm:x:3:4:adm:/var/adm:/sbin/login
lp:x:4:7:lp:/var/spool/lpd:/sbin/login
shell > git log
commit 4cba14306c482080f442043b33f1dd640352a3f9
Author: linuxidc <linux@linuxidc.com>
Date: Tue Jul 21 19:51:30 2015 +0200
three
commit 6fd5803a3fc2d3039ceaf6fc0b9c4fb8f0dfc67a
Author: linuxidc <linux@linuxidc.com>
Date: Tue Jul 21 19:43:17 2015 +0200
two
commit 7af25e23a89ba16e5ff14a8da305542bc9cc6062
Author: linuxidc <linux@linuxidc.com>
Date: Tue Jul 21 18:54:41 2015 +0200
one
## 文件内容、版本信息都回来了,现在的版本恢复到了 three 状态
4、git checkout -- <file> :还记得这个不 ?
shell > sed -i '1d' passwd
shell > git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: passwd
#
no changes added to commit (use "git add" and/or "git commit -a")
## 当修改了工作区文件内容后,使用 git status 指令可以看到这样的提示( git checkout -- <file> 可以撤销工作区文件的修改 )
shell > cat passwd
bin:x:1:1:bin:/bin:/sbin/login
daemon:x:2:2:daemon:/sbin:/sbin/login
adm:x:3:4:adm:/var/adm:/sbin/login
lp:x:4:7:lp:/var/spool/lpd:/sbin/login
shell > git checkout -- passwd
## 使用 git checkout 指令撤销工作区文件的改动( 恢复到跟版本库一样的状态,就是刚才所有的修改全部被撤回 )
shell > sed -i '1d' passwd
shell > git add passwd
shell > sed -i '1d' passwd
shell > git status
shell > git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: passwd
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: passwd
#
## 现在是一个什么情况呢 ?首先删除了文件第一行,执行了 git add 指令( 文件的修改被添加到了暂存区 ),接着又删除了一次,那么现在执行 git checkout 指令文件被恢复成什么样 ?
shell > git checkout -- passwd
## 实践证明:文件被恢复到了暂存区的状态,也就是删除一次的状态
shell > git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: passwd
#
## 这是暂存区的状态,怎么恢复暂存区的状态,也就是把第一次的删除也撤销( 注意:这已经 git add 了 )
shell > git reset HEAD passwd
Unstaged changes after reset:
M passwd
## 还记得 git reset 这个指令不,没错用来恢复文件到上个版本的,也可以用来将暂存区的改动回退到工作区
shell > git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: passwd
#
no changes added to commit (use "git add" and/or "git commit -a")
## 再次查看状态,提示工作区有改动,可以使用 git checkout -- <file> 将工作区的改动也撤销
## git reset 将文件回退到上一次版本 / 将暂存区的改动回退到工作区
## git checkout 将工作区的改动撤销 / 将工作区的改动回退到暂存区
5、删除文件
shell > git rm passwd
rm 'passwd'
shell > git commit -m "delete passwd"
[master 3be8d7a] delete passwd
1 files changed, 0 insertions(+), 5 deletions(-)
delete mode 100644 passwd
## 使用 git rm 指令将版本库中的文件删除,然后提交删除( 文件就被删除了,版本库目录下也没有了 )
shell > git log
commit 3be8d7a3ade6f8947135843832bb6cfa075638a4
Author: linuxidc <linux@linuxidc.com>
Date: Wed Jul 22 13:18:23 2015 +0200
delete passwd
commit 4cba14306c482080f442043b33f1dd640352a3f9
Author: linuxidc <linux@linuxidc.com>
Date: Tue Jul 21 19:51:30 2015 +0200
three
commit 6fd5803a3fc2d3039ceaf6fc0b9c4fb8f0dfc67a
Author: linuxidc <linux@linuxidc.com>
Date: Tue Jul 21 19:43:17 2015 +0200
two
commit 7af25e23a89ba16e5ff14a8da305542bc9cc6062
Author: linuxidc <linux@linuxidc.com>
Date: Tue Jul 21 18:54:41 2015 +0200
one
shell > git reset --hard 4cba14
HEAD is now at 4cba143 three
## ... 好不容易删除了,结果又给恢复了 ^_^ ,不要高兴太早,如果修改完文件,没有提交就删除了文件,那么刚才的修改就真的丢失了..
shell > rm -rf passwd
shell > git reset --hard 4cba14
HEAD is now at 4cba143 three
## 是不是再也不用担心误删除文件了 ^_^ ( 单机情况下你别物理磁盘爆炸了,那就真的啥也没有了 )
Git 教程系列文章: