【转】git reset HEAD 与 git reset --hard HEAD的区别
git reset HEAD 与 git reset --hard HEAD的区别
git reset HEAD 是将咱暂存区和HEAD的提交保持一致
git reset --hard HEAD 是将工作区、暂存取和HEAD保持一致
测试 git reset HEAD
1. 创建一个readme.md 的文件
![](https://upload-images.jianshu.io/upload_images/562282-311c8cc50ed8a9c8.png?imageMogr2/auto-orient/strip|imageView2/2/w/910/format/webp)
2 创建一个commit 执行以下命令
git add -A
git commit -m "create readme.md test file"
查看提交
git log --all --graph --oneline
![](https://upload-images.jianshu.io/upload_images/562282-dec78d465a30b0da.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
3 添加暂存取的文本
![](https://upload-images.jianshu.io/upload_images/562282-2c16be6950b0f7cd.png?imageMogr2/auto-orient/strip|imageView2/2/w/564/format/webp)
执行命令,保持到暂存取
git add -A
执行命令,比较暂存取和HEAD的区别
git diff --cached
![](https://upload-images.jianshu.io/upload_images/562282-27457be314bb58ab.png?imageMogr2/auto-orient/strip|imageView2/2/w/846/format/webp)
4 添加工作区的的内容
![](https://upload-images.jianshu.io/upload_images/562282-a340738e27fc0d16.png?imageMogr2/auto-orient/strip|imageView2/2/w/546/format/webp)
执行命令,比较工作区和暂存取的不同
git diff
![](https://upload-images.jianshu.io/upload_images/562282-534c3b21225fb3c4.png?imageMogr2/auto-orient/strip|imageView2/2/w/912/format/webp)
5 执行命令
git reset HEAD
![](https://upload-images.jianshu.io/upload_images/562282-0b155abdebb385dc.png?imageMogr2/auto-orient/strip|imageView2/2/w/1014/format/webp)
比较工作区和暂存区的内容
执行命令
git diif
![](https://upload-images.jianshu.io/upload_images/562282-85a9c830c7d486ec.png?imageMogr2/auto-orient/strip|imageView2/2/w/850/format/webp)
比较暂存取和HEAD的内容
执行命令
git diff --cached
![](https://upload-images.jianshu.io/upload_images/562282-31187396befa5092.png?imageMogr2/auto-orient/strip|imageView2/2/w/642/format/webp)
发现暂区和HEAD没有区别
说明 git reset HEAD 会把暂存区恢复成HEAD
6
执行命令
git add -A
添加到暂存区
执行
git diff --cached
![](https://upload-images.jianshu.io/upload_images/562282-b77c820c4039ae2e.png?imageMogr2/auto-orient/strip|imageView2/2/w/880/format/webp)
发现暂存区添加了两行内容
7
工作区添加一行文字
![](https://upload-images.jianshu.io/upload_images/562282-f9562e4c5568b0af.png?imageMogr2/auto-orient/strip|imageView2/2/w/884/format/webp)
执行
git reset --hard HEAD
![](https://upload-images.jianshu.io/upload_images/562282-5b3bd245d6a27d8e.png?imageMogr2/auto-orient/strip|imageView2/2/w/1148/format/webp)
执行
git diff
git diff --cached
发现工作区、暂存区和HEAD没有区别
![](https://upload-images.jianshu.io/upload_images/562282-2660527b6bfecf67.png?imageMogr2/auto-orient/strip|imageView2/2/w/642/format/webp)
说明执行 git reset --hard HEAD 命令 会将工作区和暂存区恢复成HEAD