有没有办法从git重置中恢复未提交的更改到工作目录-hard HEAD?
当前回答
答案来自这个SO
$ git reflog show
4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master
295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.
7c49ec7 HEAD@{2}: commit: restore dependencies to the User model
fa57f59 HEAD@{3}: commit: restore dependencies to the Profile model
3431936 HEAD@{4}: commit (amend): restore admin
033f5c0 HEAD@{5}: commit: restore admin
ecd2c1d HEAD@{6}: commit: re-enable settings app
# assuming you want to get back to 7c49ec7 (restore dependencies to the User model)
$ git reset HEAD@{2}
你的好日子回来了!:)
其他回答
根据定义,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 reflog
git checkout <commit id> // now you are in where you want but you cannot push from detached branch to master
manually copy and paste changes from detached branch to master or working branch
git reset --hard HEAD // if needed
git add ... > git commit ... > git push ...
要将指针移回你之前的提交,但保持你在最近一次提交中所做的更改,checkout git reset -soft dadada
信息丢失了。
由于您没有提交,您的.git从未存储此信息。所以基本上git不能为你恢复它。
但是,如果你只是使用git diff,有一种方法可以通过以下3个简单步骤使用终端输出恢复。
滚动你的终端并寻找git diff的o/p,将o/p保存在一个名为diff.patch的文件中 搜索并替换所有7个空格和8个空格与制表符(\t)字符,并保存更改。 进入git存储库。应用diff.patch (patch -p1 < diff.patch)
注意:当您将数据从终端复制到文件时,要小心并清楚地看到数据是连续输出的,不包含任何冗余数据(由于按下向上和向下箭头)。否则你会搞砸的。
当我在做一个本地项目时,我想把它移动到GitHub上,然后创建一个新的存储库。当我试图用.gitignore将所有这些文件添加到新的存储库中时,我不小心添加了一个错误的文件,然后试图清除它。
我运行了git reset -hard origin/master
然后我所有的本地文件都被删除了,因为回购是空的。我以为一切都消失了。
这招对我很管用:
git reflog show
git reset HEAD@{1}
git push
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之间的区别