Resync git repo with new .gitignore file

前端 未结 3 2169
感动是毒
感动是毒 2020-12-12 09:10

Is it possible to \"refresh\" a git repository after updating the gitignore file?

I just added more ignorations(?) to my gitignore and would like to remove stuff alr

3条回答
  •  抹茶落季
    2020-12-12 09:19

    The solution mentioned in ".gitignore file not ignoring" is a bit extreme, but should work:

    # rm all files
    git rm -r --cached .
    # add all files as per new .gitignore
    git add .
    # now, commit for new .gitignore to apply
    git commit -m ".gitignore is now working"
    

    (make sure to commit first your changes you want to keep, to avoid any incident as jball037 comments below.
    The --cached option will keep your files untouched on your disk though.)

    You also have other more fine-grained solution in the blog post "Making Git ignore already-tracked files":

    git rm --cached `git ls-files -i --exclude-standard`
    

    Bassim suggests in his edit:

    Files with space in their paths

    In case you get an error message like fatal: path spec '...' did not match any files, there might be files with spaces in their path.

    You can remove all other files with option --ignore-unmatch:

    git rm --cached --ignore-unmatch `git ls-files -i --exclude-standard`
    

    but unmatched files will remain in your repository and will have to be removed explicitly by enclosing their path with double quotes:

    git rm --cached ""
    

提交回复
热议问题