如何修复恢复的 git 提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5354682/
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 10:24:14  来源:igfitidea点击:

How can I fix a reverted git commit?

gitcommit

提问by mmr

I've committed a bunch of changes to a repository, and they were reverted by someone else (they compile on windows but not on linux). I think that the changes are still in the history, but how can I get those changes back, fix them, and then resubmit?

我已经对存储库进行了大量更改,但它们被其他人恢复(它们在 Windows 上编译,但不在 linux 上编译)。我认为更改仍在历史记录中,但我如何才能恢复这些更改、修复它们,然后重新提交?

回答by Nils Werner

Have you tried reverting the revert?

您是否尝试过还原还原?

They reverted your commit using

他们使用

git revert <your commit id>

The revert itself is a commit and has its own commit id. So now you need to do

还原本身是一个提交,并且有自己的提交 ID。所以现在你需要做

git revert <the commit id of his revert-commit>

回答by Bombe

You can try reverting the reverts, using git revert. You can also restore the files from your commit using git checkout. Or you can use git cherry-pick -nto re-apply them and change them. You can create a new branch from your commit where you apply the changes using git branch. The possibilities are (almost) endless. :)

您可以尝试使用git revert. 您还可以使用git checkout. 或者您可以使用git cherry-pick -n重新应用它们并更改它们。您可以从您的提交创建一个新分支,您可以使用git branch. 可能性(几乎)是无限的。:)

回答by Mark Longair

The other answers here address how to revert or cherry-pick your commits, but it sounds from the question as if you also need to know how to find your commits, so here's some guidance on that.

此处的其他答案解决了如何还原或挑选您的提交,但从问题中听起来好像您还需要知道如何找到您的提交,因此这里有一些指导。

If you just run git logyou should see a list of the commits in the history of your current branch, starting with the most recent. Each commit has an identifier technically known as the object name (or "SHA1sum of the commit") that looks like this:

如果您刚刚运行,git log您应该会看到当前分支历史记录中的提交列表,从最近的开始。每个提交都有一个标识符,技术上称为对象名称(或“提交的 SHA1sum”),如下所示:

commit d2d434beeb03e4ee648ca7ca2a1ea1ed09077306

... followed by the author name, date and a summary of the changes that were introduced by that commit. When you're specifying the object name for git revertor git cherry-pickyou can give it d2d434beeb03e4ee648ca7ca2a1ea1ed09077306or just enough characters from the beginning of the object name to be unambiguous (e.g. d2d434bee)

...后跟作者姓名、日期和该提交引入的更改摘要。当您为git revert或指定对象名称时,git cherry-pick您可以给它d2d434beeb03e4ee648ca7ca2a1ea1ed09077306或从对象名称的开头提供足够的字符以明确(例如d2d434bee

git loghas manyoptions that can help you track down your commits, like --before, -S, etc. but in this case you particularly might need --author=MyLastName.

git log许多选项,可以帮助你跟踪你的提交,如--before-S等,但在这种情况下,你特别可能需要--author=MyLastName

A graphical git tool that can present the history nicely to you and is available on every platform is gitk --all. If you click on a commit that you'd like to revert, you can copy-and-paste its object name from the "SHA1 ID" field in the middle of the window.

一个图形化的 git 工具可以很好地向你展示历史,并且在每个平台上都可用gitk --all。如果单击要还原的提交,则可以从窗口中间的“SHA1 ID”字段中复制并粘贴其对象名称。

回答by Pranav Singh

You can also do like similar situation answer https://stackoverflow.com/a/10415744/704008from @MarkLongair. Answer is better suited for multiple commitsand if you don't want mess in git log or source control historyof revert & then again revert of revert.

您也可以像 @MarkLongair 的类似情况回答https://stackoverflow.com/a/10415744/704008。Answer 更适合多次提交,并且如果您不希望 git log 或源代码控制历史中的 revert & then revert of revert弄得一团糟

git revert abcd123
git revert --no-commit wxyz789
git commit --amend -m"you edit commit message"

You can also have concise in one line for two or more revert commits like :

您还可以在一行中对两个或多个还原提交进行简洁,例如:

git revert abcd123 wxyz789 --no-commit 
git commit --amend -m"you edit commit message"

... and then write an appropriate commit message describing the combined effect of reverting both commits.

...然后编写适当的提交消息,描述恢复两次提交的综合效果。

All this will appear as single commit with given message & not multiple revert commit in git log or above source control logs(like bitbucket/TFS/JIRA git integration).

所有这些都将显示为带有给定消息的单次提交,而不是在 git log 或以上源控制日志(如 bitbucket/TFS/JIRA git 集成)中的多次还原提交。