Git - Git教程 05:工作区 | 暂存区
前言
1 - Git 和其他版本控制系统如 SVN 的一个不同之处就是有暂存区的概念
工作区 Working Directory
1 - 就是你在电脑里能看到的目录,比如我的 GitLearning 文件夹就是一个工作区
版本库 Repository
1 - 工作区中有一个隐藏目录 .git,这个不算工作区,而是 Git 的版本库
2 - Git 的版本库里存了很多东西,其中最重要的就是称为 stage(或者叫 index)的暂存区;还有 Git 为我们自动创建的第一个分支 master,以及指向 master 的指针 HEAD
我们把文件往 Git 版本库里添加的时候,是分两步执行的
第一步是用 git add 把文件添加进去,实际上就是把文件修改添加到暂存区
第二步是用 git commit 提交更改,实际上就是把暂存区的所有内容提交到当前分支:我们在创建 Git 版本库时,Git 自动为我们创建了唯一一个 master 分支,就是说 git commit 是往 master 分支上提交更改
3 - 继续在 file_D.txt 文件中添加新的内容 We've come a long way from where we began. 。并在 GitLearning 目录下添加 LICENSE.txt 文件,初始内容 hello,Git!
1 $ git status 2 On branch master 3 Changes not staged for commit: 4 (use "git add <file>..." to update what will be committed) 5 (use "git checkout -- <file>..." to discard changes in working directory) 6 7 modified: file_D.txt 8 9 Untracked files: 10 (use "git add <file>..." to include in what will be committed) 11 12 LICENSE.txt 13 14 no changes added to commit (use "git add" and/or "git commit -a")
Git 非常清楚地告诉我们:file_D.txt 被修改了;而且 LICENSE.txt 还从来没有被添加过,所以它的状态是 Untracked
4 - 使用 git add 命令天添加 file_D.txt 和 LICENSE.txt 文件;之后运行 git status
1 $ git add file_D.txt LICENSE.txt 2 $ git status 3 On branch master 4 Changes to be committed: 5 (use "git reset HEAD <file>..." to unstage) 6 7 new file: LICENSE.txt 8 modified: file_D.txt
现在 file_D.txt 和 LICENSE.txt 两文件通过 git add 命令,就被添加到了暂存区
git add 命令实际上就是把要提交的所有修改放到暂存区
5 - 执行 git commit 就可以一次性把暂存区的所有修改提交到分支!此时的暂存区中就没有任何内容了
1 $ git commit -m "understand stage works" 2 [master 90dfe9d] understand stage works 3 2 files changed, 2 insertions(+), 1 deletion(-) 4 create mode 100644 LICENSE.txt
6 - 提交完成后,如果你没有在工作区做任何修改,那么工作区就是干净的
1 $ git status 2 On branch master 3 nothing to commit, working tree clean