简体   繁体   English

Git:我可以删除推送文件的所有历史记录,除了最后一次提交吗?

[英]Git: Can I remove all history of a pushed file, except of it's last commit?

I have a pushed file with an history of sensitive data.我有一个包含敏感数据历史记录的推送文件。

I'd like to clear the repository from all the file commits history, and leave only the current one, which is clean of sensitive data.我想从所有文件提交历史中清除存储库,只留下当前的,其中没有敏感数据。

How can I do it (using git bash or GitHub desktop\\web)?我该怎么做(使用 git bash 或 GitHub desktop\\web)?

Find a version of that file you're willing to keep, say the latest master copy's clean:找到您愿意保留的该文件的版本,比如最新的master副本是干净的:

safeversion=`git rev-parse master:path/to/file`

Filter history removing any version of that file that's not the safe one:过滤历史记录删除该文件的任何不安全版本:

git filter-branch \
        --setup safeversion=`git rev-parse master:path/to/file` \
        --index-filter '
                if thisversion=`git rev-parse -q --verify $GIT_COMMIT:path/to/file` &&
                        [[ $thisversion != $safeversion ]]
                then git update-index --force-remove path/to/file
                fi
        ' -- --all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM