Git如何永久删除文件(包括历史记录)

    xiaoxiao2021-04-13  31

    参考 github 的帮助:

    https://help.github.com/articles/remove-sensitive-data

    步骤一: 从你的资料库中清除文件

    以Windows下为例(Linux类似), 打开项目的Git Bash,使用命令:

    git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path-to-your-remove-file' --prune-empty --tag-name-filter cat -- --all

    其中, path-to-your-remove-file 就是你要删除的文件的相对路径(相对于git仓库的跟目录), 替换成你要删除的文件即可.

    如果你要删除的文件很多, 可以写进一个.sh文件批量执行, 如果文件或路径里有中文, 由于MinGW或CygWin对中文路径设置比较麻烦, 你可以使用通配符*号, 例如: sound/music_*.mp3, 这样就把sound目录下以music_开头的mp3文件都删除了.

    例如这样, del-music-mp3.sh:

    #!/bin/bash # git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch projects/Moon.mp3' --prune-empty --tag-name-filter cat -- --all # git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch sound/Music_*.mp3' --prune-empty --tag-name-filter cat -- --all

     如果你看到类似下面这样的, 就说明删除成功了:

    Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266) # Ref 'refs/heads/master' was rewritten

    如果显示 xxxxx unchanged, 说明repo里没有找到该文件, 请检查路径和文件名是否正确.

    注意: 补充一点, 如果你想以后也不会再上传这个文件或文件夹, 请把这个文件或文件夹添加到.gitignore文件里, 然后再push你的repo.

    步骤二: 推送我们修改后的repo

    以强制覆盖的方式推送你的repo, 命令如下:

    git push origin master --force

    这个过程其实是重新上传我们的repo, 比较耗时, 虽然跟删掉重新建一个repo有些类似, 但是好处是保留了原有的更新记录, 所以还是有些不同的. 如果你实在不在意这些更新记录, 也可以删掉重建, 两者也差不太多, 也许后者还更直观些.

    执行结果类似下面:

    Counting objects: 4669, done. Delta compression using up to 4 threads. Compressing objects: 100% (4352/4352), done. Writing objects: 100% (4666/4666), 35.16 MiB | 51 KiB/s, done. Total 4666 (delta 1361), reused 0 (delta 0) To https://github.com/defunkt/github-gem.git + beb839d...81f21f3 master -> master (forced update)

     步骤三: 清理和回收空间

    虽然上面我们已经删除了文件, 但是我们的repo里面仍然保留了这些objects, 等待垃圾回收(GC), 所以我们要用命令彻底清除它, 并收回空间.

    命令如下:

    rm -rf .git/refs/original/ git reflog expire --expire=now --all git gc --prune=now Counting objects: 2437, done. # Delta compression using up to 4 threads. # Compressing objects: 100% (1378/1378), done. # Writing objects: 100% (2437/2437), done. # Total 2437 (delta 1461), reused 1802 (delta 1048) git gc --aggressive --prune=now Counting objects: 2437, done. # Delta compression using up to 4 threads. # Compressing objects: 100% (2426/2426), done. # Writing objects: 100% (2437/2437), done. # Total 2437 (delta 1483), reused 0 (delta 0)

    注: 绿色字部分是命令执行后的结果.

     

    现在你再看看你的.git目录文件大小是不是变小了.

     

    参考自:

    http://whoop.sinaapp.com/blog/article/21

    http://blog.csdn.net/meteor1113/article/details/4407209

    比如提交了敏感的信息或者提交了错误的版本。这个时候我们想将提交到代码库的记录删除,我们要怎么做呢? 首先,我们需要找到我们需要回滚到的提交点的hash,可以使用git log命令获取提交的历史找到需要回滚到的提交点

    复制hash值,使用git reset –hard commit_hash

    再使用git push origin HEAD –force即可

    步骤整理:

    假定需要删除的文件为:src/main/resources/passowrd.txtx

    git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch src/main/resources/passowrd.txtx' HEAD

    git reflog expire --expire=now --all   git gc --prune=now   git gc --aggressive --prune=now

    git push --all --force

    命令执行成功后,查看远程资源库的提交记录(包括需要删除文件的提交记录),查看这个更新内容这个文件已经被删除。

    转载请注明原文地址: https://ju.6miu.com/read-668539.html

    最新回复(0)