Q: How to make git ignore nuget packages while switching branches I've marked the packages folder as ignored in .gitignore but while switching branches, git removes the contents partially and it takes a while before package restore refetches the packages during the next build. Ideally, I'd like the packages folder to not be touched when I switch branches (especially when identical nuget packages are there in both branches in the repositories.config file). I'm wondering what I should do to make git ignore the packages during branch switches?
Here's a sample of my .gitignore file:
    # 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


A: You can try and use the assume-unchanged flag
https://git-scm.com/docs/git-update-index
When you switch branches set it on and when you want it to track your files again set it on.

--[no-]assume-unchanged

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.


