本博客部分参考https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001374829472990293f16b45df14f35b94b3e8a026220c5000
目录
管理修改
git diff HEAD -- readme.txt
撤销修改
1、只是修改了本地,没有add到暂存区
git checkout -- file
2、add到了暂存区
git reset HEAD
3、已经commit
删除文件
rm 文件名
git rm,并且git commit
本小节是上一篇暂存区的延续
为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件。
你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。
为什么说Git管理的是修改,而不是文件呢?我们还是做实验。第一步,对readme.txt做一个修改,比如加一行内容:
$ 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 add readme.txt
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: readme.txt
#
然后,再修改readme.txt:
$ 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.
提交:
$ git commit -m "git tracks changes"
[master 519219b] git tracks changes
1 file changed, 1 insertion(+)
提交后,再看看状态:
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
正常情况commit提交后的状态是
$ git status
On branch master
nothing to commit, working tree clean
怎么第二次的修改没有被提交?
回顾一下操作过程:
第一次修改 -> git add
-> 第二次修改 -> git commit
你看,我们前面讲了,Git管理的是修改,当你用git add
命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit
只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。
提交后,用git diff HEAD -- readme.txt
命令可以查看工作区和版本库里面最新版本的区别:
git diff HEAD -- readme.txt
$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 76d770f..a9c5755 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,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.
+Git tracks changes of files.
可见,第二次修改确实没有被提交。
那怎么提交第二次修改呢?可以继续git add
再git commit
,也可以别着急提交第一次修改,先git add
第二次修改,再git commit
,就相当于把两次修改合并后一块提交了:
第一次修改 -> git add
-> 第二次修改 -> git add
-> git commit
有三种情况:
自然,你是不会犯错的。不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt
中添加了一行:
$ 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.
在你准备提交前,一杯咖啡起了作用,你猛然发现了stupid boss
可能会让你丢掉这个月的奖金!
既然错误发现得很及时,就可以很容易地纠正它。你可以删掉最后一行,手动把文件恢复到上一个版本的状态。如果用git status
查看一下:
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
你可以发现,Git会告诉你,git checkout -- file
可以丢弃工作区的修改
git checkout -- file
$ git checkout -- readme.txt
命令git checkout -- readme.txt
意思就是,把readme.txt
文件在工作区的修改全部撤销,这里有两种情况:
现在,看看readme.txt
的文件内容:
$ 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.
文件内容果然复原了。
其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
现在假定是凌晨3点,你不但写了一些胡话,还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.
$ git add readme.txt
git reset HEAD
可以把暂存区的修改撤销掉(unstage),重新放回工作区
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD
时,表示最新的版本。
再用git status
查看一下,现在暂存区是干净的,工作区有修改:
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: readme.txt
还记得如何丢弃工作区的修改吗?
$ git checkout -- readme.txt
$ git status
On branch master
nothing to commit, working tree clean
整个世界终于清静了!
只能回退到上一个版本。不过,这是有条件的,就是你还没有把自己的本地版本库推送到远程。还记得Git是分布式版本控制系统吗?我们后面会讲到远程版本库,一旦你把stupid boss
提交推送到远程版本库,你就真的惨了……
ShawndeMacBook-Pro:git_develop shawn$ git reflog
44de6e5 (HEAD -> master) HEAD@{0}: commit: my stupid boss
9eaa624 HEAD@{1}: commit: 第五次 tracks changes of files
4f4fb36 HEAD@{2}: commit: 第四次 git tracks changes
5c1730f HEAD@{3}: commit: understand how stage works
4ed948f HEAD@{4}: reset: moving to 4ed948
6866dfe HEAD@{5}: reset: moving to HEAD^
4ed948f HEAD@{6}: reset: moving to 4ed9
6866dfe HEAD@{7}: reset: moving to HEAD^
4ed948f HEAD@{8}: commit: 第三次修改 append GPL
6866dfe HEAD@{9}: commit: 第二次提交,add distributed
c06cb97 HEAD@{10}: commit (initial): 我的第一次Git提交
ShawndeMacBook-Pro:git_develop shawn$ git reset --hard 9eaa624
HEAD is now at 9eaa624 第五次 tracks changes of files
ShawndeMacBook-Pro:git_develop shawn$ 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.
ShawndeMacBook-Pro:git_develop shawn$ 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.
ShawndeMacBook-Pro:git_develop shawn$ git reflog
9eaa624 (HEAD -> master) HEAD@{0}: reset: moving to 9eaa624
44de6e5 HEAD@{1}: commit: my stupid boss
9eaa624 (HEAD -> master) HEAD@{2}: commit: 第五次 tracks changes of files
4f4fb36 HEAD@{3}: commit: 第四次 git tracks changes
5c1730f HEAD@{4}: commit: understand how stage works
4ed948f HEAD@{5}: reset: moving to 4ed948
6866dfe HEAD@{6}: reset: moving to HEAD^
4ed948f HEAD@{7}: reset: moving to 4ed9
6866dfe HEAD@{8}: reset: moving to HEAD^
4ed948f HEAD@{9}: commit: 第三次修改 append GPL
6866dfe HEAD@{10}: commit: 第二次提交,add distributed
c06cb97 HEAD@{11}: commit (initial): 我的第一次Git提交
此时虽然自己的版本回退了,但reflog里的记录是不能改变的。
在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt
到Git并且提交:
$ git add test.txt
$ git commit -m "add test.txt"
[master b84166e] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm
命令删了:
$ rm test.txt
这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status
命令会立刻告诉你哪些文件被删除了:
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm
删掉,并且git commit
git rm
,并且git commit
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
现在,文件就从版本库中被删除了。
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
$ git checkout -- test.txt
git checkout
其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。