Git缓冲区

一直没仔细研究下git,它的分布式原理与svn的集中式管理还是有很大的不同的,小动手操作了两下

先创建一个仓库

lihui@MacBook  ~/work   master ●✚  mkdir git
lihui@MacBook  ~/work   master ●✚  cd git
lihui@MacBook  ~/work/git   master ●✚  ls
lihui@MacBook  ~/work/git   master ●✚  git init
Initialized empty Git repository in /Users/lihui/work/git/.git/

从最下面的说明可以看出,创建了一个.git目录,应该是跟.svn一个作用,跟踪用的

创建一个文件,然后通过status查看,由于没添加到版本库,所以未知文件

lihui@MacBook  ~/work/git   master  cat hello
2016
 lihui@MacBook  ~/work/git   master 
 lihui@MacBook  ~/work/git   master 
 lihui@MacBook  ~/work/git   master  git status
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

	hello

nothing added to commit but untracked files present (use "git add" to track)

将它添加到版本库,但add是不会同步到版本库的,只是会添加到缓冲区中

lihui@MacBook  ~/work/git   master  git add hello
 lihui@MacBook  ~/work/git   master ✚  git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

	new file:   hello

此时git缓冲区和你本地工作区关于hello这个文件内容应该是一致的,可以通过git diff来查看

lihui@MacBook  ~/work/git   master ✚  git diff
lihui@MacBook  ~/work/git   master ✚ 

commit一下,推到版本库里

lihui@MacBook  ~/work/git   master ✚  git commit -m "add new file hello, written 2016"
[master (root-commit) afbcacd] add new file hello, written 2016
 Committer: lihui
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 hello
 lihui@MacBook  ~/work/git   master 
 lihui@MacBook  ~/work/git   master 
 lihui@MacBook  ~/work/git   master  git status
On branch master
nothing to commit, working directory clean

此时再将本地工作区的hello文件内容修改下

lihui@MacBook  ~/work/git   master ●  cat hello
2016 04 17
 lihui@MacBook  ~/work/git   master ●  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:   hello

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

这里status显示已经本地已经有修改了,通过git diff查看

diff --git a/hello b/hello
index 6f6d045..4036a1e 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-2016
+2016 04 17
(END)

可见,由于新修改的hello还没有add,所以git缓冲区里还是2016;而本地工作区的hello里添加了04 17

这里还有另一种diff,用来查看git仓库里和git缓冲区的diff,如下

lihui@MacBook  ~/work/git   master ●  git diff --cached

这时候diff的结果只有(END),也就是此时仓库里和缓冲区里是一致的

接着将最新修改推送到缓冲区里

lihui@MacBook  ~/work/git   master ●  git add hello
lihui@MacBook  ~/work/git   master ✚  git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	modified:   hello

通过git diff来查看缓冲区和工作区的差别,可以发现两者一致,原因是add了

(END)

通过git diff –cached查看仓库里和缓冲区的差别,可以发现仓库还是老的,缓冲区已经更新了,原因是还没commit

diff --git a/hello b/hello
index 6f6d045..4036a1e 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-2016
+2016 04 17
(END)

最后,commit一下最新的修改

lihui@MacBook  ~/work/git   master ✚  git commit -m "modify hello, about add month and day"
[master 391256e] modify hello, about add month and day
 Committer: lihui
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)
 lihui@MacBook  ~/work/git   master  git status
On branch master
nothing to commit, working directory clean

不论是git diff还是git diff –cached,结果都会是

(END)

可以通过log查看修改记录

commit 391256e4053349b985c84dce7078e6db512ea964
Author: lihui
Date:   Sun Apr 17 16:28:44 2016 +0800

    modify hello, about add month and day

commit afbcacd3ad2bed8ba77c65dc4890e126c1335c53
Author: lihui
Date:   Sun Apr 17 15:56:21 2016 +0800

    add new file hello, written 2016
(END)

 最后留下网友的购物车理论:

海绵他奶
Created at 2015-12-4 18:11, Last updated at 2015-12-4 18:11
我觉得嘛,暂存区就像购物车,没到付款的时候你都不确定购物车里的东西全部都是要的。。。每拿一件商品就付一次款。。。那才麻烦大了

发表回复