您的位置:首页 > 其它

【Git】Git撤销操作、删除文件和恢复文件

2017-04-16 10:59 302 查看
Git在找工作过程中被问到了。

面试官:如果我在git中执行了,rm 111.txt,然后反悔了,又不想删除该文件了,如何恢复。

我:git status可以查看状态,然后根据提示执行相关语句。

哎,回答的感觉不好,在此总结一下吧。

删除文件

rm 删除工作区的文件

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ touch 111.txt

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git add 111.txt

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file:   111.txt

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ rm 111.txt

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file:   111.txt

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted:    111.txt


可以看到,工作区中的 game.js 删除了,但是暂存区中的 game.js 还未删除

删除暂存区中的文件:

git rm 文件名

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git rm 111.txt
rm '111.txt'

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git status
On branch master
nothing to commit, working directory clean


同时删除工作区和暂存区中的文件

git rm -f 文件名

删除暂存区的文件,不删除工作区的文件

git rm –cached 文件名

撤销操作

撤销操作的语法:

git checkout – 文件名

撤销操作一般有两种情况:

文件修改后还没有提交到暂存区,进行撤销操作之后,文件恢复到和版本库中一模一样

文件修改后提交到了暂存区,进行撤销之后,文件恢复到在提交到暂存区之前的状态

现在 111.php中的文件内容是

<?php
echo "hello";
?>


我们修改他的内容为

<?php
echo "HELLO!";
?>


文件未提交到暂存区

用 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:   111.php

no changes added to commit (use "git add" and/or "git commit -a")


可以看到 111.php 修改了,但是还没有提交到暂存库,我们对这个文件进行撤销操作

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git checkout -- 111.php

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$


然后再打开 111.php 文件,就可以发现,它就恢复到了和版本库中一模一样的状态

文件提交到了暂存区,但未提交到版本库

用 git status 命令查看:

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git add 111.php

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified:   111.php

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$


如果这个文件写错了,想要修改,但是它已经提交到了暂存区。所以先需要将它撤销到工作区

git reset HEAD 文件名

写入命令并查看文件状态

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git reset HEAD 111.php
Unstaged changes after reset:
M       111.php

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ 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:   111.php

no changes added to commit (use "git add" and/or "git commit -a")


可以看到,文件已经撤销到了工作区,但是现在文件的内容还没有变化,如果想要恢复到修改之前的状态就使用

git checkout – 文件名

此时,文件就和版本库中的文件一模一样了

恢复文件

如果我们想要恢复某个文件或者整个项目到之前的某个版本,Git提供了方便的操作

可以使用 git log 查看版本库的信息

$ git log
commit 89a454c5f62a7fc1813866c3620816b8decabfac
Author: diligentyang <905407339@qq.com>
Date:   Sun Apr 16 10:51:29 2017 +0800

modify 111.php

commit 943db2230774ef7a5fb2b80ae747eaa2dd29a2c3
Author: diligentyang <905407339@qq.com>
Date:   Sun Apr 16 10:41:05 2017 +0800

add 111.php


如果想要将某个文件恢复到某个版本时的状态,可以使用以下的命令:

git checkout id 文件名

其中的id就是commit的那个序号

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git checkout 943db22 111.php


id可以不用全部复制过来,复制一部分就可以了

如果要将所有的文件都恢复到某个版本,可以使用下面的命令

git reset –hard id

除了用id恢复到某个版本库时的状态,还有别的写法:

//恢复到上一个版本
git reset --hard HEAD^
// 恢复到前两个版本
git reset --hard HEAD~2


另外,可以用 git reflog 查看恢复的记录

diligentyang@DESKTOP-DGRQ07O MINGW64 ~/Desktop/新建文件夹 (master)
$ git reflog
89a454c HEAD@{0}: commit: modify 111.php
943db22 HEAD@{1}: commit: add 111.php
6ffcbb4 HEAD@{2}: commit: step4
ac63d4a HEAD@{3}: commit: step3
8e30348 HEAD@{4}: commit: step2
d11a37d HEAD@{5}: commit (initial): step1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息