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

发布于 2024-11-19 05:48:55 字数 980 浏览 2 评论 0原文

呃哦...我错误地犯了一个相当复杂的更改(包括子目录和文件重命名),而没有真正知道我在做什么(或者 Git 会做什么)。

我现在想撤消一切,例如:

  1. 提交完全相反(就好像 也许从来没有做过 也将其从历史记录中删除)
  2. 恢复当前工作目录 (其中 .git 是)到某个 分支(现在用最后一个分支即可)。

我找到了对 git reset --softgit但是我已经向自己证明,在不完全理解命令的情况下过早使用命令会造成真正的损害。 :)

我找到了 git Reset 手册页 但我仍然很困惑:

  1. 什么是HEAD吗?
  2. 有什么区别 HEAD* master
  3. 在我的情况下(见上文)我是否 需要使用 --soft--hard 或 其他(还有 3 个选项)?
  4. 我需要运行另一个命令吗 (在执行git重置之后) “最终”逆转?

更新:阅读以下答案后:

  1. 我是否正确理解了我所做的一切 在我的情况下需要做的是问题 单个命令 git reset --hard 头^?
  2. 我如何验证逆转已发生 执行正确吗?

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).

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).

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. :)

I found the git reset man page but I am still confused as to:

  1. What is HEAD?
  2. What is the difference between
    HEAD and * master?
  3. In my situation (see above) do I
    need to use --soft, --hard or
    other (3 more options)?
  4. Do I need to run another command
    (after doing git reset) to
    "finalize" the reversal?

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^
    ?
  2. How do I verify that reversal was
    performed correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-11-26 05:48:55
  1. HEAD 是签出分支的最新提交。
  2. master 是一个分支(按照惯例,主分支),而 HEAD 是签出分支的历史位置。 HEAD 与您所在的分支相关。
  3. git reset --soft 会将您的更改保留在工作树中,不让您做任何您喜欢的事情。 git reset --hard 会将工作树恢复到您重置到的提交时的状态。
  4. 不需要其他命令。

首先,为了保留提交以供稍后检查,请创建一个分支:(

git checkout -b my_bad_commit

或者按照 larsman 的评论中提到的那样执行 gitbranch my_bad_commit 。)

然后返回到 master 或您所在的任何分支并重置:

git checkout branch_with_bad_commit
git reset --hard HEAD^

HEAD^ 翻译为“HEAD 的父级”,您甚至可以堆叠 HEAD^^ = 2 次提交。有关此主题的更多信息,请查看 git 社区书籍中关于git 中的撤消的章节

  1. HEAD is the latest commit of the checked-out branch.
  2. master is a branch (the main branch, by convention) whereas HEAD is a location in history for the checked-out branch. HEAD is relative to the branch you are on.
  3. git reset --soft will leave your changes in the working tree, uncommitted for you to do whatever you like with. git reset --hard will restore the working tree to the state it was in at the commit you reset to.
  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.)

Then return to master or whatever branch you were on and reset:

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. For more on this topic, check the git community book chapter on undo in git

无语# 2024-11-26 05:48:55
  1. HEAD当前分支的提示
  2. HEADmaster 之间的区别在于,HEAD 在您签出分支(或提交)时发生变化。
  3. --soft 将保留更改,因此您可以通过对更改的文件执行 git checkout 来重新添加/提交它们或撤消它们。 --hard 会将工作区域重置为您要重置的提交状态。
  4. 如果您reset --hard,则不会。您可能必须 git push --force 到远程存储库(不过,如果您所做的更改已经在远程上,则强烈建议不要重写历史记录)。
  1. HEAD is the tip of the current branch.
  2. The difference between HEAD and master is that HEAD changes when you checkout a branch (or commit).
  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. --hard will reset the working area to the state of the commit you are resetting to.
  4. Not if you 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).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文