TekOnline

Resolving Git “Unable to Unlink” Errors When Stashing Changes

The Problem

When attempting to stash changes using Git with untracked files, you might encounter multiple “unable to unlink” errors that look like this:

error: unable to unlink old 'CDISV2Core.dll': Invalid argument
error: unable to unlink old 'CDISV2Core.exe': Invalid argument
error: unable to unlink old 'appsettings.json': Invalid argument
# ... and many more similar errors for various .dll files

This typically occurs when Git cannot remove or modify certain files, often due to them being in use by running processes or locked by the operating system.

Quick Solution

Here’s the simple three-step solution that resolved the issue:

  1. Move all contents (except the .git folder) to an archive folder outside the repository root
  2. Ensure only the .git folder remains in the repository directory
  3. Run git stash push -u (or git stash with appropriate flags for untracked files)

Why This Works

The solution works because:

  • Moving files outside the repository temporarily releases any file locks
  • Keeping only the .git folder ensures Git can properly manage its internal state
  • The -u flag in the stash command ensures untracked files are included in the stash

Prevention Tips

To avoid this issue in the future:

  • Close any running instances of your application
  • Close IDEs or editors that might be locking the files
  • Ensure antivirus software isn’t interfering with file operations
  • Consider using .gitignore for build outputs and dependencies

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.


Posted

in

by

Tags:

Comments

Leave a Reply

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