简体   繁体   English

获取之前在git仓库上删除的提交

[英]get previous commit that got deleted on git repository

Yesterday when I was at work, I was working on my project that i have on my home computer. 昨天,当我在工作时,我正在处理家用计算机上的项目。 The problem was when I tried cloning the repo it eclipse would crash. 问题是当我尝试克隆仓库时,Eclipse会崩溃。 So instead I downloaded it manually and initialized the repo and then force pushed to my home computer. 因此,我改为手动下载它并初始化存储库,然后将其强行推入我的家用计算机。 This deleted all the history and files in on the repo. 这将删除存储库中的所有历史记录和文件。 I did some googling and tried reverting the commit but it the force push erased the commit history so I can only go back to the force push commit. 我做了一些谷歌搜索,然后尝试还原提交,但是强制推送删除了提交历史,因此我只能返回到强制推送提交。

I have a computer at home which hasn't pulled the updates since this mistake and I was able to get its commit SHA. 自从出现此错误以来,我在家中有一台没有拉出更新的计算机,并且能够获得其提交SHA。 I used that on the web and found the last version its still there but I can't pull that commit because it got "deleted" and won't show in the repos history. 我在网络上使用了它,发现它的最新版本仍然存在,但是我无法撤回该提交,因为它被“删除”并且不会显示在回购记录中。 Is there a way I can use the SHA from the last good commit and make that the master branch restoring my history? 有什么办法可以使我从上一次的良好提交中使用SHA,并使master分支恢复我的历史记录?

When changing the HEAD (the working position in the git repository), it tracks your history in the reflog . 更改HEAD (git存储库中的工作位置)时,它会在reflog跟踪您的历史记录。 Executing git reflog in the command line will show your complete history. 在命令行中执行git reflog将显示您的完整历史记录。 You can read more about it on git-scm.com 您可以在git-scm.com了解更多有关它的信息。

When you have the desired commit SHA, you can do a git checkout with it. 当您具有所需的提交SHA时,可以对其进行git checkout。 Executing git checkout [SHA1] will take you to the desired position in the git history. 执行git checkout [SHA1]将带您到git历史记录中的所需位置。 From there, you can make a new branch from it by running git checkout -b "new_branchname" . 在这里,您可以通过运行git checkout -b "new_branchname"从中创建一个新分支。

If you want to set your current branch to the commit, you can run git reset --hard [SHA1] . 如果要将当前分支设置为提交,则可以运行git reset --hard [SHA1] This would reset the current branch, but the previous state can however be found in the reflog. 这将重置当前分支,但是可以在引用日志中找到先前的状态。

I do NOT recommend doing this as general practice BUT...You should be able to force push from the other computer, thus resetting the repo to its former state. 我不建议这样做,但一般情况下...您应该能够从另一台计算机上强制推送,从而将存储库重置为以前的状态。 Providing you don't want to keep anything you did while making a giant mess of your git repo yesterday. 如果您不想保留昨天做的git repo造成的大混乱时所做的任何事情。

Since you know the right version you want to recover, it will make things easier. 既然您知道要恢复的正确版本,它将使事情变得简单。

In your local repo, use below commands: 在您的本地仓库中,使用以下命令:

git reset --hard <commit sha value>
git push -f

Note: if you execute the commands in the local not initialized (not cloned from remote), you should fetch first by git fetch --all , and then execute the two commands . 注意:如果在未初始化的本地(未从远程克隆)中执行命令,则应首先通过git fetch --all ,然后执行这两个命令。

Now the version will go back to the latest as you need both locally and remotely. 现在,根据您本地和远程的需要,该版本将恢复到最新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM