如何让 git 在切换分支时忽略 nuget 包

How to make git ignore nuget packages while switching branches

我在 中将包文件夹标记为 忽略。git忽略 但是在切换分支时,git 部分删除了内容,并且在下一次构建期间包还原重新获取包之前需要一段时间。理想情况下,我希望在切换分支时不要触及 packages 文件夹(尤其是当 repositories.config 文件的两个分支中都存在相同的 nuget 包时)。我想知道我应该怎么做才能让 git 在分支切换期间忽略包?

这是我的 .gitignore 文件的示例:

    # NuGet Packages
    *.nupkg
    # The packages folder can be ignored because of Package Restore
    **/packages/*
    # except build/, which is used as an MSBuild target.
    !**/packages/build/    
    !**/packages/repositories.config

您可以尝试使用 assume-unchanged 标志
https://git-scm.com/docs/git-update-index

当你切换分支时设置它,当你希望它再次跟踪你的文件时设置它。


--[no-]假设不变

When this flag is specified, the object names recorded for the paths are not updated.

Instead, this option sets/unsets the "assume unchanged" bit for the paths.

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.
If you want to change the working tree file, you need to unset the bit to tell Git.

This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.