Git – How to Solve `’git/index.lock’: File exists` Error in Submodules

Published on November 7, 2017Less than a minute read

While I was working on this blog, I faced with that error several times. In this post, we’ll look into a quick hacky solution for this issue.

The Issue

So, you’ve a Git Submodule as part of your repository. Everything’s fine. But then – your task runner cleans up that submodule directory and for some reason – it’s removed. Suddenly, when you try to add changes to the staging area, it fails. Then, you figure out that you cannot perform the common Git commands because it keeps failing.

Thereafter, you decide to investigate the terminal and you face with that error:

The error - 'git/index.lock': File exists
The error – ‘git/index.lock’: File exists

If you’re familiar with this situation – you probably should keep on reading.

Note: I’m not a fan of hacky solutions, but the next steps solve the issue quickly. Follow these just in case you don’t know how to proceed with your issue.

The Solution

Disclaimer: I don’t take responsibility for the outcomes of your actions. You must back up your project before starting these steps.

Let’s assume the removed submodule directory was called "public".

So, the first step will be to navigate to the root directory of the repository. Then, we execute the following command for removing the temporary index.lock file:

rm -f .git/index.lock

Now, we verify there are no leftovers in the submodule directory:

rm -rf public

In the next step, we remove the submodule directory from Git index file:

git rm public

To end that, we reattach the submodule repository:

git submodule add -f -b master ORIGIN_REPOSITORY_URL public

Note: Replace ORIGIN_REPOSITORY_URL with the remote’s repository URL.

That’s all – the issue was solved. 😊

Follow Me

Join My Newsletter

Get updates and insights directly to your inbox.

Site Navigation


© 2024, Nitay Neeman. All rights reserved.

Licensed under CC BY 4.0. Sharing and adapting this work is permitted only with proper attribution.