回滚到公共存储库中旧的 Git 提交
我如何才能回滚到 git 中的特定提交?
有人能给我的最好答案是使用 git revert X 次,直到达到所需的提交。
假设我想恢复到 20 次提交之前的提交,我必须运行它 20 次。
有没有更简单的方法来做到这一点?
我无法使用重置,因为该存储库是公共的。
How can I go about rolling back to a specific commit in git?
The best answer someone could give me was to use git revert
X times until I reach the desired commit.
So let's say I want to revert back to a commit that's 20 commits old, I'd have to run it 20 times.
Is there an easier way to do this?
I can't use reset because this repository is public.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
试试这个:
其中
[revision]
是提交哈希(例如:12345678901234567890123456789012345678ab
)。不要忘记最后的
.
,非常重要。这会将更改应用于整个树。您应该在 git 项目根目录中执行此命令。如果您位于任何子目录中,则此命令仅更改当前目录中的文件。然后承诺,你应该会很好。您可以通过
删除工作目录和暂存区域中的所有修改来撤消此操作。
Try this:
where
[revision]
is the commit hash (for example:12345678901234567890123456789012345678ab
).Don't forget the
.
at the end, very important. This will apply changes to the whole tree. You should execute this command in the git project root. If you are in any sub directory, then this command only changes the files in the current directory. Then commit and you should be good.You can undo this by
that will delete all modifications from the working directory and staging area.
回滚到特定提交:
回滚 10 次提交:
如果您不想重写历史记录,可以使用“git revert”,如下面的文章所示
如何将 Git 存储库恢复到以前的提交?
To rollback to a specific commit:
To rollback 10 commits back:
You can use "git revert" as in the following post if you don't want to rewrite the history
How to revert Git repository to a previous commit?
好吧,我想问题是,“回滚”是什么意思?如果您无法
重置
,因为它是公开的,并且您希望保持提交历史记录完整,您的意思是您只想让您的工作副本反映特定的提交吗?使用 git checkout 和提交哈希。编辑:正如评论中所指出的,使用 git checkout 而不指定分支将使您处于“无分支”状态。使用 git checkout; -b检出到分支,或
git checkout.
检出到当前分支。Well, I guess the question is, what do you mean by 'roll back'? If you can't
reset
because it's public and you want to keep the commit history intact, do you mean you just want your working copy to reflect a specific commit? Usegit checkout
and the commit hash.Edit: As was pointed out in the comments, using
git checkout
without specifying a branch will leave you in a "no branch" state. Usegit checkout <commit> -b <branchname>
to checkout into a branch, orgit checkout <commit> .
to checkout into the current branch.原海报写道:
没有必要使用 git revert X 次。
git revert
可以接受提交范围作为参数,因此您只需使用它一次即可恢复范围
例如,如果您想恢复最近 20 次提交:
提交范围
HEAD~20..
是HEAD~20..HEAD
的缩写>,表示“从 HEAD 提交的第 20 个父级开始,并将其之后的所有提交恢复到 HEAD”。这将恢复最后 20 次提交,假设这些提交都没有合并
如果存在合并提交,那么您无法在一个命令中将它们全部还原,您需要使用
单独还原它们。另请注意,我已经使用带有 < 的范围进行了测试。 code>git revert 使用 git 版本 1.9.0。 如果您使用的是较旧版本的 git,则使用
git revert
的范围可能有效,也可能无效。在这种情况下,
git revert
优于git checkout
。请注意,与这个答案说使用
git checkout
不同,git恢复
实际上会删除您正在提交的任何提交中添加的所有文件
恢复,这使得这是恢复一系列修订的正确方法。
文档
The original poster states:
It's not necessary to use
git revert
X times.git revert
can accept acommit range as an argument, so you only need to use it once to revert a range
of commits. For example, if you want to revert the last 20 commits:
The commit range
HEAD~20..
is short forHEAD~20..HEAD
, and means "start from the 20th parent of the HEAD commit, and revert all commits after it up to HEAD".That will revert that last 20 commits, assuming that none of those are merge
commits. If there are merge commits, then you cannot revert them all in one command, you'll need to revert them individually with
Note also that I've tested using a range with
git revert
using git version 1.9.0. If you're using an older version of git, using a range withgit revert
may or may not work.In this case,
git revert
is preferred overgit checkout
.Note that unlike this answer that says to use
git checkout
,git revert
will actually remove any files that were added in any of the commits that you're
reverting, which makes this the correct way to revert a range of revisions.
Documentation
第 1 步:获取提交列表:
您将获得如下例所示的列表:
第 2 步:复制所需的提交哈希并将其粘贴以进行结帐:
就这样.
Step 1: fetch list of commits:
You'll get list like in this example:
Step 2: copy needed commit hash and paste it for checkout:
That's all.
想要 HEAD 分离模式吗?
如果您希望使用 DETACHED HEAD 将 X 时间回滚到某个提交(意味着您不能弄乱任何东西),那么请务必使用以下命令:(
将 X 替换为您希望返回的提交数量)
IE返回一个提交:
Want HEAD detached mode?
If you wish to rollback X time to a certain commit with a DETACHED HEAD (meaning you can't mess up anything), then by all means, use the following:
(replace X with how many commits you wish to go back)
I.E. to go back one commit:
会做的。这是“git checkout”,但没有更新 HEAD。
则可以达到相同的效果
如果您更喜欢将便捷命令串在一起,
。这些使您的工作树和索引处于所需的状态,您只需
git commit
即可完成。will do it. It's "git checkout" but without updating HEAD.
You can achieve the same effect with
if you prefer stringing convenience commands together.
These leave you with your worktree and index in the desired state, you can just
git commit
to finish.我不确定发生了什么变化,但如果没有选项
--detach
,我无法签出特定提交。对我有用的完整命令是:git checkout --detach [commit hash]
要从分离状态返回,我必须签出我的本地分支:
git checkout master
I'm not sure what changed, but I am unable to checkout a specific commit without the option
--detach
. The full command that worked for me was:git checkout --detach [commit hash]
To get back from the detached state I had to checkout my local branch:
git checkout master
假设您在完成一个项目大约一天后。您注意到一项功能仍然给您带来错误。但您不知道您所做的更改导致了错误。所以你必须收集以前的工作提交。恢复到特定提交:
好的,所以该提交适合您。不再有错误。你指出了问题所在。现在您可以返回到最新的提交:
并在导致错误之前签出特定文件(在我的例子中,我使用示例 Gemfile.lock):
这是处理您在提交中创建的错误的一种方法,直到稍后才意识到错误。
Let's say you work on a project and after a day or so. You notice one feature is still giving you errors. But you do not know what change you made that caused the error. So you have to fish previous working commits. To revert to a specific commit:
Ok, so that commit works for you. No more error. You pinpointed the issue. Now you can go back to latest commit:
And checkout a specific file before it caused the error (in my case I use example Gemfile.lock):
And this is one way to handle errors you created in commits without realizing the errors until later.
您可以在 GitHub/BitBucket/Gitlab 的提交部分找到与每个提交相关的提交 ID。
它非常简单,假设您的提交 ID 是 5889575,那么如果您想返回到代码中的这一部分,那么您只需键入
This will take you to that time point in your code。
You can find the commit id related to each commit in the commits section of GitHub/BitBucket/Gitlab.
Its very simple, suppose your commit id is 5889575 then if you want to go back to this part in your code then you simply need to type
This will take you to that point of time in your code.
这是一个执行此操作的示例
Here is an example to do that
回滚 git 中的特定提交:
ac2ec...
是提交名称)现在该分支具有过去的提交项。例如,如果您愿意,可以将其合并到母版中。
有关 git head 以及回滚到旧版本的更多信息:点击此处
to rollback a specific commit in git:
ac2ec...
is the commit name)now that branch has the past commit items. you can merge it to the master for example if you would like to.
for more info about git head, and rollback to old version: click here
在 Github 上找到您所做的提交并签出它。
示例:
git checkout b29ce12
on the Github find the commit you made and checkout to it.
example:
git checkout b29ce12