git-有日志提交但缺少.sln文件,因此无法打开项目-如何还原 [英] git - have log commits but missing .sln file so cannot open project - how to restore

查看:99
本文介绍了git-有日志提交但缺少.sln文件,因此无法打开项目-如何还原的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用git恢复我的项目文件.

Is there any way to restore my project files using git.

这是场景....我在分支机构工作了几天.该分支从未被推送到远程仓库,因此它完全是本地的.

Here is the scenario.... I worked on a branch for few days. This branch was never pushed to remote repo, so it is entirely local.

我最终失去了.sln解决方案文件,因此我无法再打开项目,但是在项目目录中,我的目录仍然是.git.

I ended up loosing my .sln solution file so I cannot open the project any more but in the project directory I do have the .git directory still.

如果我发布git log,我可以看到它列出的提交很早,因此日志似乎存在.

If I issue git log, I can see it lists commit far back so the logs seem to exists.

是否可以通过某种方式在这些日志中查找文件或基于日志中的提交还原我的项目?还是有其他方法?

is there a way to somehow find files in these logs or restore my project based on the commits in log? Or is there some other way?

更新

由于@RomainValeri, git checkout< commit-hash> 可以工作,但是它将HEAD设置为分离状态.虽然这可能没什么大不了,但后来我发现 git reset --hard HEAD〜1 效果更好,因为我不需要在上一次提交中保留更改,因此使用-hard 丢弃它们.如果需要保留这些更改,则使用-soft 会将HEAD重置为最后一次提交之前的提交,并将最后一次提交中的更改添加到登台区域.

Thanks to @RomainValeri, git checkout <commit-hash> works but it sets HEAD in detached state. While that might not be big deal, later I found that git reset --hard HEAD~1 works better since I dont need to keep changes in last commit, so using --hard discards them. If you need to keep these changes, using --soft instead will reset HEAD to your commit before last commit and add changes in last commit to staging area.

git reset --hard HEAD~1
git reset --hard HEAD~2
git reset --hard HEAD~3
...

以上命令在上次提交之前重置HEAD指针1,2,3,...提交,并在之后放弃所有更改.如果您不想放弃这些更改,请使用--soft而不是--hard,在这种情况下,这些更改将为您准备.

Above commands reset HEAD pointer 1, 2, 3, ... commits before last commit and discard any changes after. Use --soft instead of --hard if you dont want to discard these changes in which case these changes will be staged for you.

推荐答案

是的,这就是git的基本原理:能够在开发的不同阶段恢复代码库.

Yes, this is the very principle of git : being able to restore a codebase at different points in its development.

基本上,任何提交都是快照.

Basically, any commit is a snapshot.

请放心:如果您看到一长串日志,则不仅是在阅读静态日志.此输出是在执行时根据git repo中可用的提交构建的.提交包含所有内容.每次提交都是代码库的完整副本.

So be reassured : if you see a long list of logs, you're not just reading a static log. This output is built at execution from the commits available in your git repo. And commits contain everything. Each commit is a full copy of the code base.

如果愿意

git checkout <commitHash>

工作树(带有链接到仓库的文件系统的目录)将被更新,以反映给定提交中描述的状态.

the working tree (the directory with the system of files linked to the repo) will be updated to reflect the state described in the given commit.

您可以使用已知的 git log 命令找到这些提交哈希,因此可以使用它读取提交消息以确定要检查的快照,存储/复制其哈希(什么是SHA-1哈希?),然后检查出来.

You can find these commit hashes with the git log command you already know, so use it to read the commit messages to determine which snapshot you want to inspect, store/copy its hash (what is a SHA-1 hash?), and check it out.

这篇关于git-有日志提交但缺少.sln文件,因此无法打开项目-如何还原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆