如何在git中回滚到特定的提交?
有人能给我的最好答案是使用git恢复X次,直到我达到所需的提交。
假设我想要恢复到20次提交前的提交,我需要运行20次。
有更简单的方法吗?
我不能使用重置,因为这个存储库是公共的。
如何在git中回滚到特定的提交?
有人能给我的最好答案是使用git恢复X次,直到我达到所需的提交。
假设我想要恢复到20次提交前的提交,我需要运行20次。
有更简单的方法吗?
我不能使用重置,因为这个存储库是公共的。
当前回答
第一步:获取提交列表:
git log
你会得到如下所示的列表:
[Comp:Folder User$ git log
commit 54b11d42e12dc6e9f070a8b5095a4492216d5320
Author: author <author@gmail.com>
Date: Fri Jul 8 23:42:22 2016 +0300
This is last commit message
commit fd6cb176297acca4dbc69d15d6b7f78a2463482f
Author: author <author@gmail.com>
Date: Fri Jun 24 20:20:24 2016 +0300
This is previous commit message
commit ab0de062136da650ffc27cfb57febac8efb84b8d
Author: author <author@gmail.com>
Date: Thu Jun 23 00:41:55 2016 +0300
This is previous previous commit message
...
步骤2:复制所需的提交散列并粘贴以供检查:
git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f
这是所有。
其他回答
假设你在做一个项目,过了一天左右。您注意到有一个特性仍然出错。但是你不知道你做了什么改变导致了错误。所以你必须找出以前的工作提交。恢复到特定的提交:
git checkout 8a0fe5191b7dfc6a81833bfb61220d7204e6b0a9 .
好的,这个commit对你有用。不会再有错误了。你指出了问题所在。现在你可以回到最近的提交:
git checkout 792d9294f652d753514dc2033a04d742decb82a5 .
在导致错误之前签出一个特定的文件(在我的例子中,我使用Gemfile.lock):
git checkout 8a0fe5191b7dfc6a81833bfb61220d7204e6b0a9 -- /projects/myproject/Gemfile.lock
这是一种处理在提交中创建的错误的方法,直到以后才会意识到这些错误。
回退到特定的提交:
git reset --hard commit_sha
回滚10次提交:
git reset --hard HEAD~10
如果你不想重写历史,你可以像下面的帖子一样使用“git revert”
如何将Git存储库恢复到以前的提交?
第一步:获取提交列表:
git log
你会得到如下所示的列表:
[Comp:Folder User$ git log
commit 54b11d42e12dc6e9f070a8b5095a4492216d5320
Author: author <author@gmail.com>
Date: Fri Jul 8 23:42:22 2016 +0300
This is last commit message
commit fd6cb176297acca4dbc69d15d6b7f78a2463482f
Author: author <author@gmail.com>
Date: Fri Jun 24 20:20:24 2016 +0300
This is previous commit message
commit ab0de062136da650ffc27cfb57febac8efb84b8d
Author: author <author@gmail.com>
Date: Thu Jun 23 00:41:55 2016 +0300
This is previous previous commit message
...
步骤2:复制所需的提交散列并粘贴以供检查:
git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f
这是所有。
这里有一个例子
cd /yourprojects/project-acme
git checkout efc11170c78 .
你可以在GitHub/BitBucket/Gitlab的提交部分找到与每个提交相关的提交id。 这很简单,假设你的提交id是5889575,那么如果你想回到代码中的这一部分,那么你只需要输入
git checkout 5889575 .
这将带您到代码中的那个时间点。