根据文件在 Git中 的状态,可将其内部分为三个工作区域:
如果拿 Git 来管理项目的源代码,那工作目录就是一个Workspace。其中的源代码文件可根据其是否纳入Git的管理流程分为三类:
1.1)Untracked:未纳入Git管理流程的
1.2)Tracked:已纳入Git管理流程的
对于已纳入Git管理流程的可以再分为三类:
a)已修改,未暂存的 (Changes not staged for commit)
b)已修改,已暂存的,未提交的 (Changes to be committed)
c)已提交,未修改的 (commited)
1.3)不纳入Git管理流程的(在.gitignore文件中定义)
对于未纳入Git管理流程的文件和已纳入Git管理流程中处于 a)状态的文件,可以使用add命令来将其添加到暂存区域:
git add a.file此时,a.file就会被保存到暂存区域。
3)本地仓库
git commit -m "Commit Staged files"指定 -m 参数,可以后面添加对提交内容的注释信息。
如果不指定 -m 参数,则 Git 会跳转到一个vi输入界面,等待用户输入注释信息之后,再提交。
1)先利用git status查看一下当前的工作目录状态:
$ git status
# On branch master
nothing to commit (working directory clean)
2)添加一个文件 test.txt,再查看一下其状态,如下:
$ touch test.txt$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
nothing added to commit but untracked files present (use "git add" to track)
可看到,在Untracked files:下面存在一个test.txt文件,而最下面一行则提示没有文件需要提交,但是存在未跟踪的文件(未跟踪,也即未纳入Git管理流程),此时 test.txt 文件就是处于未纳入管理流程的状态。
$ echo "Add content to end of the a.txt" >> a.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: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
no changes added to commit (use "git add" and/or "git commit -a")
可以看到,对于a.txt文件,其已纳入Git管理流程,所以其显示的是Changes not staged for commit,即上面所述的状态 a)。
对于未纳入Git管理流程的文件, add 命令可将其纳入Git管理流程,同时将其添加到暂存区域。
对于已纳入Git管理流程的文件,add 命令可将修改过的文件添加到暂存区域。
切记,只有添加到暂存区域的文件,才能够提交到本地仓库。
$ git add test.txt$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: test.txt
#
# 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: a.txt
#
利用add命令,将test.txt文件添加到暂存区域(Changes to be commited),可看到test.txt已经是处于暂存区域,等待提交了,其状态就是上述状态中的 b)状态了。
$ git add a.txt$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
# new file: test.txt
#
可看到,上面两个文件都被加入到暂存区域了。
git commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
# new file: test.txt
当用户输入提交信息之后,保存在暂存区的内容就会被保存到本地的仓库了。
$ git commit
[master d15838d] Commit the changes
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test.txt
可看到,在提交成功之后,Git会计算出一个Hash值(如上面,最前面几位是:d15838d),作为这次提交的标识值,可利用git log来查看。
$ git commit -m "Add new files"(0)工作目录中,创建文件。
(1)编辑文件,利用 add 添加进暂存区域。
(2)利用 commit 将暂存区域的文件保存到本地仓库。
(3)编辑文件,利用 add 将其添加进暂存区域,回到(2)步,如此循环。
$ git commit -am "Add and then Commit"但是对于未纳入Git管理流程的文件,则不能这么做,必须先显式使用add命令将其纳入Git管理流程,保存进暂存区域,再提交。
$ git rm b.txt
rm 'b.txt'
查看其状态:
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: b.txt
#
同样的,这次删除操作也会被保存进暂存区域,直接提交了,才是真正从本地仓库中删除了,如下:
$ git commit -m "delete b.txt"
[master 2daa294] delete b.txt
0 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 b.txt而此时,b.txt文件也从工作目录中被删除了。
$ ls
a.txt c.txt d.txt test.txt
$ git rm --cached d.txt
rm 'd.txt'
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: d.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# d.txt
提交之后,再看一下Git状态:
$ git commit -m "remove d.txt from git"
[master d625705] remove d.txt from git
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 d.txt$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# d.txt
nothing added to commit but untracked files present (use "git add" to track)
最后,还可以使用log命令来查看多次的提交信息,如下:
$ git log
commit d62570527d545ae2677708ac2f9b015aab2df86f
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:31:10 2014 +0800
remove d.txt from git
commit 2daa294a500bf9b3b0af7fe586353d882f0c3592
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:27:50 2014 +0800
delete b.txt
commit d15838d91e2c09e5e25a0a348e4dfb27c7e6b928
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:18:28 2014 +0800
Commit the changes
commit b52882cca685b024991dec8b976c35a1cbc6c9cf
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:11:13 2014 +0800
结束。
6266



被折叠的 条评论
为什么被折叠?



