首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >恢复到git add的最后一个更改。

恢复到git add的最后一个更改。
EN

Stack Overflow用户
提问于 2018-10-30 16:19:05
回答 1查看 94关注 0票数 1

我正在做一个git回购并发出命令

代码语言:javascript
复制
$ git add .

为了保存当前的修改,

10分钟后,我意外地做了一些坏的更改,所以我想恢复到最后的添加。状态。

我搜索了,但发现只有方法可以重置为最新提交。

我怎么能回到十分钟前的状态。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-30 16:27:08

简短的回答是:您不能,git只提供返回以前提交的方法(例如:使用git commit注册的内容)

将来使用:您可以运行git add . && git commit -m WIP来“保存当前的修改”

更长的答案是:如果回到这个文件的前一个版本比保持你的精神健康更重要的话,你可能会深入到悬空的斑点列表中。

嘿,我知道我在某个地方有个剧本:

下面的脚本将列出尚未打包到对象包中的不可访问的blob (最近的blob通常是这种情况),并按创建日期对它们进行排序(实际上:使用磁盘上文件的创建日期来估计blob创建的时间)

代码语言:javascript
复制
#!/bin/sh

git fsck --no-reflogs --unreachable |\
    grep blob |\
    cut -d' ' -f3 |\
    sed -e 's|^\(..\)\(.*\)|.git/objects/\1/\2|' |\
    xargs ls -l -t 2> /dev/null

一些解释:

代码语言:javascript
复制
# git fsck --unreachable  ,  if you also use "--no-reflogs" this will search
# through commits which could be reached by the reflog but not by live branches
git fsck --no-reflogs --unreachable |\

# only keep lines mentioning "blob" (files)
    grep blob |\

# keep the 3rd field of the output (hash of blob)
    cut -d' ' -f3 |\

# turn hashes into filenames, e.g :
#   aee01f414061ea9b0bdbbc1f66cec0c357f648fe ->
#   .git/objects/ae/e01f414061ea9b0bdbbc1f66cec0c357f648fe
# (this will be the path of this single blob)
    sed -e 's|^\(..\)\(.*\)|.git/objects/\1/\2|' |\

# give this to ls -lt (list by modification time),
# discard messages saying "file does not exist"
    xargs ls -l -t 2> /dev/null
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53068697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档