忘记在git filter-branch之后删除refs/original。

3
我在执行完git filter-branch命令后,忘记了删除refs/original,并且运行了git gc清空了refs。此外,我还向新的库提交了一些内容,并希望保留这些提交。
提取出的分支只有几千字节大小,但是.git文件夹的大小仍然为80 MB,与过滤前相同。
现在refs为空,我无法轻松地再次删除refs/original。如何才能删除原始记录,同时避免重新执行filter-branch命令?
2个回答

2
您的引用已经被打包(git gc运行git pack-refs)。这不会对引用本身进行任何更改,除了将每个引用放在单独的文件中,它们都在“packed”文件中。
您只需要删除refs/origina/引用。理论上,对每个引用使用git update-ref -d应该可以正常工作,但如果有很多,最好在编辑器中打开.git/packed-refs并手动删除所有refs/original/行。
您可能还需要清除reflogs。
有关更多信息,请参见此StackOverflow答案

0

git gc 运行 git pack-refs

如果您开始删除引用,请确保使用 Git 2.24+(2019 年第三季度):"git pack-refs" 可能会丢失在运行时创建的引用,这已得到纠正。

请参见 commit a613d4f(2019 年 7 月 31 日),作者为 Sun Chao (sunchao)
(由 Sun Chao -- sunchao --commit a613d4f 中合并,2019 年 8 月 2 日)

pack-refs:获取锁定文件后始终刷新

当删除打包的引用时,整个打包的引用文件将被重写以省略不再存在的引用。
但是,如果另一个gc命令正在运行并同时调用pack-refs --all, 则有可能丢失刚更新的引用中新创建的提交。

通过这些步骤,可以演示在新更新的引用上丢失提交:

  # step 1: compile git without `USE_NSEC` option
  Some kernel releases do enable it by default while some do
  not. And if we compile git without `USE_NSEC`, it will be easier
  demonstrated by the following steps.

  # step 2: setup a repository and add the first commit
  git init repo &&
  (cd repo &&
   git config core.logallrefupdates true &&
   git commit --allow-empty -m foo)

  # step 3: in one terminal, repack the refs repeatedly
  cd repo &&
  while true
  do
    git pack-refs --all
  done

  # step 4: in another terminal, simultaneously update the
  # master with update-ref, and create and delete an
  # unrelated ref also with update-ref
  cd repo &&
  while true
  do
    us=$(git commit-tree -m foo -p HEAD HEAD^{tree}) &&
    git update-ref refs/heads/newbranch $us &&
    git update-ref refs/heads/master $us &&
    git update-ref -d refs/heads/newbranch &&
    them=$(git rev-parse master) &&
    if test "$them" != "$us"
    then
      echo >&2 "lost commit: $us"
      exit 1
    fi
    # eye candy
    printf .
  done

虽然我们有 packed-refs 锁文件和松散引用锁文件来避免更新冲突,但如果 packed-refs 文件的竞争状态有效性发生,引用将失去其新提交。
最好的方法是在获取 packed-refs 文件的锁文件后始终进行刷新。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接