如何将错误的更改回滚到本地 git 存储库?

发布于 2024-11-02 12:06:10 字数 704 浏览 2 评论 0原文

我是 git 新手,遇到了一个大问题。即如果我做错了什么,如何将存储库恢复到良好状态。我已经了解了如何使用“git revert”进行更改来撤消以前的更改,并且我已经阅读了 stackoverflow 和相关位置上的很多问题,但我还没有找到一种简单的方法来简单地说“将我的本地存储库位恢复到我执行 X 之前的状态,其中 X 可能是提交、合并或其他存储库操作。

长话短说:

今天,当我尝试按照 子树合并。我按照这些步骤设置了一个本地存储库,其中包含一个跟踪远程 Jasmine 存储库的分支。然后我使用 read-tree 将其连接到我的项目中的子目录,一切都很好。不幸的是,Jasmine 分支包含对非公共 URL 的子模块引用,当我尝试修复此问题时,我在跟踪远程存储库的分支上进行了更改(并提交)。无论我做什么来尝试恢复这些提交/更改,我的“git push”命令都无法再工作,因为存储库认为我对 Jasmine 代码进行了更改,并试图通过我正在跟踪的公共 URL 将它们推回。

摘要:我搞砸了我的存储库的一部分,并且不知道如何将其恢复到在我发出由于我的 git 新手状态而导致的命令之前的原始状态。

无论如何,是否可以回滚整个存储库一些提交/更改,或者我只需要再次克隆它并在发生这种情况时重新开始?

I am new to git, and I am running into one big issue. Namely how to revert a repository back to a good state if I do something wrong. I have seen how to use 'git revert' to make changes that undo previous changes, and I have read quite a few of the questions on stackoverflow and related locations, but I haven't found a simple way to simply say "put my local repository bits back to exactly what they were before I did X" where X may be a commit, merge, or other repository operation.

Long Story:

This bit me particularly hard today when I was trying to setup a subtree branch for a third-party dependency (Jasmine) following the steps in the ProGit book on Subtree Merging. I followed the steps and setup a local repository with a branch that tracked a remote Jasmine repository. Then I connected this up using read-tree to a subdirectory in my probject and all was good. Unfortunately the Jasmine branch contained a submodule reference to a non-public URL and when I tried to fix this up I made the changes (and commits) on the branch I had tracking the remote repository. No matter what I did to try to revert these commits/changes, my 'git push' commands couldn't work anymore because the repository thought I had changes to the Jasmine code and was trying to push them back through the public URL I was tracking.

Summary: I screwed up part of my respository and could not figure out how to get it back in the pristine condition it was in before I issued the commands caused by my git newbie status.

Is there anyway to rollback the entire repository a few commits/changes or do I just have to clone it again and start over when this happens?

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

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

发布评论

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

评论(1

找回味觉 2024-11-09 12:06:10

使用 git-reflog

git reflog

找到您想要返回的修订版本

git reset HEAD@{n}

(用正确的数字替换 n)

如果您的工作树不干净,也许之后会使用 git reset --hard ;请务必小心,因为这些将无法恢复)

Use git-reflog

git reflog

Spot the revision that you want to go back to

git reset HEAD@{n}

(replacing n by the proper number)

Perhaps git reset --hard after that if you have unclean work tree; be very careful as those won't be restoreable)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文