'git reset --hard HEAD〜1'和'git reset --soft HEAD〜1'有什么区别? [英] What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'?

查看:297
本文介绍了'git reset --hard HEAD〜1'和'git reset --soft HEAD〜1'有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图撤消我在git中的提交。使用 git reset --hard HEAD〜1



有什么区别? git reset



是否还有其他方法可以撤销Sourcetree Gui的提交?

解决方案

git reset 确实知道五种模式:软,混合,保持。我会讨论前三项,因为这些是你通常使用的。

soft



code> git reset --soft HEAD〜1 您将撤销上次提交,但文件更改将保留在您的工作树中。此外,这些更改将保留在您的索引中,因此,使用 git commit 后,将创建一个与您之前删除的提交完全相同的更改的提交。



混合



这是默认模式,与soft非常相似。当使用 git reset HEAD〜1 删除一个提交时,您仍然会保留工作树中的更改,但不会保留在索引上;所以如果你想重做提交,你必须在提交前添加更改( git add )。



hard



当使用 git reset --hard HEAD〜1 时,您将失去在上次提交。这些更改不会留在工作树中,因此执行 git status 命令会告诉您,您的存储库中没有任何更改。






您可以在 git reset文档



注意

当执行 git reset 删除提交,提交并没有真正丢失,只是没有引用指向它或它的任何子节点。你仍然可以通过使用 git reflog 或类似的命令找到一个带有 git reset 的已删除提交。它是SHA-1键。


I tried to undo my commit in git. Is it dangerous to use git reset --hard HEAD~1?

What is the difference between different options for git reset?

Is there also any other way to undo a commit by Sourcetree Gui?

解决方案

git reset does know five "modes": soft, mixed, hard, merge and keep. I will discuss the first three, since those are the ones you usually use.

soft

When using git reset --soft HEAD~1 you will undo the last commit, but the file changes will stay in your working tree. Also the changes will stay on your index, so following with a git commit will create a commit with the exact same changes as the commit you "removed" before.

mixed

This is the default mode and quite similar to soft. When "removing" a commit with git reset HEAD~1 you will still keep the changes in your working tree but not on the index; so if you want to "redo" the commit, you will have to add the changes (git add) before commiting.

hard

When using git reset --hard HEAD~1 you will lose the changes introduced in the last commit. The changes won't stay in your working tree so doing a git status command will tell you that you don't have any changes in your repository.


You can read more about that in the git reset documentation.

Note
When doing git reset to remove a commit the commit isn't really lost, there just is no reference pointing to it or any of it's children. You can still recover a "deleted" commit with git reset, for example by using git reflog or similar commands to find it's SHA-1 key.

这篇关于'git reset --hard HEAD〜1'和'git reset --soft HEAD〜1'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆