'git reset --hard HEAD~1' 和 'git reset --soft HEAD~1' 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24568936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:16:14  来源:igfitidea点击:

What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'?

gitundogit-reset

提问by mesutali

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

我试图在 git 中撤消我的提交。使用有危险git reset --hard HEAD~1吗?

What is the difference between different options for git reset?

不同选项之间有什么区别git reset

回答by Sascha Wolf

git resetdoes know five "modes": soft, mixed, hard, merge and keep. I will start with the first three, since these are the modes you'll usually encounter. After that you'll find a nice little a bonus, so stay tuned.

git reset确实知道五种“模式”:软、混合、硬、合并和保持。我将从前三个开始,因为这些是您通常会遇到的模式。之后你会发现一个不错的小奖励,敬请期待。

soft

柔软的

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

使用时,git reset --soft HEAD~1您将从当前分支中删除最后一次提交,但文件更改将保留在您的工作树中。此外,这些更改将保留在您的索引中,因此使用 agit commit将创建一个与您之前“删除”的提交具有完全相同的更改的提交。

mixed

混合的

This is the default mode and quite similar to soft. When "removing" a commit with git reset HEAD~1you 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.

这是默认模式,与 soft 非常相似。与git reset HEAD~1您一起“删除”提交时,仍会保留工作树中的更改,但不会保留在索引上;因此,如果您想“重做”提交,则必须在提交git add之前添加更改 ( )。

hard

难的

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

使用时,除了上次提交中引入的更改之外,git reset --hard HEAD~1您还将丢失所有未提交的更改。更改不会保留在您的工作树中,因此执行git status命令会告诉您您的存储库中没有任何更改。

Tread carefully with this one. If you accidentally remove uncommited changes which were never tracked by git(speak: committed or at least added to the index), you have no way of getting them back using git.

小心踩着这个。如果您不小心删除了从未被跟踪的未提交更改git(说:已提交或至少添加到索引中),您将无法使用git.

Bonus

奖金

keep

保持

git reset --keep HEAD~1is an interesting and useful one. It only resets the files which are different between the currentHEADand the given commit. It aborts the reset if anyone of these files has uncommited changes. It's basically acts as a safer version of hard.

git reset --keep HEAD~1是一个有趣和有用的。它只重置当前HEAD提交和给定提交之间不同的文件。如果这些文件中的任何一个有未提交的更改,它就会中止重置。它基本上充当hard.

This mode is particularly useful when you have a bunch of changes and want to switch to a different branch without losing these changes - for example when you started to work on the wrong branch.

当您有大量更改并希望在不丢失这些更改的情况下切换到不同的分支时,此模式特别有用 - 例如,当您开始在错误的分支上工作时。



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

您可以在git reset 文档中阅读更多相关信息。

Note
When doing git resetto 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 commit which was "deleted" with git resetby finding it's SHA-1 key, for example with a command such as git reflog.

注意
git reset删除提交时,提交并没有真正丢失,只是没有指向它或其任何子项的引用。您仍然可以git reset通过找到它的 SHA-1 密钥来恢复被“删除”的提交,例如使用诸如git reflog.

回答by FamiliarPie

This is a useful article which graphically shows the explanation of the reset command.

这是一篇有用的文章,它以图形方式显示了重置命令的解释。

https://git-scm.com/docs/git-reset

https://git-scm.com/docs/git-reset

Reset --hard can be quite dangerous as it overwrites your working copy without checking, so if you haven't commited the file at all, it is gone.

Reset --hard 可能非常危险,因为它会在不检查的情况下覆盖您的工作副本,因此如果您根本没有提交文件,它就会消失。

As for Source tree, there is no way I know of to undo commits. It would most likely use reset under the covers anyway

至于源树,我知道无法撤消提交。无论如何,它很可能会在幕后使用重置

回答by apadana

Git reset has 5 main modes: soft, mixed, merged, hard, keep. The difference between them is to change or not change head, stage (index), working directory.

Git 重置有 5 种主要模式:软、混合、合并、硬、保持。它们之间的区别是改变或不改变head, stage (index), working directory

Git reset --hard will change head, index and working directory.
Git reset --soft will change head only. No change to index, working directory.

Git reset --hard 将改变头、索引和工作目录。
Git reset --soft 只会改变头部。索引、工作目录没有变化。

So in other words if you want to undo your commit, --soft should be good enough. But after that you still have the changes from bad commit in your index and working directory. You can modify the files, fix them, add them to index and commit again.

所以换句话说,如果你想撤销你的提交, --soft 应该足够好。但是在那之后,您的索引和工作目录中仍然有来自错误提交的更改。您可以修改文件,修复它们,将它们添加到索引并再次提交。

With the --hard, you completely get a clean slate in your project. As if there hasn't been any change from the last commit. If you are sure this is what you want then move forward. But once you do this, you'll lose your last commit completely. (Note: there are still ways to recover the lost commit).

使用--hard,您的项目将完全干净利落。就好像上次提交没有任何变化一样。如果您确定这是您想要的,那么继续前进。但是一旦你这样做,你将完全失去你的最后一次提交。(注意:仍有办法恢复丢失的提交)。

回答by Alexandr

This is the main difference between use git reset --hardand git reset --soft:

这是 use git reset --hardgit reset --soft之间的主要区别

--soft

Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

根本不接触索引文件或工作树(但将头部重置为 ,就像所有模式一样)。这会留下所有更改的文件“要提交的更改”,正如 git status 所说的那样。

--hard

Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

重置索引和工作树。此后对工作树中跟踪文件的任何更改都将被丢弃。