你工作目录下的每一个文件只有两种状态:tracked 或 untracked
可以用 git status
命令查看哪些文件处于什么状态。 如果在克隆仓库后立即使用此命令,会看到类似这样的输出:
git status On branch master Your branch is up-to-date with ‘origin/master‘. nothing to commit, working directory clean
在项目下创建一个新的 README
文件,使用 git status
命令,将看到一个新的 untracked 文件
echo ‘My Project‘ > README $ git status On branch master Your branch is up-to-date with ‘origin/master‘. Untracked files: (use "git add <file>..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track)
README
文件出现在 Untracked files
下面
git add 文件名
# 更新目录下所有文件
git add .
后面会再详细讲这个命令
使用命令 git add
开始跟踪一个文件。 所以,要跟踪 README
文件,运行:
git add README
此时再运行 git status
命令,会看到 README
文件已被跟踪,并处于 staged
git status On branch master Your branch is up-to-date with ‘origin/master‘. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: README
Changes to be committed
这行下面的,就说明是 staged 状态git add
时的版本将被留存在后续的历史记录中
如果修改了一个名为 CONTRIBUTING.md
的已被 tracked 的文件,然后运行 git 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) new file: 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
CONTRIBUTING.md
出现在 Changes not staged for commit
这行下面,说明已跟踪文件的内容发生了变化,但还没有放到暂存区git add
命令
运行 git add
将“CONTRIBUTING.md”放到暂存区,然后再看看 git status
的输出:
$ git add 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) new file: README modified: CONTRIBUTING.md
编辑 CONTRIBUTING.md
,再运行 git status
看看
$ vim 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) new file: README 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
CONTRIBUTING.md
文件同时出现在暂存区和非暂存区git add
命令时的版本CONTRIBUTING.md
的版本是你最后一次运行 git add
命令时的那个版本,而不是当前版本git add
之后又编辑了文件,需要再次 git add
把最新版本暂存$ git add 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) new file: README modified: CONTRIBUTING.md
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
修改 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 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 --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
后却什么也没有
像之前说的,暂存 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"
master
)提交的9a8c6b3
)
git commit
加上 -a
选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add
步骤-a
选项使本次提交包含了所有修改过的文件,但是要小心,有时这个选项会将不需要的文件添加到提交中
两种情况
直接看栗子
已跟踪的文件(出现在暂存区)的文件被执行 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
原文:https://www.cnblogs.com/poloyy/p/14769935.html