Git比SVN优秀的另一个原因是:Git跟踪并管理的是修改,而非文件。
那么什么是修改?新增了一行,是修改,删除了一行,也是修改,更改了某些字符,是修改,删了一些又加了一些,也是修改,甚至创建一个新文件,也算修改。
1. 如果你做了修改,但是没有先执行 git add,而是直接执行了git commit,那会怎样?
还是做个实验来验证一下吧。
(1)修改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.
(2)$ git add readme.txt
(3)$ git status
On branch master
Changes to be committed:
(use "git reset HEAD..." to unstage)
modified: readme.txt
(4)再次修改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.
(5)$ git commit -m "git tracks changes"
[master 29b3a6f] git tracks changes
1 file changed, 1 insertion(+)
(6)$ 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")
咦,怎么第二次的修改没有被提交?
(7)对比工作区和版本库中的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.
可见,第二次修改确实没有被提交。
(8)$ git add readme.txt
(9)$ git commit -m "git tracks changes of file"
2. 撤销修改
(1)修改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.
(2)在你准备提交前,你发现了“stupid boss”这一句并不合适,你想回到修改之前的版本。
(3)$ 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 可以丢弃工作区的修改:
(4)$ git checkout -- readme.txt
readme.txt 自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
注意,git checkout -- file 命令中的--很重要,没有--,就变成了“创建一个新分支”的命令。
(5)如果你做完了第(1)步,并且执行了 $ git add readme.txt
(6)$ git status
On branch master
Changes to be committed:
(use "git reset HEAD..." to unstage)
modified: readme.txt
从输出中,Git告诉我们可以使用 $ git reset HEAD... 可以暂存区的修改撤销。
这里的HEAD表示当前最新的版本。
(7)$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
(8)$ 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")
现在暂存区是干净的,工作区有修改。
(9)$ git checkout -- readme.txt
现在撤销工作区的修改
(10)$ git status
On branch master
nothing to commit, working directory clean
整个世界终于清静了!
再进一步,假设你执行了 git add 和 git commit操作,现在后悔了,应该怎么办呢?
还记得怎么做回退操作吗?
注意回退的前提是你没有推送到远程库。
3. 删除与撤销删除
(1)新添加一个文件:test.txt,并提交。
$ git add test.txt
$ git commit -m "add test.txt"
[master e963d7c] add test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
(2)$ rm test.txt
这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:
(3)$ 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:
(4)$ git rm test.txt
(5)$ git commit -m "remove test.txt"
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
(6)$ git checkout -- test.txt
那么什么是修改?新增了一行,是修改,删除了一行,也是修改,更改了某些字符,是修改,删了一些又加了一些,也是修改,甚至创建一个新文件,也算修改。
1. 如果你做了修改,但是没有先执行 git add,而是直接执行了git commit,那会怎样?
还是做个实验来验证一下吧。
(1)修改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.
(2)$ git add readme.txt
(3)$ git status
On branch master
Changes to be committed:
(use "git reset HEAD
modified: readme.txt
(4)再次修改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.
(5)$ git commit -m "git tracks changes"
[master 29b3a6f] git tracks changes
1 file changed, 1 insertion(+)
(6)$ git status
On branch master
Changes not staged for commit:
(use "git add
(use "git checkout --
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
咦,怎么第二次的修改没有被提交?
(7)对比工作区和版本库中的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.
可见,第二次修改确实没有被提交。
(8)$ git add readme.txt
(9)$ git commit -m "git tracks changes of file"
2. 撤销修改
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.
(2)在你准备提交前,你发现了“stupid boss”这一句并不合适,你想回到修改之前的版本。
(3)$ git status
On branch master
Changes not staged for commit:
(use "git add
(use "git checkout --
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
从输出中,Git告诉我们可以使用 $ git checkout -- file 可以丢弃工作区的修改:
(4)$ git checkout -- readme.txt
readme.txt 自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
注意,git checkout -- file 命令中的--很重要,没有--,就变成了“创建一个新分支”的命令。
(5)如果你做完了第(1)步,并且执行了 $ git add readme.txt
(6)$ git status
On branch master
Changes to be committed:
(use "git reset HEAD
modified: readme.txt
从输出中,Git告诉我们可以使用 $ git reset HEAD
这里的HEAD表示当前最新的版本。
(7)$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
(8)$ git status
On branch master
Changes not staged for commit:
(use "git add
(use "git checkout --
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
现在暂存区是干净的,工作区有修改。
(9)$ git checkout -- readme.txt
现在撤销工作区的修改
(10)$ git status
On branch master
nothing to commit, working directory clean
整个世界终于清静了!
再进一步,假设你执行了 git add 和 git commit操作,现在后悔了,应该怎么办呢?
还记得怎么做回退操作吗?
注意回退的前提是你没有推送到远程库。
3. 删除与撤销删除
(1)新添加一个文件:test.txt,并提交。
$ git add test.txt
$ git commit -m "add test.txt"
[master e963d7c] add test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
(2)$ rm test.txt
这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:
(3)$ git status
On branch master
Changes not staged for commit:
(use "git add/rm
(use "git checkout --
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:
(4)$ git rm test.txt
(5)$ git commit -m "remove test.txt"
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
(6)$ git checkout -- test.txt
没有评论:
发表评论