Foreword
First, we must first know what is the workspace (Working Directory) and the suspension zone (Postponed Zone).
Workspace (Working Directory):
As the name suggests, the work area. Our computer is a directory for each project workspace.
Suspend Area (Postponed Zone):
Where is suspended in the area? We workspace .git
This hidden directory is our postpone area
Git and other version control systems (SVN) difference is that the concept of the temporary area. No authority to say that the suspension is a district, we often say Git Git repository or warehouse. The Git repository saved a lot of things, most important of which is called stage
the temporary area, and the first branch Git automatically created for us master
, and points to master
a pointer HEAD
.
Add submitted (fully reflects the core concepts)
回顾一下过程:工作区 >>>> 暂存区 >>>> 仓库
When I create a new Git repository, Git automatically creates a master
branch. When I was git commit
affirmative to master
change presented at the branch, no doubt about it.
First, create a test.txt file in the work area, immediately git status
view the status:
$ touch test.txt
$ git status
On branch master
nothing to commit, working tree clean
From the results, the first line On branch master
prompt us in which branch, is currently in master
. The second line nothing to commit, working tree clean
means that we currently do not have any commit operation.
Second, the new piece of content in test.txt file immediately git status
view the status:
$ vim test.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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
从结果来看,第一句还是提示我们处在哪个分支。第二行表示我们已经有了操作,第五行已经给出提示,而这个操作就发生在 test.txt
文件,最后一行提示 我们 没有要添加提交的更改,Git 一直监视这工作区文件的修改,接下来我们添加提交。
三、将 test.txt 添加提交到暂缓区,紧接着 git status
查看状态:
$ git add test.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: test.txt
从结果来看,modified:test.txt
从红色变为绿色字体,这表示改动的文件已经放入暂缓区,此时文件是 “安全” 的,下图表示了 test.txt
文件的所在位置:
在暂缓区中,test.txt
能被拿出来吗?能!
四、从暂缓区中移除 test.txt 文件,紧接着 git status
查看状态:
$ git rm --cached test.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: test.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
从结果上不难看出,test.txt
文件已被 Git 列为不跟踪的文件了。
五、把暂缓区的所有文件提交到当前分支 master
:
注意,如果你是跟着本文一步步做的,那么执行以下命令前,先执行 git add
把 test.txt
添加到暂存区,因为第四步我们给从暂缓区移除了。
$ git commit -m "This is a demo!"
[master d5cbf24] This is a demo!
1 file changed, 1 insertion(+)
从结果可以看到,Git 完成了提交,并提示我们一些信息。
此时 test.txt
就跑到了 master
分支:
以上便是 工作区 >>>> 暂存区 >>>> 仓库
整个过程。
注意事项
每次 git commit
提交后,你又没有对工作区做任何修改的话,此时暂缓区是 干净 的:
$ git status
On branch master
nothing to commit, working tree clean
也就是这个样子:
写在后面
以下简单明了的分析来自 九只蜗牛Leo 分享:
Git managed file is divided into: workspace, the repository, the repository is divided into stage staging area and staging area branch master (warehouse)
git the Add File >>>> the staging area from the work area, git commit to file >>>> warehouse from scratch,
git diff to see the difference the work area and staging area,
git diff --cached see the staging area and warehouse difference,
git diff to see the difference the HEAD work area and warehouse,
git reverse the Add command git checkout, undo the work area changes, the latest version of the staging area that is transferred to the work area,
git reverse the commit command git reset HEAD, the latest version is to be transferred to the warehouse staging area.