【问题标题】:Revert to the last change of git add恢复到 git add 的最后更改
【发布时间】:2019-04-03 18:06:03
【问题描述】:

我正在处理一个 git repo 并发出一个命令

$ git add .

保存当前修改,

10 分钟后,我不小心做了一些不好的更改,所以我想恢复到上次添加 .状态。

我搜索但发现只有重置为最新提交的方法。

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

【问题讨论】:

  • 我很确定你不走运。尽早提交,经常提交。 进行提交的唯一原因是您害怕以后修改提交历史。不要害怕这样做。
  • 使用 git reset 取消暂存提交的文件。

标签: git


【解决方案1】:

简短的回答是:你不能,git 只提供返回到以前的提交的方法(例如:你使用git commit 提交的东西)

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


更长的答案是:如果恢复此文件的先前版本比保持您的心理健康更重要,您可以挖掘悬空 blob 列表


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

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

#!/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

一些解释:

# 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 "blobs" (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

【讨论】:

  • 使用 git fsck --lost-found 是恢复所有悬空 blob 的快速方法,但它们的名称无济于事,而且通常需要查看很多。
猜你喜欢
  • 1970-01-01
  • 2012-11-11
  • 2020-03-30
  • 1970-01-01
  • 2021-02-17
  • 2016-05-25
  • 1970-01-01
  • 2011-03-15
  • 2022-09-23
相关资源
最近更新 更多