十四、Git 取消已缓存 – git reset HEAD

十四、Git 取消已缓存 – git reset HEAD

git reset HEAD 命令用于取消已缓存的内容

我们先将 README 文件内容修改如下

1
2
a
b

hello.php 文件修改为:

1
2
3
4
<?php
echo 'a';
echo 'a';
echo 'a';

然后将两个修改的文件都提交到了缓存区,我们现在要取消其中一个的缓存,操作如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git status -s
M README
M hello.php
$ git add .
$ git status -s
M README
M hello.php
$ git reset HEAD -- hello.php
Unstaged changes after reset:
M hello.php
$ git status -s
M README
M hello.php

现在我们执行 git commit,只会将 README 文件的改动提交,而 hello.php 是没有的

1
2
3
4
5
$ git commit -m '修改'
[master f50cfda] 修改
1 file changed, 1 insertion(+)
$ git status -s
M hello.php

可以看到 hello.php 文件的修改并为提交。

这时我们可以使用以下命令将 hello.php 的修改提交:

1
2
3
4
5
6
$ git commit -am '修改 hello.php 文件'
[master 760f74d] 修改 hello.php 文件
1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working directory clean

简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存