This error happens because inside the directory .git
there is a file called index.lock
, after deleting this file you will be able to run git commands.
But after all, what is index.lock.?
The index.lock file is created every time a change occurs in the original index file.
To ensure that the original file doesn’t end up corrupted, git creates index.lock with all the new information, saves and renames it to index, then overwrites the original.
The index file in turn is a binary file (usually maintained
in . git / index) containing an ordered list of all paths of
files being versioned by git, each with permissions
and the SHA1 of the blob of this file.
That is, all files being monitored by git are listed in the index along with their path, permissions (read, write, executable) and SHA1 hash.
You can see the contents of the index file with the command git ls-files
.
To display the contents of the index for all the versioned files run:
$ git ls-files -s
100644 d1bed128fa83b3ed094d8387f4b8f19c019e2ae8 0 crud/.gitignore
100644 62dff0d03aeddb3cfc3e10ded315193fcf00f4c4 0 crud/app.js
100644 86091205bdb895f94d904c1c1a00457e2a3355f0 0 crud/bin/www
100644 afa3b3b360b58def97f483a7163032abba849fc9 0 crud/db.js
100644 311524c74e19968b8a580c19ed663b4e2e96a34e 0 crud/package-lock.json
100644 da91bb97990abbf27edbd2c93046054e458d0e4e 0 crud/package.json
100644 9453385b9916ce9bc5e88d2f5d8cd8a554223590 0 crud/public/stylesheets/style.css
The first column with the numbers 100644
represents permissions, the second column is the SAH1 hash, the third is the Stage number (this is used to represent merge conflicts) and the fourth column is the path of your file.
For more details on what I commented above a look in this article and on the eBook Git Community Book.
Now that we understand what the index and index.lock files are for, I think it’s easy to see that whenever we commit, merge, we try to solve a Conflict problem or any other command that git needs to recreate the index it will generate this index.lock file and until this command is completed it will not save and replace the original index file, for this reason git won’t let you run another command while index.lock exists, because it understands that there is another command running at that time.
If it doesn’t work out delete the lock and rebut the machine and try again should drop. I can’t explain why but the cause is this.
– Rafael Salomão
@Marcelo, if any of the answers helped you understand the problem don’t forget to mark it as right, so help others with the same question to find the answer more easily.
– Murilo Portugal
@Marcelo, I don’t understand why in the last few minutes you took the answer as correct, just to avoid receiving the reward? you know that after you put a reward on a survey, even if you don’t give it to anyone as the correct answer it doesn’t come back to you right?
– Murilo Portugal