简体   繁体   English

Git:如何撤消提交*和*恢复到最后一个分支

[英]Git: How to Undo commit *and* revert to last branch

Uh oh... I mistakenly committed a pretty complex change (including subdirectory and files renames) without really knowing what I am doing (or what Git would be doing).呃哦......我错误地进行了一个非常复杂的更改(包括子目录和文件重命名),但我并不知道我在做什么(或者 Git 会做什么)。

I now want to undo everything such that:我现在想撤消所有这些:

  1. commit is completely reversed (as if it has never been done, perhaps removing it from history as well)提交被完全逆转(好像它从未完成过,也许也将它从历史中删除)
  2. Restore current working directory (where .git is) to a certain branch (last one will do for now).将当前工作目录( .git所在的位置)恢复到某个分支(现在最后一个)。

I found references to git reset --soft and git reset --hard but I have already proven to myself that I can do real damage by prematurely using a command without fully understanding it.我找到了对git reset --softgit reset --hard的引用,但我已经向自己证明,在不完全理解命令的情况下过早使用命令会造成真正的损害。 :) :)

I found the git reset man page but I am still confused as to:我找到了git 重置手册页,但我仍然对以下内容感到困惑:

  1. What is HEAD ?什么是HEAD
  2. What is the difference between HEAD and * master ? HEAD* master有什么区别?
  3. In my situation (see above) do I need to use --soft , --hard or other (3 more options)?在我的情况下(见上文),我是否需要使用--soft--hard或其他(另外 3 个选项)?
  4. Do I need to run another command (after doing git reset ) to "finalize" the reversal?我是否需要运行另一个命令(在执行git reset之后)来“完成”反转?

UPDATE: After reading the answer below:更新:阅读以下答案后:

  1. Do I understand correctly that all I need to do in my situation is issue a single command git reset --hard HEAD^ ?我是否正确理解在我的情况下我需要做的就是发出一个命令git reset --hard HEAD^
  2. How do I verify that reversal was performed correctly?如何验证是否正确执行了冲销?
  1. HEAD is the latest commit of the checked-out branch. HEAD是签出分支的最新提交。
  2. master is a branch (the main branch, by convention) whereas HEAD is a location in history for the checked-out branch. master是一个分支(按照惯例,主分支),而HEAD是签出分支的历史位置。 HEAD is relative to the branch you are on. HEAD与您所在的分支相关。
  3. git reset --soft will leave your changes in the working tree, uncommitted for you to do whatever you like with. git reset --soft会将您的更改留在工作树中,不承诺您可以随心所欲地做任何事情。 git reset --hard will restore the working tree to the state it was in at the commit you reset to. git reset --hard会将工作树恢复到您重置到的提交时所在的 state。
  4. No other command is needed.不需要其他命令。

First, to keep the commit in case you want to inspect it later, make a branch:首先,要保留提交以防您以后要检查它,请创建一个分支:

git checkout -b my_bad_commit

(or alternatively do git branch my_bad_commit as mentioned in larsman's comment.) (或者做git branch my_bad_commit ,如 larsman 的评论中所述。)

Then return to master or whatever branch you were on and reset:然后返回到master或您所在的任何分支并重置:

git checkout branch_with_bad_commit
git reset --hard HEAD^

HEAD^ translates to "the parent of HEAD," which you can even stack for HEAD^^ = 2 commits back. HEAD^ 转换为“HEAD 的父级”,您甚至可以将其堆叠为 HEAD^^ = 2 次提交。 For more on this topic, check the git community book chapter onundo in git有关此主题的更多信息,请查看 git 中有关撤消的 git 社区书籍章节

  1. HEAD is the tip of the current branch . HEAD当前分支的尖端
  2. The difference between HEAD and master is that HEAD changes when you checkout a branch (or commit). HEADmaster之间的区别在于,当您签出分支(或提交)时HEAD会发生变化。
  3. --soft will leave the changes around, so you can re-add/commit them or undo them by doing git checkout on the changed files. --soft将保留更改,因此您可以通过对更改的文件执行git checkout出来重新添加/提交或撤消它们。 --hard will reset the working area to the state of the commit you are resetting to. --hard会将工作区域重置为您要重置的提交的 state。
  4. Not if you reset --hard .如果您reset --hard则不会。 You might have to git push --force to remote repos (although, if the changes you made are already on a remote, rewriting history is strongly discouraged).您可能需要git push --force到远程存储库(尽管,如果您所做的更改已经在远程,则强烈建议不要重写历史记录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM