目 录CONTENT

文章目录
Git

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

云少
2022-06-02 / 0 评论 / 0 点赞 / 109 阅读 / 301 字
温馨提示:
本文最后更新于 2022-06-02,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

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

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

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

a
b

hello.php 文件修改为:

<?php
echo 'a';
echo 'a';
echo 'a';

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

$ 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 是没有的

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

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

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

$ 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 添加,但不希望包含在下一提交快照中的缓存

0
广告 广告

评论区