Git: How to ignore/specify files for *checkout*

前端 未结 5 1163
日久生厌
日久生厌 2021-02-13 02:56

If I don\'t want .html files tracked I can add the pattern to .gitignore and they\'ll be ignored. I\'d like to know how I can do the converse - at checkout, how could I ask git

5条回答
  •  暖寄归人
    2021-02-13 03:22

    If you want just a one-time change (or not consistent) or you just don't want to put files in .gitignore then do the below:

    1. Stash specific files that you want to exclude from checkout
    git stash push -m "files_to_ignore" my/path/of/the/file/file.txt
    
    1. Do checkout as you expected before:
    git checkout .
    
    1. Extract stashed content:
    git stash apply stash^{/files_to_ignore}
    

    The complete solution using git aliases can look like this:

    alias reset-non-dev='git stash push -m "ignore_files" MyRepo/file1.txt MyRepo/dir/file2.js MyRepo/dir/dir2/file3.cs PSOne/Startup/PSOne/Views/Login.xaml.cs ; git checkout . ; git stash apply stash^{/ignore_files} ;'
    

    Later you can use this alias simply by putting its name in git bash and hitting enter.

    One benefit here that I observed is that with this approach you can change the files or skip them whenever you really need it and not marked such files as always ignored.

提交回复
热议问题