Git跟踪并管理的是修改,而非文件。你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。
我们对readme.txt进行修改加入一行内容:
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
windowsÏÂgitµİ²װ¡¢ÅäÖÃÓë²Ù×÷
we are young!
然后添加:
$ git add readme.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
然后我们再次修改readme.txt文件:
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
windowsÏÂgitµİ²װ¡¢ÅäÖÃÓë²
we are young!yes we are young!
然后我们提交文件:
$ git commit -m "added we are young"
[master b48f2fc] added we are young
1 file changed, 2 insertions(+), 1 deletion(-)
我们发现第二次的修改并没有提交上去:
$ 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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
查看不同:
diff --git a/readme.txt b/readme.txt
index d5c3ceb..1547332 100644
--- a/readme.txt
+++ b/readme.txt
@@ -2,4 +2,4 @@ Hello World!
this is a git file.
this file will commit to git.
windows<CF><C2>git<B5>İ<B2>װ<A1><A2><C5><E4><D6><C3><D3><EB><B2><D9><D7><F7>
-we are young!
\ No newline at end of file
+we are young!yes we are young!
\ No newline at end of file
你可以继续
git add
再
git commit
,也可以别着急提交第一次修改,先
git add
第二次修改,再
git commit
,就相当于把两次修改合并后一块提交了:
第一次修改 -> git add
-> 第二次修改 -> git add
-> git commit
如果已经修改了工作区文件还没有添加到暂存区。那么可以通过git checkout -- file
丢掉工作区的修改:
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
this is a wrong line.
我们查看文件的状态:
$ 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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
我们发现git已经给与了我们提示。
use "git checkout -- <file>..." to discard changes in working directory
用git checkout指令可以覆盖本地的修改。我们来试一下:
$ git checkout -- readme.txt
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
我们发现错误的一行已经清除了。
如果我们有错误并且已经add到了暂存区去的话。需要另外的操作:
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
this is a wrong line and we added it to git.
$ git add readme.txt
查看文件状态:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
git同样给出了提示。
use "git reset HEAD <file>..." to unstage
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
$ 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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
先使用git reset HEAD将暂存区的文件回退到工作区的状态 然后使用刚才的git checkout删除修改。
$ git checkout -- readme.txt
$ git status
On branch master
nothing to commit, working directory clean
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
这样我们的修改就撤销啦。
如果已经commit到版本库还没有推送到远程。那同样可以使用版本回退。
如果推送到远程就没法补救啦。
文件删除
我们在工作区新建一个文件:
$ git add hello.txt
$ git commit -m "add hello.txt"
[master c1a0bcb] add hello.txt
1 file changed, 2 insertions(+)
create mode 100644 hello.txt
$ cat hello.txt
hello world!
你好 世界!
现在我们删除这个文件:
$ git status
On branch master
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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
然后删除版本库的文件:
$ git rm hello.txt
rm 'hello.txt'
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: hello.txt