Remove sensitive files and their commits from Git history

前端 未结 11 2184
借酒劲吻你
借酒劲吻你 2020-11-21 04:36

I would like to put a Git project on GitHub but it contains certain files with sensitive data (usernames and passwords, like /config/deploy.rb for capistrano).

I kno

11条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 05:27

    I recommend this script by David Underhill, worked like a charm for me.

    It adds these commands in addition natacado's filter-branch to clean up the mess it leaves behind:

    rm -rf .git/refs/original/
    git reflog expire --all
    git gc --aggressive --prune
    

    Full script (all credit to David Underhill)

    #!/bin/bash
    set -o errexit
    
    # Author: David Underhill
    # Script to permanently delete files/folders from your git repository.  To use 
    # it, cd to your repository's root and then run the script with a list of paths
    # you want to delete, e.g., git-delete-history path1 path2
    
    if [ $# -eq 0 ]; then
        exit 0
    fi
    
    # make sure we're at the root of git repo
    if [ ! -d .git ]; then
        echo "Error: must run this script from the root of a git repository"
        exit 1
    fi
    
    # remove all paths passed as arguments from the history of the repo
    files=$@
    git filter-branch --index-filter \
    "git rm -rf --cached --ignore-unmatch $files" HEAD
    
    # remove the temporary history git-filter-branch
    # otherwise leaves behind for a long time
    rm -rf .git/refs/original/ && \
    git reflog expire --all && \
    git gc --aggressive --prune
    

    The last two commands may work better if changed to the following:

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

提交回复
热议问题