【问题标题】:git remove all deleted files from entire historygit 从整个历史记录中删除所有已删除的文件
【发布时间】:2012-12-05 13:10:13
【问题描述】:

我想知道是否有人有更有效、更智能的方法来做到这一点。循环结构要求通过读取每个提交从每个提交中删除每个已删除的文件。对于许多提交,这需要很长时间。

git log --pretty=format: --name-status | grep -i ^D | cut -f2- | sort -u | xargs -I {} git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch {}' HEAD

【问题讨论】:

    标签: git bash git-bash


    【解决方案1】:

    这似乎一次删除每个文件。
    考虑到git rm 可以删除多个文件。

    因此,一项优化是构建该列表并调用一次过滤器分支。
    您可以在“Proper way to remove unwanted files with git filter-branch without git rm failing”中看到该方法的一个示例。

    【讨论】:

    • 我发现grep -v -x -f /tmp/files-in-repo.txtxargs --delimiter=\\n -r git rm ... 很有用。
    【解决方案2】:

    对于可能正在寻找相同答案的其他人,我使用the other thread 中链接的建议修改了上面的原始命令,并创建了这个似乎对我有用的命令。

    git filter-branch --prune-empty --index-filter 'git log --pretty=format: --name-status | grep -i ^D | cut -f2- | sort -u | xargs git rm --cached --ignore-unmatch DoesNotExistInMyProject'
    

    【讨论】:

      【解决方案3】:

      支持递归删除的小改进

      git filter-branch --prune-empty --index-filter 'git log --pretty=format: --name-status | grep -i ^D | cut -f2- | sort -u | xargs git rm -r --cached --ignore-unmatch DoesNotExistInMyProject'
      

      【讨论】:

        【解决方案4】:

        改进了命令以避免文件名带有 ' 导致 xargs 出错

        echo "/!\ This file contains a single quote ' and it's gonna bug with xargs, so it's excluded from the filter operation" ; bash -c "git log --pretty=format: --name-status | grep -i ^D " | grep \' git filter-branch -f --prune-empty --index-filter "git log --pretty=format: --name-status | grep -i ^D | grep -v \' | cut -f2- | sort -u | xargs git rm -r --cached --ignore-unmatch DoesNotExistInMyProject"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-19
          • 2020-03-20
          • 1970-01-01
          • 2016-02-24
          • 1970-01-01
          相关资源
          最近更新 更多