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

November 7, 2017 2 min read Git

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:

`'git/index.lock': File exists` Error regarding Git Submodules

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_REPOSITORYURL with the remote’s repository URL.

That’s all - the issue was solved. 😊

✉️ Subscription
Only new content, no spam.
Aspire. Accomplish. Online courses from $9.99