美文网首页
git reset --soft --mixed --hard

git reset --soft --mixed --hard

作者: 燕城白夜 | 来源:发表于2019-03-15 14:50 被阅读0次

实战派

在mac下:

mkdir gittest # 创建测试文件夹

cd gittest

git init

touch 1.txt # 创建文件

git add -A

git commit -m "add 1.txt" # 第一次提交

接下里添加一个文件并继续提交

touch 2.txt

git add -A

git commit -m "add 2.txt"

在该版本下添加两个文件一个在缓存区一个在工作区

touch 3.txt

touch 4.txt

git add '3.txt'

然后回退到上一个版本

git reset --hard HEAD~

然后查看status

git status

我们会发现2.txt, 3.txt不见了 4.txt在工作区

--hard总结:对于回退到的版本来说,只会保留当前版本下工作区的文件(注意2.txt也不见了)

我们在当前状态继续

git add -A

git commit -m "add 4.txt"

touch 5.txt

touch 6.txt

git add '5.txt'

git reset --mixed HEAD~

git status

我们发现4.txt 5.txt 6.txt 全部都在工作区

--mixed总结:对于回退到的版本来说,会保留当前版本工作区和缓存区的文件以及版本之间新添加的文件(4.txt)在工作区中,当前版本工作区的文件也会添加到回退到版本的工作区中。

继续将当前工作区的文件提交

git add -A

git commit -m "add 4/5/6.txt"

touch 7.txt

touch 8.txt

git add '7.txt'

git reset --soft HEAD~

查看状态git status

4/5/6/7.txt 都在缓存区,8.txt在工作区

--soft总结:回退到的版本会将当前版本缓存区与版本之间新添加的文件放到回退到版本的缓存区中,当前版本中的工作区中的文件放到回退到版本的工作区中。

这就是为什么我们一般用--hard 就行了

相关文章

网友评论

      本文标题:git reset --soft --mixed --hard

      本文链接:https://www.haomeiwen.com/subject/yaoakqtx.html