本文内容主要来自Progit书籍的Page291 The Workflow部分。
在git使用的时候,需要需要正确的理解git使用的3个区(至于这3个区的翻译各不相同),为了保证表述的准确性,我们直接使用Working Directory, Index 和HEAD来表述3个区。
首先在本地获取远端的代码之后, 我们在本地工作区对文件file进行修改。 修改内容为“This is the first change”。
如果我们需要将修改提交的话,首先需要运行 git add 命令, 然后运行git commit命令。
至于git commit命令的作用,文章中是这样描述的:
Then we run git commit, which takes the contents of the Index and saves it
as a permanent snapshot, creates a commit object which points to that snapshot, and updates master to point to that commit
(git commit的作用基本上是对当前修改的内容创建一个快照,然后将HEAD指针指向这个快照)
这个时候运行git status是没有任何输出的,因为三个区的文件是一样的。
假如这个时候,我们发现刚才的修改不完整, 应该将文件file修改为“This is the right change”
这时候运行git status命令,会看到提示“Changes not staged for commit”。 这是因为Working Directory和Index 区域中文件不一致所致。
这时候我们需要再次运行git add命令,将修改同步到Index区。
这个时候运行git status命令,会看到提示“Changes to be committed”。 这是因为Index 和 HEAD 区域中的文件不一致所致的。
这时候我们需要运行git commit命令,将修改同步到到HEAD 区域。