简体   繁体   English

如何在本地 git 项目中恢复以前提交(但未删除)的文件

[英]How to restore previously committed(but NOT deleted) files in local git project

I had a local project directory(with some source files) which I tried to push to a new blank gitlab repo and ran into trouble.我有一个本地项目目录(带有一些源文件),我试图将其推送到一个新的空白 gitlab 存储库并遇到了麻烦。 Actually for pushing the local files to the gitlab repo, I ran few git commands on local directory and now all the source src files have disappeared from the local directory.实际上,为了将本地文件推送到 gitlab 存储库,我在本地目录上运行了几个 git 命令,现在所有源 src 文件都从本地目录中消失了。 can anyone help me in suggesting for restoring those files please?任何人都可以帮我建议恢复这些文件吗? Below are the commands I ran in the directory in given order which caused the files to disappear.以下是我按给定顺序在目录中运行的命令,这些命令导致文件消失。

git init
git add .
git commit -m "Add existing project files prior to the push to GitHub."  ###This actually committed all the source files
git remote add origin https://gitlab.com/<project.git>
### Till this point the command line was showing (master) i.e master branch in bracket
git branch -M develop
git push -uf origin develop ## error: failed to push some ref ...
git push -u origin develop ## error: failed to push some refs to ...
git pull
git push -u origin develop ## error: failed to push some refs to ...
git push ## fatal: The current branch develop has no upstream branch ..
git push --set-upstream origin develop ## error: failed to push some refs to ...
git pull https://gitlab.com/ielts-cmds/IELTS-CMDS-services-grp/cmds-circuit-breaker.git develop    ## * branch            develop    -> FETCH_HEAD  .... fatal: refusing to merge unrelated histories

git checkout feature/initial ## switched to another branch..

etc. etc.

Please note, I did NOT run any remove or delete command so I am guessing those files are there somewhere in the local git references.请注意,我没有运行任何删除或删除命令,所以我猜测这些文件在本地 git 引用中的某处。 And I cannot go back to the very initial 'master' branch now.而且我现在不能回到最初的“主”分支。 Please help !请帮忙 !

You have not shown the actual error output, which would be helpful in diagnosing the more basic "what went wrong" problem, but we can say why git checkout master is not working: you simply don't have a branch named master .您没有显示实际的错误输出,这将有助于诊断更基本的“出了什么问题”问题,但我们可以说明为什么git checkout master不起作用:您根本没有名为master的分支。 You have one named develop .你有一个名为develop的。 You can switch the name back to master if you like.如果您愿意,可以将名称切换回master

 git init git add . git commit -m "Add existing project files prior to the push to GitHub." ###This actually committed all the source files git remote add origin https://gitlab.com/<project.git> ### Till this point the command line was showing (master) ie master branch in brackets git branch -M develop

The last operation above changed the (local) branch name from master to develop .上面的最后一个操作将(本地)分支名称master更改为develop (Note: it's safer in general to use -m , ie, lowercase m , to avoid deleting an existing branch name by mistake. But if git init created a new empty repository, as seems likely, then there were no other branch names and this could not have harmed anything.) (注意:通常使用-m更安全,即小写m以避免错误地删除现有的分支名称。但是如果git init创建了一个新的存储库,看起来很可能,那么就没有其他分支名称了不可能伤害任何东西。)

 git push -uf origin develop ## error: failed to push some ref ...

This probably should have worked , and knowing what the actual error output was might help.可能应该有效,并且知道实际的错误输出可能会有所帮助。 My guess is that there was an additional message, prefixed with remote: , during which GitLab was telling you something important.我的猜测是有一条附加消息,前缀为remote: ,在此期间 GitLab 告诉你一些重要的事情。

 git push -u origin develop ## error: failed to push some refs to ...

Given the failure with --force ( -f is the same flag, just spelled with fewer characters), it's not surprising that the push without --force would also fail.鉴于--force的失败 -f是相同的标志,只是用更少的字符拼写),没有--force的推送也会失败也就不足为奇了。

 git pull

Did this work?这行得通吗? It would be surprising if it did, given that you then say:如果确实如此,那将是令人惊讶的,因为您随后会说:

 git push ## fatal: The current branch develop has no upstream branch ..

since git pull with no additional options also requires that the current branch have an upstream set (the git fetch step would be able to run, but the second command that git pull runs requires an upstream).因为没有附加选项的git pull要求当前分支有一个上游集( git fetch步骤将能够运行,但git pull运行的第二个命令需要上游)。

 git pull https://gitlab.com/ielts-cmds/IELTS-CMDS-services-grp/cmds-circuit-breaker.git develop ## * branch develop -> FETCH_HEAD .... fatal: refusing to merge unrelated histories

This output tells me that ielts-cmds/IELTS-CMDS-services-grp/cmds-circuit-breaker.git does have a branch named develop .此输出告诉我ielts-cmds/IELTS-CMDS-services-grp/cmds-circuit-breaker.git确实有一个名为develop的分支。 I cannot access that repository myself, so I have no idea what other branch names it might have.我自己无法访问该存储库,因此我不知道它可能还有哪些其他分支名称。

The commits that are on that repository's develop are now available in your own repository as well.该存储库develop中的提交现在也可以您自己的存储库中使用。 But the history —ie, the set of commits—obtained by starting from their last develop commit and working backwards through all other commits is independent of the commit(s) on your branch named develop .但是历史——即提交集——通过从他们的最后一次develop提交开始并通过所有其他提交向后工作而获得独立于名为develop的分支上的提交。 That's all fine, since their develop is their branch name;没关系,因为他们的develop他们的分支名称; your develop is your name, and these need not denote the same set of commits at all.你的develop你的名字,这些根本不需要表示同一组提交。

 git checkout feature/initial ## switched to another branch..

This would just fail, given the output shown above.鉴于上面显示的输出,这将失败。 You would still be on your own develop .您仍然会自行develop

If you have not done anything to change the fact, your own commit(s) are still on your own develop .如果你没有做任何事情来改变这个事实,你自己的 commit(s) 仍然在你自己的develop上。 To make this—that your develop is completely independent of their develop —less confusing, you might want to rename your own develop :为了使这一点——你的develop完全独立于他们的develop ——不那么令人困惑,你可能想要重命名你自己的develop

git branch -m develop my-develop

for instance.例如。

Run git branch with no options, or with -a , to list your own branch names, or all names (your branch names plus any remote-tracking names your Git has created based on other repositories' branch names).运行不带任何选项的git branch或使用-a来列出您自己的分支名称或所有名称(您的分支名称以及您的 Git 根据其他存储库的分支名称创建的任何远程跟踪名称)。 If there are no applicable Git commands other than the ones shown above, you will still have just the one branch named develop unless and until you rename it.如果除了上面显示的命令之外没有适用的 Git 命令,您仍然只有一个名为develop的分支,除非您重命名它。

To fix the problem, read a good book on Git and learn what "related histories" means, and/or see, eg, How to repair a git history and correctly merge unrelated histories and What happens in git when you merge unrelated histories?要解决这个问题,请阅读一本关于 Git 的好书并了解“相关历史”的含义,和/或查看,例如,如何修复 git 历史并正确合并不相关的历史以及合并不相关的历史时 git 会发生什么?

Firstly, @torek for your explanation and dan1st and sytech for commenting.首先,@torek 进行解释,dan1st 和 sytech 进行评论。 The solution which worked was git hard reset.有效的解决方案是 git hard reset。 So in the logs of git I found the commit ID where those files I committed.因此,在 git 的日志中,我找到了我提交的那些文件的提交 ID。 The git hard reset to that commit ID took the directory in its previous state ie with all files restored.对该提交 ID 的 git 硬重置将目录置于其先前状态,即所有文件都已恢复。

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

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