有没有办法从git重置中恢复未提交的更改到工作目录-hard HEAD?
当前回答
根据定义,git reset -hard会丢弃未提交的更改,git无法恢复它们(你的备份系统可能会有帮助,但不是git)。
实际上,很少有git重置的情况——困难是个好主意。在大多数情况下,有一个更安全的命令来做同样的事情:
If you want to throw away your uncommitted changes, then use git stash. It will keep a backup of these changes, which will expire after some time if you run git gc. If you're 99.9% sure you'll never need these changes back, then git stash is still your friend for the 0.1% case. If you're 100% sure, then git stash is still your friend because these 100% have a measurement error ;-). If you want to move your HEAD and the tip of the current branch in history, then git reset --keep is your friend. It will do the same thing as git reset --hard, but will not discard your local changes. If you want to do both, then git stash && git reset --keep is your friend.
教你的手指不要使用git重置——很难,总有一天会有回报的。
其他回答
一般来说,您无法返回未提交的更改。
以前的分段更改(git add)应该可以从索引对象中恢复,所以如果你这样做了,使用git fsck——lost-found来定位与它相关的对象。(这将对象写到.git/lost-found/目录;从那里你可以使用git show <filename>来查看每个文件的内容。)
如果没有,答案是:看看你的备份。也许你的编辑器/IDE将临时拷贝存储在/tmp或C:\ temp之类的目录下
git reset HEAD@{1}
这将恢复到前一个HEAD
[1] vim例如,可选地存储持久撤销,eclipse IDE存储本地历史;这样的功能可能会节省你的**
以后你可以使用VSCode和ext: GitLens - Git增压,所以你可以用这个扩展反转你的代码
如果你幸运地有相同的文件打开在另一个编辑器(例如。Sublime Text)试试按ctrl-z键。它救了我。
我也遇到了同样的问题,我几乎要疯了....最初我提交了项目并合并。 后来当我尝试运行git push——set-upstream origin master时,我得到了这个错误
致命的:拒绝合并不相关的历史
所以我运行git reset—hard HEAD,它删除了一个3周的项目,但下面的几个命令挽救了这一天:
git reset HEAD@{1} //this command unstage changes after reset
git fsck --lost-found //I got the dangling commit fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b
git show <dangling commit something like-> fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b>
git rebase fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b
IntelliJ有一个临时文件夹,可以通过history命令访问:
在导航窗格中选择要还原文件的文件夹 双击Shift键(Shift - Shift) 在弹出的输入框中输入本地历史记录并按Enter 选择显示历史记录 现在您可以恢复到所需的版本。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别
