上一个章节已经学会了提交文件,不过还需要再详细解释一下。git add readme.md 是提交这单个文件,git commit 的 -m 是 --messages 的简写。

命令 git add 支持文件通配符,在 git add 之前,怎么知道哪些文件会被提交呢? 那就需要查看 git 仓库的状态。新建一个文件 echo hello > hello.txt 后,输入 git status 看看:

wangbo@wangbo-VirtualBox:~/test/git-demo$ echo hello > hello.txtwangbo@wangbo-VirtualBox:~/test/git-demo$ git statusOn branch masterUntracked files:  (use "git add ..." to include in what will be committed)hello.txtnothing added to commit but untracked files present (use "git add" to track)

提示 Untracked files,看来新建的 hello.txt 文件,没有被纳入跟踪库,这个文件的状态叫未跟踪状态,git 提示我们需要使用 git add 来跟踪它。我们知道 readme.md 文件已经被 git 个跟踪了,我们修改这个文件,git 会怎么处理呢? echo make more changes >> readme.md,同样输入 git status 看看:

wangbo@wangbo-VirtualBox:~/test/git-demo$ echo make more changes >> readme.mdwangbo@wangbo-VirtualBox:~/test/git-demo$ git statusOn branch masterChanges not staged for commit:  (use "git add ..." to update what will be committed)  (use "git restore ..." to discard changes in working directory)modified:   readme.mdUntracked files:  (use "git add ..." to include in what will be committed)hello.txtno changes added to commit (use "git add" and/or "git commit -a")

发现 git 对待 readme.md 的态度和 hello.txt 不一样,git 识别出 readme.md 这个文件已经被修改了,也提示我们使用 git add 继续操作,如果有很多文件的时候我们挨个 git add 单个文件点累人,因为 . 表示当前目录,输入 git add . 可以添加所有文件,操作后再次输入 git status 看看 git 仓库的状态:

5d55ceed246629e37e4b34a171b524f2.png

这两个文件目前都提示可以 unstage 了,说明它们目前是 staged 状态,这个文件状态叫暂存状态,Changes to be committed 说明这些 changes 还没有被提交 (commit),输入 git commit 回车,发现系统打开了 nano 让我们填写提交说明,并且下面列出了所有要提交的文件和它的状态。

994297748b988c7e675902c5fb710578.png

git commit -m 'xxx' 直接在命令行写简短信息的时候很方便,git commit 回车对于写分段的提交信息更方便,问题是我不希望用 nano 来操作想用 vim 怎么办呢? 直接 ctrl +x 退出 nano 编辑器, 在 ~/.gitconfig 配置中增加:

[core]  editor = vim

再次 git commit 回车发现这次是 vim 编辑器了。

88f24d5f444840cad78c60670eb66a48.png

wq 保存以后,git 提示如下:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git commit[master d9a13af] say hello and update readme 2 files changed, 2 insertions(+) create mode 100644 hello.txt

再次输入 git status 后发现 git 没有什么可以提交的,也就是当前工作目录里的文件没有变化:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git statusOn branch masternothing to commit, working tree clean

当使用 git add . 的时候会暂存所有的文件,但工作中我们有一些文件,比如写代码的时候产生的临时文件,或者工作目录中一些测试数据文件,还有一些 IDE 工具的配置文件,我们并不想提交到 git 仓库中,怎么办呢? git 说可以,你把这些文件放到工作目录的 .gitignore 文件就可以了。我们新建一个 tmp.txt 文件,输入 git status 后:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git statusOn branch masterUntracked files:  (use "git add ..." to include in what will be committed)tmp.txtnothing added to commit but untracked files present (use "git add" to track)

git 提示这个文件未跟踪,但其实我们根本就不想把文件纳入版本库管理,也即是这个文件的内容变化历史我们不关心,但是作为工作的一部分,这个文件又必须存在在当前目录里,这时候就可以把它在 .gitignore 文件注册一下,告诉 git 这个文件你就别管了。echo tmp.txt >> .gitignore 后输入 git status 看看:

wangbo@wangbo-VirtualBox:~/test/git-demo$ echo tmp.txt >> .gitignorewangbo@wangbo-VirtualBox:~/test/git-demo$ git statusOn branch masterUntracked files:  (use "git add ..." to include in what will be committed).gitignorenothing added to commit but untracked files present (use "git add" to track)

git 现在提示 .gitignore 没有被跟踪,但是没有再提示 tmp.txt 了,说明 git 已经知道了 tmp.txt 是不需要跟踪的,已经忽略它了。但 .gitignore 这个文件本身我们希望把它纳入跟踪,因为我们以后可能还需要添加更多的忽略文件并频繁的修改它。现在可以放心的执行 git add . 来添加了:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git add .wangbo@wangbo-VirtualBox:~/test/git-demo$ git commit -m 'add igno[master 1e5fee4] add ignore 1 file changed, 1 insertion(+) create mode 100644 .gitignore

思考一个问题,如果 tmp.txt 已经被纳入版本库了,怎么告诉 git 不要再跟踪它呢?

wangbo@wangbo-VirtualBox:~/test/git-demo$ git lg* bba4235 - (HEAD -> master) add tmp (2 seconds ago) * d9a13af - say hello and update readme (19 minutes ago) * 3e8d68a - make changes (74 minutes ago) * a0ae41e - add readme file (84 minutes ago) 

这时候 tmp.txt 已经被跟踪了,在 .gitignore 里添加它已经晚了,还要多做一步,告诉 git 先删除它,执行:

wangbo@wangbo-VirtualBox:~/test/git-demo$ echo tmp.txt > .gitignorewangbo@wangbo-VirtualBox:~/test/git-demo$ git rm tmp.txt --cachedrm 'tmp.txt'wangbo@wangbo-VirtualBox:~/test/git-demo$ git statusOn branch masterChanges to be committed:  (use "git restore --staged ..." to unstage)deleted:    tmp.txtUntracked files:  (use "git add ..." to include in what will be committed).gitignorewangbo@wangbo-VirtualBox:~/test/git-demo$ git add .wangbo@wangbo-VirtualBox:~/test/git-demo$ git commit -m 'ignore tmp'[master d5d999f] ignore tmp 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 .gitignore delete mode 100644 tmp.txt

我们再次对 tmp.txt 文件进行修改,发现 git 已经对它不感冒了。

wangbo@wangbo-VirtualBox:~/test/git-demo$ echo more changes >> tmp.txtwangbo@wangbo-VirtualBox:~/test/git-demo$ git statusOn branch masternothing to commit, working tree clean

让 git 删除一个跟踪中的文件,为什么需要 git rm tmp.txt --cached 呢? 这个 --cached 参数的意思是告诉 git 把 tmp.txt 这个文件从版本仓库中拿掉,但是不要删除工作目录中的 tmp.txt 文件,如果执行 git rm tmp.txt,工作目录中的 tmp.txt 也会被删除掉,这不是我们期望的,我们只是希望 git 不要再跟踪它,但这个 tmp.txt 文件在程序运行的时候还是需要保留的。

小结一下:

1. git commit 回车可以更方便的写提交信息,可以在 ~/.gitconfig 中定义要使用的编辑器

2. git status 用于查看工作目录中文件的状态,修改了内容和执行了 git 命令都可以运行确认一下,git status -s 可以看简洁状态

3. .gitignore 是用来告诉 git 不要跟踪的文件列表,它其实是一行一个文件申明,并支持 ? * ! 等通配符,当通配符有冲突的时候顺序就很重要

4. 如果一个文件已经被 git 跟踪了,想要忽略除了在 .gitignore 文件中添加申明外,还需要执行 git rm 文件 --cached,--cached 的意思是在工作目录中,保留文件不要彻底删除它

5. git add . 可以添加工作目录中所有的文件,不用挨个去添加了,但是不会添加在 .gitignore 中的文件,试试 git add 也可以使用通配符

下一章我们详细看看 git 的提交日志。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐