文章永久连接:https://tech.souyunku.com/?p=5235
git reset HEAD 命令用于取消已缓存的内容
我们先将 README 文件内容修改如下
搜云库技术团队 (tech.souyunku.com)
搜云库技术团队,教程
hello.php 文件修改为:
<?php
echo '教程 :tech.souyunku.com';
echo '教程 :tech.souyunku.com';
echo '教程 :tech.souyunku.com';
然后将两个修改的文件都提交到了缓存区,我们现在要取消其中一个的缓存,操作如下:
$ 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 添加,但不希望包含在下一提交快照中的缓存
干货推荐
附录:Git:系列文章
- 一、Git 基础教程
- 二、Git 安装 – Linux
- 三、Git 安装 – Window
- 四、Git 安装 – Mac OS
- 五、Git 配置 – git config
- 六、Git 工作流程
- 七、Git 工作区、暂存区和版本库
- 八、Git 创建仓库 – git init
- 九、Git 添加文件到暂存区- git add
- 十、Git 提交文件到版本库 – git commit
- 十一、Git 克隆仓库 – git clone
- 十二、Git 删除文件 – git rm
- 十三、Git 重命名文件 – git mv
- 【当前读到】十四、Git 取消已缓存 – git reset HEAD
- 十五、Git 分支管理 – git branch
- 十六、Git 查看提交历史 – git log
- 十七、Git 标签 – git tag
- 十八、Git 远程仓库 ( Github )
- 十九、Git 远程服务搭建
- 二十、Git 快速参考手册
- 二十一、Git 基本操作
- 二十二、Git Flow