5.1 关于索引的一切
Git的索引不包含任何文件内容,它仅仅追踪你需要提交的那些内容。当执行git commit命令的时候,Git会通过检查索引而不是工作目录来找到提交的内容。
5.2 Git中的文件分类
Git将所有文件分层3类:
已追踪的
:已追踪的文件是指已经在版本库中的文件,或者是已暂存到索引中的文件。git add
被忽略的
:被忽略的文件必须在版本库中被明确声明为不可见或被忽略,即使它可能会在你的工作目录中出现。
未追踪的
:为追踪的文件是指那些不在前两类中的文件。
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest
$ mkdir my_stuff
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest
$ cd my_stuff/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff
$ git init
Initialized empty Git repository in D:/gittest/my_stuff/.git/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ echo "New data" > data
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
data
nothing added to commit but untracked files present (use "git add" to track)
git status报告一个未追踪的文件。
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ touch main.o
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
data
main.o
nothing added to commit but untracked files present (use "git add" to track)
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ echo main.o > .gitignore
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
data
nothing added to commit but untracked files present (use "git add" to track)
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$
为了让Git忽略目录中的文件,只需要将该文件名添加到一个特殊的文件.gitignore中就可以了。
5.3 使用git add
git add命令将暂存一个文件。
可以使用git ls-files命令查看隐藏在对象模型下的东西,并且可以找到那些暂存文件的SHA1值。
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git ls-files --stage
100644 0487f44090ad950f61955271cf0a2d6c6a83ad9a 0 .gitignore
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 data
在任何编辑之后,提交变更之前,请执行git add命令,用最新版本的文件去更新索引。
5.4 使用git commit的一些注意事项
5.4.1 使用git commit –all
git commit的-a或者-all选项会导致执行提交之前自动暂存所有未暂存的和未追踪的文件变化,包括从工作副本中删除已追踪的文件。
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest
$ mkdir commit-all-example
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest
$ cd commit-all-example/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example
$ git init
Initialized empty Git repository in D:/gittest/commit-all-example/.git/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ echo something >> ready
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ echo something else >> notyet
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git add readty notyet
fatal: pathspec 'readty' did not match any files
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git add ready notyet
warning: LF will be replaced by CRLF in notyet.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in ready.
The file will have its original line endings in your working directory.
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git commit -m "Setup"
[master (root-commit) 599023e] Setup
2 files changed, 2 insertions(+)
create mode 100644 notyet
create mode 100644 ready
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git status
On branch master
nothing to commit, working tree clean
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ vim ready
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git add ready
warning: LF will be replaced by CRLF in ready.
The file will have its original line endings in your working directory.
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ vim notyet
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ mkdir subdir
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ echo Nope >> subdir/new
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: ready
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: notyet
Untracked files:
(use "git add <file>..." to include in what will be committed)
subdir/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git commit -all
error: did you mean `--all` (with two dashes ?)
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git commit --all
warning: LF will be replaced by CRLF in notyet.
The file will have its original line endings in your working directory.
[master 5b37342] yes
2 files changed, 2 insertions(+), 2 deletions(-)
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
subdir/
nothing added to commit but untracked files present (use "git add" to track)
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$
5.4.2 编写提交日志信息
5.5 使用rm
git rm命令自然是与git add相反的命令。它会在版本库与工作目录中同时删除文件。
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ echo "Random stuff" >> cops
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git rm cops
fatal: pathspec 'cops' did not match any files
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git add cops
warning: LF will be replaced by CRLF in cops.
The file will have its original line endings in your working directory.
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: cops
Untracked files:
(use "git add <file>..." to include in what will be committed)
subdir/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git ls-files --stage
100644 fcd87b055f261557434fa9956e6ce29433a5cd1c 0 cops
100644 5daf280806b7c3a07dccbe5e8682cd183fa9382a 0 notyet
100644 6fd96237a0210e56c126124dc8e01ba22638687e 0 ready
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git rm --cached cops
rm 'cops'
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git ls-files --stage
100644 5daf280806b7c3a07dccbe5e8682cd183fa9382a 0 notyet
100644 6fd96237a0210e56c126124dc8e01ba22638687e 0 ready
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ ls
cops notyet ready subdir/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$
git rm –cached会删除索引中的文件并把它保留在工作目录中,而git rm则会将文件从索引和工作目录中都删除。
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ ls
cops notyet ready subdir/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git rm ready
rm 'ready'
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: ready
Untracked files:
(use "git add <file>..." to include in what will be committed)
cops
subdir/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ ls
cops notyet subdir/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git add ready
fatal: pathspec 'ready' did not match any files
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git add ready
fatal: pathspec 'ready' did not match any files
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git checkout HEAD -- ready
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ cat ready
something edit
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
cops
subdir/
nothing added to commit but untracked files present (use "git add" to track)
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$ ls
cops notyet ready subdir/
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master)
$
5.6 使用git mv
移动或者重命令文件,可以对旧文件使用git rm命令,然后用git add命令添加新文件,或者可以直接使用git mv命令。
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git mv data mydata
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: mydata
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git commit -m "Moved data to mydata"
[master (root-commit) db61163] Moved data to mydata
2 files changed, 1 insertion(+)
create mode 100644 .gitignore
create mode 100644 mydata
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git log mydata
commit db61163b9d15f3e2f576cab69c34d614bfa9521a
Author: peter <tuziyuxi@gmail.com>
Date: Tue Jul 4 00:01:33 2017 +0800
Moved data to mydata
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$ git log --follow mydata
commit db61163b9d15f3e2f576cab69c34d614bfa9521a
Author: peter <tuziyuxi@gmail.com>
Date: Tue Jul 4 00:01:33 2017 +0800
Moved data to mydata
Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master)
$
5.7 追踪重命名注解
5.8 .gitignore文件
可以将想要忽略的文件的文件名加到同一目录下的.gitignore中即可。此外,可以通过将文件名添加到该版本库顶层目录下的.gitignore文件中来忽略它。
一个.gitignore文件下可以包含一个文件名模式列表,指定哪些文件要忽略。
5.9 Git中对象模型和文件的详细视图