In this article, we will see how to solve unable to index file Permission denied error which comes up during when we add the changes using git add command. It is not very often that when I run git add command then I would end up in getting an error but last night when I was trying to add my changes after updating the data excel sheet then I didn't helped myself noticing the error which was also a sudden sock for me as I didn't expected it.
But it did showed up due to my little mistake happened at that time which I am going to explain in below section. The same mistake also led me write this article so that it will help you folks as well if you are indeed also facing the same error. So without further delay let's begin !!
Solved "git add shows unable to index file Permission denied" error
As I said earlier, when I tried to add my changes using
git add command then I noticed
unable to index file, Permission denied error on the output as you can also see below.
Gittool installed on
cyberithub@DESKTOP-PX1TGHA MINGW64 ~/Desktop/repo/atlantis-example (master) $ git add . error: open("~$data.xlsx"): Permission denied error: unable to index file '~$data.xlsx' fatal: adding files failed
While the above error could show due to many reasons but most of the time it is because the file in which you did the changes is still open and not yet closed. Hence this error. So to solve this, you just need to close the file and then try adding the changes again. This time you will notice that it did not throw that error again as you can see below.
cyberithub@DESKTOP-PX1TGHA MINGW64 ~/Desktop/repo/atlantis-example (master) $ git add .
Now if you check and verify the status using
git status command then you will notice that the changes are added successfully and hence the error is resolved now.
cyberithub@DESKTOP-PX1TGHA MINGW64 ~/Desktop/repo/atlantis-example (master) $ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: data.xlsx modified: main.tf
Above case is shown for an excel sheet file but for you it might be different file opened in some code editor such
Visual Studio Code,
IntelliJ IDEA etc. In those cases also, you will see the same error. So to fix the error you need to do the same thing there as well. Just close the file in the code editor and then try to run git add again. It will surely work.
Most of the time above solution works however there are some cases in which above solution might not work and you still end up in getting that same error. In that case, you can check your user permission and see if you are allowed to run
git add command. If it's indeed a user permission issue then you can fix this by running
git add command with
sudo access. In some cases, this solves the problem as well.
Then there is another scenario that could be possible is that user don't have enough permission to edit the file. This means that file is created by another user or the permission to the file is such that other users apart from owner of the file won't able to perform any operation on that file. In that you can either change the user permission on that file by using chmod command or you can change the ownership of the file by using chown command.
Above solutions should be enough to solve your git add error as well. Hope it helped. Please let me know your feedback in the comment box !!