How do I delete files locally without commiting these deletions to a remote repository while keeping a “clean” working tree?

前端 未结 2 1428
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 09:41

This question is essentially the opposite of this one.

There are certain files that exist for reasons on the team\'s remote. They are not frequently changed

相关标签:
2条回答
  • 2021-01-07 10:09

    The sparse-checkout should work.

    Enable sparse-checkout:

    git config core.sparsecheckout true
    

    Create the config file .git/info/sparse-checkout and input the patterns:

    *
    !path/to/file1/you/want/to/delete
    !path/to/file2/you/want/to/delete
    !path/to/file3/you/want/to/delete
    

    The patterns mean "to checkout everything(*) except(!) the rest paths".

    Make it work:

    git checkout
    

    This way, these files are not checked out and everything is the same to you as long as you don't need to modify or checkout these files. In case you need these files to be checked out, remove or comment the file paths in .git/info/sparse-checkout and leave only *, and run git checkout again to checkout the hidden files.

    0 讨论(0)
  • 2021-01-07 10:28

    You can try:

    git update-index --assume-unchanged -- unWantedFile
    rm unWantedFile
    # work
    # ...
    # later on
    git update-index --no-assume-unchanged -- unWantedFile
    git restore unWantedFile
    

    That should be enough to work in a working tree without that file, while telling Git the file is still there.

    It works too with git update-index --skip-worktree -- unWantedFile.
    I have written about the difference between the two here.

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