如何检索 git 存储库中丢失的更改?

发布于 2025-01-03 13:32:38 字数 353 浏览 6 评论 0原文

我在一个小工作室工作,刚开始使用 Git,所以我们这里没有 Git 大师。最近,一位开发人员在推动一个小更改的过程中成功地从代码库中完全删除了一堆其他更改。

看起来当他合并时,他合并了一些旧版本,其中一个缺少添加的文件,并且有旧版本的文件。因此,当他将提交推送到服务器时,最终结果是删除了文件,并且文件的内容现在丢失了许多更改。

现在,如果您克隆一个新的仓库,则主分支中的所有代码都会丢失下图中圈出的所有更改。这些更改的提交仍在日志中,只是它们所代表的更改不在代码中。

基本上,问题可以在这张图片中看到:

The issues

I work at a small studio which is new to using Git, so we have no Git masters here. Recently, a developer managed to, in the process of pushing a small change, completely remove a bunch of other changes from the codebase.

It looks like when he merged, he merged with some old revision, one which was missing added files, and had old versions of files. So when he pushed his commit up to the server, the end result is deleted files, and files whose contents are now missing many changes.

Now, if you clone a new depot, all the code in the main branch is missing all the circled changes in the below image. The commits for those changes are still in the log, only the changes they represented are not in the code.

Basically, the problem can be seen in this image:

The problems

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

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

发布评论

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

评论(3

兔小萌 2025-01-10 13:32:38

最简单的方法是 git revert合并提交并使开发人员这次正确地重做合并。

git revert 将记录一个新的提交,该提交会撤消 的影响,这应该是您错误的合并提交。

由于此更改已被推送到中央存储库,因此您不应该使用 git 的任何重写历史记录魔法。

The easiest way would be to git revert the merge commit and make the developer redo the merge, correctly this time.

git revert <sha> will record a new commit that undoes the effects of <sha>, which should be your bad merge commit.

Since this change has been pushed to the central repository already, you shouldn't use any of git's rewrite-history magic.

与他有关 2025-01-10 13:32:38

几天前我遇到了同样的情况。

可能的原因

我发现这种合并的可能原因可能是有人

git merge -s ours <somebranch>

在不知情的情况下使用。

一些开发人员不使用控制台,而是使用他们的 IDE 进行拉/提交/推送。当出现问题时,一些 IDE 会提供诸如“强制”、“解决我的问题”等选项……除非人们确切知道它的作用,否则并不总是应该单击这些选项。

糟糕的是,在这样的合并提交之后,推送 git 会告诉你合并分支的所有提交都在那里,但是你不会发现任何这些更改......悲伤的故事。

解决方案

当一切正常时,我们决定与重置大师一起回到最新的点。

git checkout master
git reset --hard
git reset --hard <OKAY_POINT>

在您的情况下,3221961

然后我们不得不强行把它推到上游。

git push --force

结果,origin/master 被重置为发生错误合并之前的状态。

一般来说,这不是一个好主意。但我们还是同意了,因为所有团队都是本地人,大多数人都知道发生了什么,并且在强制重置发生的那一刻,没有人在那个破碎的主人之上做出任何事情。

之后,破坏了 master 的开发人员必须重新进行合并,这一次是以正确的方式进行的。

我看到的唯一不重置远程的其他选项是手动重新合并提交可能已“删除”的所有内容。这对我来说似乎并不简单。

Couple days ago I ran into same situation.

Possible cause

I've found out that possible cause for such merges could be someone using

git merge -s ours <somebranch>

without even knowing that.

Some developers don't use console and pull/commit/push using their IDEs. When something goes bad, some IDEs present options like "force", "resolve to mine", etc... which are not always should be clicked unless one exactly knows what it does.

Bad thing is that after such a merge commit pushed git will tell you that all the commits of merged branch are in there, however you won't find any of those changes.... Sad story.

Solution to go

We decided to go with the resetting master back to the latest point when everything was OK.

git checkout master
git reset --hard
git reset --hard <OKAY_POINT>

In your case <OKAY_POINT> is 3221961.

And then we had to forcefully push this to the upstream.

git push --force

As a result origin/master was reset to the state before that bad merge happened.

Generally it is NOT a good idea. But we went with it since all the team is local, most guys were aware of what's happening and no-one committed anything on top of that broken master at the moment forced back reset was happening.

Afterwards the developer who broke the master had to redo his merge and this time the right way.

The only other option without resetting remote I see is to manually re-merge everything that commit might have "erased". Which does not seem like something simple to me.

故笙诉离歌 2025-01-10 13:32:38

我不确定你在问什么,但你几乎总是可以使用 git reflog 取回意外删除的内容

I'm not sure what you are asking, but you can almost always use git reflog to get back something that was accidently removed

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