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

后端 未结 11 2031
陌清茗
陌清茗 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
    
    0 讨论(0)
  • 2020-11-22 02:46

    From the man file:

    When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

    So, for a single file:

    git rm --cached mylogfile.log
    

    and for a single directory:

    git rm --cached -r mydirectory
    
    0 讨论(0)
  • 2020-11-22 02:49

    Ignore the files, remove the files from git, update git (for the removal).

    Note : this does not deal with history for sensitive information.

    This process definitely takes some undertanding of what is going on with git. Over time, having gained that, I've learned to do processes such as:

    1) Ignore the files

    • Add or update the project .gitignore to ignore them - in many cases such as yours, the parent directory, e.g. log/ will be the regex to use.
    • commit and push that .gitignore file change (not sure if push needed mind you, no harm if done).

    2) Remove the files from git (only).

    • Now remove the files from git (only) with git remove --cached some_dir/
    • Check that they still remain locally (they should!).

    3) Add and commit that change (essentially this is a change to "add" deleting stuff, despite the otherwise confusing "add" command!)

    • git add .
    • git commit -m"removal"
    0 讨论(0)
  • 2020-11-22 02:50

    Also, if you have commited sensitive data (e.g. a file containing passwords), you should completely delete it from the history of the repository. Here's a guide explaining how to do that: http://help.github.com/remove-sensitive-data/

    0 讨论(0)
  • 2020-11-22 02:50

    A more generic solution:

    1. Edit .gitignore file.

      echo mylogfile.log >> .gitignore

    2. Remove all items from index.

      git rm -r -f --cached .

    3. Rebuild index.

      git add .

    4. Make new commit

      git commit -m "Removed mylogfile.log"

    0 讨论(0)
  • 2020-11-22 02:51

    This depends on what you mean by 'remove' from git. :)

    You can unstage a file using git rm --cached see for more details. When you unstage something, it means that it is no longer tracked, but this does not remove the file from previous commits.

    If you want to do more than unstage the file, for example to remove sensitive data from all previous commits you will want to look into filtering the branch using tools like the BFG Repo-Cleaner.

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