Remove a file from a Git repository without deleting it from the local filesystem

后端 未结 11 2259
陌清茗
陌清茗 2020-11-22 02:25

My initial commit contained some log files. I\'ve added *log to my .gitignore, and now I want to remove the log files from my repository.



        
11条回答
  •  面向向阳花
    2020-11-22 02:44

    Above answers didn't work for me. I used filter-branch to remove all committed files.

    Remove a file from a git repository with:

    git filter-branch --tree-filter 'rm  file'
    

    Remove a folder from a git repository with:

    git filter-branch --tree-filter 'rm -rf directory'
    

    This removes the directory or file from all the commits.

    You can specify a commit by using:

    git filter-branch --tree-filter 'rm -rf directory' HEAD
    

    Or an range:

    git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD
    

    To push everything to remote, you can do:

    git push origin master --force
    

提交回复
热议问题