How to remove file from Git history?

后端 未结 7 834
时光说笑
时光说笑 2020-11-28 03:04

Some time ago I added info(files) that must be private. Removing from the project is not problem, but I also need to remove it from git history.

I use Gi

相关标签:
7条回答
  • 2020-11-28 03:25
    • remove the file and rewrite history from the commit you done with the removed file(this will create new commit hash from the file you commited):

      git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all

    • now force push the repo:

      git push origin --force --all

    • now tell your collaborators to rebase.

    0 讨论(0)
  • 2020-11-28 03:28

    Using the bfg repo-cleaner package is another viable alternative to git-filter-branch. Apparently, it is also faster...

    0 讨论(0)
  • 2020-11-28 03:28

    git-repo-filter

    git recommends to use git-filter-repo (when git filter-branch command is executed). There is a long list of why it is better than any other alternatives (https://github.com/newren/git-filter-repo#why-filter-repo-instead-of-other-alternatives), my experience is that it is very simple and very fast.

    This command removes the file from all commits in all branches:

    git filter-repo --path <path to the file or directory> --invert-paths

    Multiple paths can be specified by using multiple --path parameters. You can find detailed documentation here: https://www.mankier.com/1/git-filter-repo

    0 讨论(0)
  • 2020-11-28 03:29

    I have found this answer and it helped:

    git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
    

    Found it here https://myopswork.com/how-remove-files-completely-from-git-repository-history-47ed3e0c4c35

    0 讨论(0)
  • 2020-11-28 03:35

    If you have recently committed that file, or if that file has changed in one or two commits, then I'd suggest you use rebase and cherrypick to remove that particular commit.

    Otherwise, you'd have to rewrite the entire history.

    git filter-branch --tree-filter 'rm -f <path_to_file>' HEAD
    

    When you are satisfied with the changes and have duly ensured that everything seems fine, you need to update all remote branches -

    git push origin --force --all
    

    Note:- It's a complex operation, and you must be aware of what you are doing. First try doing it on a demo repository to see how it works. You also need to let other developers know about it, such that they don't make any change in the mean time.

    0 讨论(0)
  • 2020-11-28 03:35
    • First of all, add it to your .gitignore file and don't forget to commit the file :-)
    • You can use this site: http://gtiignore.io to generate the .gitignore for you and add the required path to your binary files/folder(s)

    • Once you added the file to .gitignore you can remove the "old" binary file with BFG.


    How to remove big files from the repository

    You can use git filter-branch or BFG. https://rtyley.github.io/bfg-repo-cleaner/

    BFG Repo-Cleaner

    an alternative to git-filter-branch.

    The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:

    * Removing Crazy Big Files*
    * Removing Passwords, Credentials & other Private data

    Examples (from the official site)

    In all these examples bfg is an alias for java -jar bfg.jar.

    # Delete all files named 'id_rsa' or 'id_dsa' :
    bfg --delete-files id_{dsa,rsa}  my-repo.git
    

    0 讨论(0)
提交回复
热议问题