Git push error: Unable to unlink old (Permission denied)

In the remote server I have a post-receive hook set up in order to make a git checkout of my repository:

#!/bin/sh
GIT_WORK_TREE=/var/www/<website> git checkout -f

But when I make a push from my local machine to the git repository in the server, I get the following error messages:

remote: error: unable to unlink old '<file>' (Permission denied)

This appears many times over, one error message for almost every file.

However I have a README.txt file that I'm able to change using git, here are its permissions:

-rw-r--r--  1 <serverusername>  <serverusername>  2939 Aug  2 10:58 README.txt

But other files with exactly the same owner and same permissions, give me that error.

In another local repository for another website, I have the files with my local machine username as owner, and when I push to the remote server it respects the remote server owner of the files and works like a charm.

Obviously it seems a permissions related error, but I can't find a way to fix it, any suggestions?


Solution 1:

When you have to unlink file, you have to have permission 'w' for directory, in which file is, not for the file...

Solution 2:

sudo chmod -R ug+w .;

This command would fix the issue. It gives write permissions to the folder.

Solution 3:

If you are using any IDE most likely the problem is that file was used by some process. Like your tomcat might be using the file. Try to identify that particular process and close it. That should solve your problem.