欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

第5章 文件管理和索引

发布时间:2023/12/16 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 第5章 文件管理和索引 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

5.1 关于索引的一切
Git的索引不包含任何文件内容,它仅仅追踪你需要提交的那些内容。当执行git commit命令的时候,Git会通过检查索引而不是工作目录来找到提交的内容。

5.2 Git中的文件分类
Git将所有文件分层3类:
已追踪的
:已追踪的文件是指已经在版本库中的文件,或者是已暂存到索引中的文件。git add
被忽略的
:被忽略的文件必须在版本库中被明确声明为不可见或被忽略,即使它可能会在你的工作目录中出现。
未追踪的
:为追踪的文件是指那些不在前两类中的文件。

Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest $ mkdir my_stuffAdministrator@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 masterInitial commitnothing to commit (create/copy files and use "git add" to track)Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ echo "New data" > dataAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ git status On branch masterInitial commitUntracked files:(use "git add <file>..." to include in what will be committed)datanothing added to commit but untracked files present (use "git add" to track)

git status报告一个未追踪的文件。

Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ touch main.oAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ git status On branch masterInitial commitUntracked files:(use "git add <file>..." to include in what will be committed)datamain.onothing added to commit but untracked files present (use "git add" to track)Administrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ echo main.o > .gitignoreAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ git status On branch masterInitial commitUntracked files:(use "git add <file>..." to include in what will be committed).gitignoredatanothing 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-exampleAdministrator@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 >> readyAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ echo something else >> notyetAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ git add readty notyet fatal: pathspec 'readty' did not match any filesAdministrator@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] Setup2 files changed, 2 insertions(+)create mode 100644 notyetcreate mode 100644 readyAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ git status On branch master nothing to commit, working tree cleanAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ vim readyAdministrator@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 notyetAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ mkdir subdirAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ echo Nope >> subdir/newAdministrator@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: readyChanges 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: notyetUntracked 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] yes2 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" >> copsAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ git rm cops fatal: pathspec 'cops' did not match any filesAdministrator@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: copsUntracked 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 readyAdministrator@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 readyAdministrator@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: readyUntracked files:(use "git add <file>..." to include in what will be committed)copssubdir/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 filesAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ git add ready fatal: pathspec 'ready' did not match any filesAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ git checkout HEAD -- readyAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/commit-all-example (master) $ cat ready something editAdministrator@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)copssubdir/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 mydataAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ git status On branch masterInitial commitChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: .gitignorenew file: mydataAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $ git commit -m "Moved data to mydata" [master (root-commit) db61163] Moved data to mydata2 files changed, 1 insertion(+)create mode 100644 .gitignorecreate mode 100644 mydataAdministrator@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 +0800Moved data to mydataAdministrator@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 +0800Moved data to mydataAdministrator@BGUJ9QLXIRFWC3S MINGW32 /d/gittest/my_stuff (master) $

5.7 追踪重命名注解

5.8 .gitignore文件
可以将想要忽略的文件的文件名加到同一目录下的.gitignore中即可。此外,可以通过将文件名添加到该版本库顶层目录下的.gitignore文件中来忽略它。
一个.gitignore文件下可以包含一个文件名模式列表,指定哪些文件要忽略。

5.9 Git中对象模型和文件的详细视图

总结

以上是生活随笔为你收集整理的第5章 文件管理和索引的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。