825

Something wrong is going on with one of the files in my local git repository. When I'm trying to change the branch it says:

Unlink of file 'templates/media/container.html' failed. Should I try again? (y/n)

What could that mean?

Jim Fell
  • 13,750
  • 36
  • 127
  • 202
Pol
  • 24,517
  • 28
  • 74
  • 95
  • possible duplicate of [Git rebase got 'unlink of file failed' error ](http://stackoverflow.com/questions/3698369/git-rebase-got-unlink-of-file-failed-error) – Josh Lee Dec 08 '10 at 20:49
  • 1
    Check out [this question](http://stackoverflow.com/q/3698369/285619) – RDL Dec 08 '10 at 16:47
  • did the given answer work for you, if process viewer is not working then restart and you can then remove file, also you can accept the answer if it worked for you – dhaval Apr 17 '12 at 12:25
  • 1
    Also check that the git process has write permissions on that file. – boileau May 13 '14 at 07:52
  • 5
    possible duplicate of [Unlink of file failed](http://stackoverflow.com/questions/10181057/unlink-of-file-failed) – BartoszKP May 11 '15 at 14:26
  • Git 2.19 should improve the situation, at least on Windows: https://stackoverflow.com/a/51755262/6309 – VonC Aug 09 '18 at 07:31
  • Given that this now shows as the first result on a Google search, and the question and the answers here have gotten > 10x more views and upvotes than the other questions linked above, I would venture that this has now become the de-facto reference QA for the "unlink of file" failed question. – Suman Nov 19 '18 at 19:38
  • Git 2.29 (Q4 2020) might improve the situation. See [my answer below](https://stackoverflow.com/a/63662344/6309). – VonC Aug 30 '20 at 21:33

39 Answers39

1118

This could mean that another program is using the file, which is preventing git from "moving" the file into or out of the working directory when you are attempting to change branches.

I have had this happen on Windows Vista where eclipse is the program "using" the file. The file may not be actually open in eclipse but may have been opened by a process run by eclipse.

In this event, try closing the file in any applications that might have used it. If that doesn't work, completely exit any applications which may have opened the file.

Liam
  • 27,717
  • 28
  • 128
  • 190
Melissa
  • 11,312
  • 1
  • 16
  • 3
  • 73
    On Windows, use ProcessExplorer to find which process has the file open. – Dave C Mar 16 '12 at 14:43
  • 2
    Is there a way to write a script like "if this file cannot be deleted (e.g. the file just does not exists), just skip the file". – 2xMax Apr 28 '14 at 14:34
  • +1 @DaveC. I searched in vain for ProcessExplorer in my tools and then downloaded https://technet.microsoft.com/en-us/sysinternals/bb896653.aspx – Bennett Brown Feb 19 '15 at 21:28
  • @BBrown That's the ProcessExplorer I was talking about. You found the official link for downloading it. – Dave C Mar 19 '15 at 15:09
  • 31
    In my case that was Visual Studio and its Git plugin. – Michał Powaga May 11 '15 at 08:39
  • 2
    thanks @dave-c, use Process Explorer Search (Process Explorer > ctrl + F > file name) find PID using your file and kill it – Guillaume Vincent May 28 '15 at 08:07
  • Whenever I switch to another branch immediately after I stop the project from running, I get the same error and all I do is close Visual Studio and click on "Yes" to try again and it works. – Emil Filip Nov 23 '16 at 12:40
  • In Windows 10: I have to kill git processes running from Task-Manager(Ctrl + Shift + Esc), to do away with `Unlink of file Failed` issue. – Abhijeet Dec 22 '16 at 06:20
  • 1
    This is very inconvenient if you've opened a terminal *inside* your IDE (IntelliJ IDEA in my case) and the IDE is the program that's using the file. – herman Jan 13 '17 at 12:49
  • 1
    The process locking the file in my case is `git.exe`. This is also the process being blocked by the file being locked (silly). I guess when it's finished it might unlock the files, and I can delete them, or something, but the problem is that the `git svn clone` has been running for a week now, and it's stopping the process when it hits a locked file... annoying... – Svend Hansen Aug 07 '17 at 06:43
  • 34
    On Windows, if you don't have ProcessExplorer available or don't want to download it, you can run (Windows + R) "resmon", go to the CPU tab and search the file name in the "Associated Identifiers" textbox. – Raphael Oct 16 '17 at 17:51
  • 3
    @Raphael I think you probably may have referred to "Associated Handles" in the CPU tab of Resource Monitor on Windows 10. Apart from that in my case the file in question was not listed and I still had the problem. To resolve it I simply restarted File Explorer which by the looks still had a handle on it without showing up in the "Associated Handles" list. – Manfred May 06 '18 at 02:45
  • TY in my case I only needed close jaspersoft report to work it you clarify what was happening – DemonFilip May 18 '18 at 11:50
  • 1
    @Manfred Yes, "Associated Handles". The other was a mistranslation on my part. :) – Raphael May 21 '18 at 19:48
  • 16
    If you use `GitKraken` you might get this if you perform commands like a rebase – mtpultz Jul 12 '18 at 19:21
  • In my case it was the explorer.exe itself which i had to restart. Thanks Dave for the tip with the ProcessExplorer – Tim Schmelter Jul 22 '19 at 10:00
  • 14
    The problem occurs for me when GitKraken is opened. – Marcin Kulik Feb 17 '20 at 10:35
  • Thanks. if anybody gets this problem try to close on program opening. My case is on window system. – Le Nguyen Dec 03 '20 at 07:58
  • 1
    Having to close VS every time I need to change a branch is a little bit insane isn't it? – riv Apr 05 '21 at 12:43
  • Thanks. I only needed the "This could mean that another program is using the file", meaning it is still running. – AyukNayr Jun 07 '21 at 07:16
  • In my case I had two instances of the Windows terminal open. One was running the Python application I was working on. I just closed it. Thanks. – AfriPwincess Apr 12 '22 at 10:59
  • Answer is spot on. In my case it was a jar file that was stuck and my server was up and running and using that jar file. Shutting down the server worked – Anurag May 13 '22 at 13:06
  • In my case it was MATLAB which was using the file even though the file was not opened. By closing the MATLAB and executing a force checkout `git checkout -f master` solved my issue. – Salah Nov 25 '22 at 06:22
  • For my case in Windows, the files that was not able to unlink had other access rights (admin) and the only why to solve it, was to manually delete them in Explorer (given the admin rights at given point). Then during pull re-create those again. – Andreas Mattisson Feb 15 '23 at 09:55
  • I had to do a full reboot of Windows for Windows to release the file. I closed everything and could not figure out why Windows still had the .git files locked. One more reason to switch to Linux, as Linux doesn't do this... – Gabriel Staples Jul 27 '23 at 19:13
  • How to use and find the Resource Monitor / Process Explorer, which others are mentioning in the comments: [Super User: Find out which process is locking a file or folder in Windows](https://superuser.com/a/643312/425838) – Gabriel Staples Jul 27 '23 at 19:27
363

I had this issue and solved it by the command : git gc The above command remove temp and unnecessary files. (Garbage collector.)

Nikita R.
  • 7,245
  • 3
  • 51
  • 62
khilo
  • 3,793
  • 1
  • 12
  • 11
  • 60
    `git pull` initiated `git gc` automatically, and gc tried to access some .pack and .inx files. These were hold by "windows git" process, that owned `git pull` command. Running `git gc` manually and than - `git pull` really resolves the issue. – Nikita Apr 27 '16 at 10:23
  • 8
    While running `git gc` i got the same error again when `gc` was in progress – Prakash K Oct 25 '16 at 13:33
  • 6
    "Running git gc manually and than - git pull really resolves the issue." This helped. Thank you Nikita. – Sangam Uprety Jan 03 '19 at 08:35
  • 1
    This helped me as well. Ran this on Windows with Git Bash – Ascalonian Jan 07 '19 at 12:44
  • After starting the git pull I got this error. I pressed CTRL+C to stop the pull, run the git gc and then git pull again, the problem had disappeared and nothing was broken by interrupting the pull. – daniel sp Jan 06 '20 at 14:12
  • Ran this command because I was trying to revert a commit using `git reset --hard HEAD^` and worked out perfectly. – claudiodfc Jun 16 '21 at 09:07
  • What worked for me was to run git bash as an administrator and run git pull – dmeehan Mar 14 '23 at 10:40
112

this solution from here worked for me:

This is a Windows specific answer, so I'm aware that it's not relevant to you... I'm just including it for the benefit of future searchers.

In my case, it was because I was running Git from a non-elevated command line. "Run as Administrator" fixed it for me.

Community
  • 1
  • 1
sepehr
  • 5,479
  • 3
  • 29
  • 33
  • 3
    Worked for me. Ran VS as admin and then launched the command prompt from VS – Sentinel Jan 04 '16 at 14:54
  • 5
    I: typed `Ctrl+C` to terminate the very long list of unlinkable files; exited gitbash, my IDE, and the GitHub Windows application; restarted gitbash in elevated "Run as Administrator" mode; and ran `git gc --aggressive`. Took a while to complete, but made it through with no errors. – Mark McClelland Jul 22 '16 at 16:49
  • If you believe this is a duplicate you should flag it as such, not add an answer that points to another answer – Liam Jan 20 '17 at 14:04
  • 3
    @Liam This question is older but this answer was only on the other question, and there isn't only one answer to this question which makes it harder to just set one as duplicate. – sepehr Jan 21 '17 at 11:14
  • This worked pretty well for me. None of the previous answer did. Thanks a lot. – Roberto Rodriguez Mar 14 '17 at 20:04
47

I encountered this issue while doing a git pull.

I tried git gc and it resolved my problem.

cgmb
  • 4,284
  • 3
  • 33
  • 60
Rahul Chandran
  • 471
  • 4
  • 2
  • I ran into this issue on Windows machine and this solution didn't initially work. However once I ran this command from an admin command prompt it did resolve my issue. – William W Jan 30 '19 at 04:25
27

In my case there are no processes touching the file or directory. Maybe it happens if the path is very long, because an operating system restriction (windows). Try enabling the longpath support flag in the global git configuration as indicated below:

git config --global core.longpaths true

or try to setting the yes/no answer flag if it is not conflictive for you

set GIT_ASK_YESNO=false

If the path is too long, I've not found a successful solution.

Liam
  • 27,717
  • 28
  • 128
  • 190
andhdo
  • 945
  • 8
  • 11
  • 7
    I do not know why but setting "git config --global core.longpaths true" was helpful for me. – Maxim Sep 27 '14 at 12:25
  • `GIT_ASK_YESNO`, can't find info about this one. Where does it come from ? – majkinetor Jun 22 '16 at 12:31
  • see: http://stackoverflow.com/questions/3698369/git-rebase-got-unlink-of-file-failed-error for example; I don't now if it works with recents versions of git (2.9) – andhdo Jun 23 '16 at 18:21
  • `core.longpaths` with additional: reset --hard prune gc worked for me - but had to close AndroidStudio to not mess up with its build in tools – bovquier Jun 20 '19 at 10:01
19

This might be useful for someone; if all the above didn't work for you, follow these steps:

  1. Close your IDE (mine was Eclipse, not sure if it applies to Intellij and others) or any other app that might be using git.

  2. Open git from the command line (in my case I had git bash) and run git gc as mentioned by others.

This did the magic for me.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
papigee
  • 6,401
  • 3
  • 29
  • 31
16

As i am using gitkraken and command prompt, i ran into the same issue. And then i run git gc command it resolved my problem. So i am happy and want share some of the points which might be helpful.

What git gc will do ?

git gc removing unreachable objects which may have been created from prior invocations of git add.

When to run git gc?

From doc, users are encouraged to run this task on a regular basis within each repository to maintain good disk space utilization and good operating performance.

How to make it auto-configurable?

Some git commands may automatically run git gc; see the --auto flag below for details. If you know what you’re doing and all you want is to disable this behavior permanently without further considerations, just do

git config --global gc.auto 0
Ramesh Papaganti
  • 7,311
  • 3
  • 31
  • 36
  • I closed gitkraken and my external git command line windows, then executed `git gc`, and everything appears to be back to normal. – JWess Apr 11 '22 at 21:59
12

I had this kind of issue on Windows 7 and it turned out to be due to some orphaned git.exe process.

To solve it, open Task Manager and kill all git.exe processes.

Since git commands are short-lived, you should normally never see any git.exe in Task Manager. When they are there, it usually means something is wrong, and you should kill those processes.

jakub.g
  • 38,512
  • 12
  • 92
  • 130
11

I tried git gc and it resolved my problem.

Mukesh Yadav
  • 249
  • 2
  • 6
10

On Windows 8: I ran git gc and it said git gc was already running, I ran git gc --force and the garbage collector ran.

I could then switch branches and merge without any issues, try git gc --force.

Perhaps the gc process didn't stop gracefully for one reason or another.

Michael
  • 8,362
  • 6
  • 61
  • 88
Mark Howard
  • 526
  • 4
  • 13
9

I got this problem in Windows. I closed my IDE (Android Studio) and selected YES in git shell. It worked.

vovahost
  • 34,185
  • 17
  • 113
  • 116
9

I was able to solve this by opening Powershell as Administrator and from there git checkout <branch_name>

Bar Horing
  • 5,337
  • 3
  • 30
  • 26
  • 1
    Thanks @bar-horing. This helped me identify it as a permission issue. Once I fixed the windows permission for that folder it worked. – Kumar Jan 17 '19 at 23:29
  • Me too, run cmd as administrator on Win Server 16 :) – geedubb Sep 09 '20 at 11:23
8

In my case (Win8.1, TortoiseGit running), it was the process called "TortoiseSVN status cache" that was locking the file.

Killing it allowed me to run "git gc" without any more problems. The above process is fired up by TortoiseGit, so there is no need to manually restart it.

nickD
  • 141
  • 1
  • 4
4

I had this issue with .tmp files in /.git/objects/pack folder. I think something had failed during a push or pull, so I removed these temporary files and reset the HEAD to my last commit. Not sure if this is advised but it worked for me. Also git count-objects -v gave me a list of the .tmp files that didn't belong in the pack folder.

Or to suppress the y/n messages in windows git open cmd.exe and run:

SETX GIT_ASK_YESNO false

seen here: https://twitter.com/petercamfield/status/494805475733807104

mistaecko
  • 2,675
  • 1
  • 20
  • 18
blueberries
  • 71
  • 1
  • 4
  • After having run `git gc`, `git count-objects -v` helps in identifying leftover temp objects in the `pack` folder. – mistaecko Oct 19 '16 at 18:47
4

I faced same issue while doing 'git pull'. I tried manual housekeeping git command 'git gc' and it resolved my problem.

doitright
  • 115
  • 3
  • 5
  • 4
    While this **is** an attempt at an answer, it doesn't add anything that isn't already covered by other answers (such as [this one](//stackoverflow.com/a/26229658/2747593)). If you are trying to confirm that this solution works, or wish to thank the author of that answer, please do not post an answer to do so. After you gain some more [reputation](//stackoverflow.com/help/whats-reputation), you will gain sufficient [privileges](//stackoverflow.com/privileges) to [upvote](//stackoverflow.com/privileges/vote-up) answers you like, which is the Stack Overflow way of saying thank you. – Scott Weldon Nov 10 '16 at 16:25
  • **Flaggers / reviewers:** [For duplicate answers such as this one, downvote and/or flag for moderator attention, don't delete!](//meta.stackoverflow.com/a/310097/2747593) – Scott Weldon Nov 10 '16 at 16:46
3

After trying various solutions finally git clean -f helped me.

EDIT: I hit the problem again few times - closing all processes dependent on git seems to help (like gitbash, Eclipse IDE, etc.)

FazoM
  • 4,777
  • 6
  • 43
  • 61
3

I ran into this issue in Windows, you might want to run the git bash as an administrator and then perform the desire commands, that solved the issue for me.

Pablo Fallas
  • 781
  • 2
  • 7
  • 17
3

After run command

git rm -rf foo.bar

I see error

Unlink of file 'foo.bar' failed. Should I try again? (y/n)

Because another program is using this file. For example, when I run Java web application in debug model or run web application on server, I can't delete log file. Turn off application sever (or turn off debug process), re-try

git rm -rf foo.bar

I see file has been deleted.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
3

if your os host is linux/unix-like, you can run git gc --force to stop the file handler which take use of git files,

else if you're working on windows, you can stop git process by Task Manager or Process Manager. It seems that there are another git process taking use of your git files. Just kill the git process

Fan Yer
  • 377
  • 3
  • 7
2

I ran into this issue running git Bash and Eclipse EGit at the same time. Solution: close Eclipse. Also wouldn't hurt to run git gc as @khilo mentioned.

jhyry
  • 91
  • 6
2

As stated above, something else is holding the files. Thing is that program doesnt look suspicious for us. I was trying to do a git pull from console, while having GitKraken opened. Closing GitKraken fixed the problem.

100r
  • 1,099
  • 1
  • 12
  • 24
2

If you're using Docker and running Windows 10, you may want to stop the container(s) where the file may be running at. To show the statuses of your containers, run

docker ps -a

To stop them, simply run

docker stop <container name or container id>

This worked for me as I am running my local files using a .sh file

Norseback
  • 193
  • 2
  • 4
  • 16
2

Worked for me, Tried on windows:

Stop your server running from IDE or close your IDE

Intellij/Ecllipse or any, it will work.

Codiee
  • 3,047
  • 2
  • 17
  • 18
1

After none of the above answers seemed to work, running git fetch -p did the job for me.

https://git-scm.com/docs/git-fetch

Jamie Wade
  • 277
  • 3
  • 19
1

I tried every single tip on this page and nothing helped. I was doing a git fetch and a git reset --hard origin/development gave me the unkink error. I couldn't reset to the latest commit.

What helped was checking out another branch and then checking out the previous branch. Very strange but it solved the problem.

T3rm1
  • 2,299
  • 5
  • 34
  • 51
1

If closing your IDE and running various git commands listed here won't help, try manually killing all running Java processes. I had a Java process probably left over from eclipse that somehow kept a configuration file open.

Marc.S
  • 31
  • 7
1

For my Spring Boot project I ran into this errror when attempting to run git stash.

The solution was to killed all of the Java processes running on my machine.

0xCourtney
  • 83
  • 1
  • 9
1

In my situation the cause was that I had ran Visual Studio as an administrator earlier in the day. When I went to switch branches the error occurred.

I use GitExtensions and I run it as a regular users and files created by VS while running as an administrator ( run as Administrator, still me though) were not accessible. This was the root of the cause for me. I'm not sure why it happened in this situation because I regularly run VS as administrator on my primary development environment and have not experienced this problem before. But for some reason today the above scenario caused any files generated by VS during that session to be un-editable...you can read them, but not change or delete them and that is exactly what Git is going to do when you switch branches.

To resolve the problem for Git I had to start windows explorer as administrator and take ownership and re give myself permissions to files and directories in question. I don't fully understand why this as I was running explorer as Administrator too but it did.

You can do this for the files individually but I ended up doing this process for the root folder of the branch.

So the exact steps I did to fix the problem are: Went to the top of the branch in windows explorer, right clicked properties of the directory, select the security tab, advanced options which opens the Advanced Security Settings dialog, Selected change ownership at the top (it might show you already own it), I re-selected myself. Then it returns you to the 'Advanced Security Settings' dialog, at the bottom of remember to check the box 'Replace all object...' Then click apply The system cycles through each file and folder fixing ownership and that resolved the problem for me.

For me it had locked out my migrations folder and wouldn't let me switch branches...so I originally just fixed that directory and sub tree which allowed me to switch branches but I had closed VS trying to resolved the issue. When I reopened VS I did not run VS as the Administrator and I found that could not compile the solution now! It was the same\similar issue. It could not compile the solution as all of the obj directories were now non editable... so that is when I did the above steps and all was good again.

Hope this helps someone... ;)

Shiloh
  • 25
  • 5
1

As mentioned in the above answers, yes file must be used maybe by other java processes, simple way to get rid-off this is, just kill all java processes then execute git pull I did this, it works for me.

For windows kill all java processes:

taskkill /F /IM java.exe

For Linux kill all java processes:

kill -9 `pidof java`
Dharman
  • 30,962
  • 25
  • 85
  • 135
ravibeli
  • 484
  • 9
  • 30
1

In my case, I was trying to run git fetch --all from within GitKraken's terminal. I had to close Gitkraken and run the command from Windows Terminal in order for it to work.

Ricardo Yubal
  • 374
  • 3
  • 8
  • This was exactly my issue as well. After closing GitKracken the `gc` that `git` was trying to run was successful. – WBuck Apr 18 '23 at 23:55
0

This may be a separate gitk window running to see some git history.

Just close that window to fix that problem.

weinerk
  • 472
  • 6
  • 12
0

On Windows, saw this error on a git clone of a (fairly large) repo. Closed SmartGit and paused my backup software (CrashPlan), and after that it worked. Not sure which of the 2 did the trick, but if running either, this might do it for you too.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
0

I had the same issue while doing a git pull and as stated above, it was because of a program that was holding those files and was not allowing a git pull. Closing the program helped. Usually, the IDE (like Eclipse) from where the files are being checked-in will be holding it in the background. Closing the same and re-running git pull solved the problem for me.

0

I had this same error and closing the app which had the file open solved it. I was able to go back and press "Y"

Paul McDaniel
  • 1,471
  • 1
  • 7
  • 6
0

If you are developing a web application, a common reason is to forget shutting down the server. For example this could be a simple Node.js process, or on windows your IIS process running more unobtrusive as background process.

alex3683
  • 1,460
  • 14
  • 25
0

in my case there was a copy or replace window for that particular file open and hence the unlink error. i closed it and there is no unlink error

schin chan
  • 59
  • 1
  • 5
0

I solved this problem by restarting PHPStorm (insert your programm instead) as administrator

0

You might see that error less often with Git 2.29 (Q4 2020): the "unlink" emulation on MinGW has been optimized.

See commit 680e0b4 (17 Aug 2020) by Jeff Hostetler (Jeff-Hostetler).
(Merged by Junio C Hamano -- gitster -- in commit 5a04826, 19 Aug 2020)

mingw: improve performance of mingw_unlink()

Signed-off-by: Jeff Hostetler
Signed-off-by: Johannes Schindelin

Update mingw_unlink() to first try to delete the file with existing permissions before trying to force it.

Windows throws an error when trying to delete a read-only file.
The mingw_unlink() compatibility wrapper always tries to _wchmod(666) the file before calling _wunlink() to avoid that error.
However, since most files in the worktree are already writable, this is usually wasted effort.

Update mingw_unlink() to just call DeleteFileW() directly and if that succeeds return.
If that fails, fall back into the existing code path to update the permissions and use _wunlink() to get the existing error code mapping.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I'm on 2.30.1.windows.1 and I still get this a lot. – harvzor Apr 14 '21 at 20:04
  • @harvzor Are you using the Git for Windows or Git through a WSL2 Linux session? – VonC Apr 14 '21 at 20:07
  • Typing `where git` returns `C:\Program Files\Git\cmd\git.exe` so I'm pretty sure it's Windows, that's probably why the version also has "windows" in it. I had VS Code open when I used git so likely the top rated answer is correct in regards to Code locking the files up. – harvzor Apr 14 '21 at 22:01
  • @harvzor I agree, especially considering that VSCode auto-fetch Git repositories (https://github.com/microsoft/vscode/issues/40282) – VonC Apr 14 '21 at 22:45
0

For me I had to go in my .git folder and delete index.lock file. After that, I can undo as usual.

ItaiRoded
  • 142
  • 7