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

后端 未结 11 2033
陌清茗
陌清茗 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:56

    You can also remove files from the repository based on your .gitignore without deleting them from the local file system :

    git rm --cached `git ls-files -i -X .gitignore`
    

    Or, alternatively, on Windows Powershell:

    git rm --cached $(git ls-files -i -X .gitignore)
    
    0 讨论(0)
  • 2020-11-22 02:58

    If you want to just untrack a file and not delete from local and remote repo then use thhis command:

    git update-index --assume-unchanged  file_name_with_path
    
    0 讨论(0)
  • 2020-11-22 03:02

    As per my Answer here: https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in-my-github-repository

    To remove folder/directory or file only from git repository and not from the local try 3 simple steps.


    Steps to remove directory

    git rm -r --cached File-or-FolderName
    git commit -m "Removed folder from repository"
    git push origin master
    

    Steps to ignore that folder in next commits

    To ignore that folder from next commits make one file in root named .gitignore and put that folders name into it. You can put as many as you want

    .gitignore file will be look like this

    /FolderName
    

    remove directory

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

    Git lets you ignore those files by assuming they are unchanged. This is done by running the git update-index --assume-unchanged path/to/file.txt command. Once marking a file as such, git will completely ignore any changes on that file; they will not show up when running git status or git diff, nor will they ever be committed.

    (From https://help.github.com/articles/ignoring-files)

    Hence, not deleting it, but ignoring changes to it forever. I think this only works locally, so co-workers can still see changes to it unless they run the same command as above. (Still need to verify this though.)

    Note: This isn't answering the question directly, but is based on follow up questions in the comments of the other answers.

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

    To remove an entire folder from the repo (like Resharper files), do this:

    git rm -r --cached folderName
    

    I had committed some resharper files, and did not want those to persist for other project users.

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