Fix Git “Unable to Unlink” Errors When Stashing on Windows

The problem

On Windows, git stash may report unable to unlink when it cannot replace or remove a working-tree file. A running program may have the file open, but permissions, antivirus scanning, or another filesystem issue can also prevent the operation. The message alone does not identify the cause.

Recover without moving repository files

  1. Read the complete Git error and note each affected path. Do not move or delete the repository contents as a workaround.
  2. Close the application that may be using the affected file, such as the program being built or a process launched from that folder.
  3. On Windows, use Microsoft Sysinternals Process Explorer (Find Handle or DLL) or Handle to identify a process with the path open. Close that application normally; avoid forcibly closing an individual handle.
  4. Check that the intended changes are still present with git status. If needed, save a separate backup before retrying.
  5. Retry git stash push -u to include untracked files. If it still fails, record the exact path and error and investigate permissions, filesystem health, or security software instead of repeatedly retrying.

What the stash option does

-u includes untracked files in the stash. It does not fix an operating-system lock or permission error. Git stash does not include ignored files unless you use -a; check the Git stash documentation before choosing that option.

Prevention

  • Stop programs that are using files before switching branches or stashing.
  • Keep generated build outputs out of version control when appropriate, using a project-specific .gitignore.
  • Review git status and preserve any uncommitted work before recovery steps.

Technical Details

The error commonly occurs with:

  • Build output files (.dll, .exe, .pdb)
  • Configuration files (appsettings.json)
  • Referenced assemblies in the refs/ directory

This is particularly common in .NET Core applications where the build process generates multiple interdependent files.

Leave a Reply

Your email address will not be published. Required fields are marked *