状态简述
git status
命令的输出十分详细,但其用语有些繁琐git status -s
或git status --short
,输出结果更加简洁
git status -s M markers.py M test.py D test_func01.py D test_login.py ?? text.txt
忽略文件
https://www.cnblogs.com/poloyy/p/12342073.html
查看已暂存和未暂存的修改
修改 README 文件后暂存,然后编辑 CONTRIBUTING.md
文件后先不暂存, 运行 status
命令将会看到:
$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README 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: CONTRIBUTING.md
初次使用 git diff
要查看尚未暂存的文件更新了哪些部分,不加参数直接输入 git diff
:
$ git diff diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebb991..643e24f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,8 @@ branch directly, things can get messy. Please include a nice description of your changes when you submit your PR; if we have to read the whole diff to figure out why you're contributing in the first place, you're less likely to get feedback and have your change -merged in. +merged in. Also, split your changes into comprehensive chunks if your patch is +longer than a dozen lines. If you are starting to work on a particular area, feel free to submit a PR that highlights your work in progress (and note in the PR title that it's
实际栗子
git diff 的作用
- 比较的是当前文件和暂存区快照之间的差异,也就是修改之后还没有暂存起来的变化内容
- 若要查看已暂存且要添加到下次提交里的内容,可以用
git diff --staged
命令 - 会比较已暂存文件与最后一次提交的文件差异
$ git diff --staged diff --git a/README b/README new file mode 100644 index 0000000..03902a1 --- /dev/null +++ b/README @@ -0,0 +1 @@ +My Project
git diff
git diff 本身只显示尚未暂存的改动,而不是自上次提交以来所做的所有改动,所以有时候一下子暂存了所有更新过的文件,运行 git diff
后却什么也没有
像之前说的,暂存 CONTRIBUTING.md
后再编辑,可以使用 git status
查看已被暂存的修改或未被暂存的修改。 如果我们的环境(终端输出)看起来如下:
$ git add CONTRIBUTING.md $ echo '# test line' >> CONTRIBUTING.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: CONTRIBUTING.md 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: CONTRIBUTING.md
现在运行 git diff
看暂存前后的变化:
$ git diff diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 643e24f..87f08c8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -119,3 +119,4 @@ at the ## Starter Projects See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md). +# test line
然后用 git diff --cached
查看已经暂存起来的变化( --staged
和 --cached
是同义词):
$ git diff --cached diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebb991..643e24f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,8 @@ branch directly, things can get messy. Please include a nice description of your changes when you submit your PR; if we have to read the whole diff to figure out why you're contributing in the first place, you're less likely to get feedback and have your change -merged in. +merged in. Also, split your changes into comprehensive chunks if your patch is +longer than a dozen lines. If you are starting to work on a particular area, feel free to submit a PR that highlights your work in progress (and note in the PR title that it's
实际栗子
提交更新
- 现在的暂存区已经准备就绪,可以提交了
- 在此之前,请务必确认还有什么已修改或新建的文件还没有
git add
过, 否则提交的时候不会记录这些尚未暂存的变化 - 这些已修改但未暂存的文件只会保留在本地磁盘
- 所以,每次准备提交前,先用
git status
看下,你所需要的文件是不是都已暂存起来了, 然后再运行提交命令git commit
:
git commit -m "test"
可以获取到的内容
- commit 之后 git status 可以看到本地是干净的
- 提交后会显示当前是在哪个分支(
master
)提交的 - 本次提交的完整 SHA-1 校验和是什么(
9a8c6b3
) - 以及在本次提交中,有多少文件修订过,多少行添加和删改过
重点
- 提交时(git commit)记录的是放在暂存区域的快照
- 每一次运行提交操作,都是对项目作一次快照,以后可以回到指定快照版本,或者进行比较
跳过使用暂存区域
- Git 提供了一个跳过使用暂存区域的方式
git commit
加上-a
选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过git add
步骤
-a
选项使本次提交包含了所有修改过的文件,但是要小心,有时这个选项会将不需要的文件添加到提交中
移除文件
两种情况
- 直接删除文件(从本地删除):-f 参数
- 只是将文件从 Git 的暂存区移除,并不会删除本地文件:--cached
直接看栗子
已跟踪的文件(出现在暂存区)的文件被执行 git rm --cached 命令后,它会被移出暂存区,重新变成一个未跟踪的文件
移动文件
在 Git 中对文件改名
$ git mv file_from file_to
查看状态信息,也会看到关于重命名操作的说明
文件会自动提交到暂存区,且不需要手动执行 git add
其实,运行 git mv
就相当于运行了下面三条命令
$ mv README.md README $ git rm README.md $ git add README