[git] 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?

This question is related to git git-push git-checkout

The answer is


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


Pulling may have created local change.

Add your untracked file:

git add .

Stash changes.

git stash

Drop local changes.

git stash drop

Pull with sudo permission

sudo git pull remote branch


FWIW - I had a similar problem and I'm not sure if this alleviated it (beyond the permission mod): Closing Eclipse that was using the branch with this problem.


git reset --hard

Worked for me


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.


I had the same issue and none of the solutions above worked for me. I deleted the offending folder. Then:

git reset --hard

Deleted any lingering files to clean up the git status, then did:

git pull

It finally worked.

NOTE: If the folder was, for instance, a public folder with build files, remember to rebuild the files


After checking the permission of the folder, it is okay with 744. I had the problem with a plugin that is installed on my WordPress site. The plugin has hooked that are in the corn job I suspected.

With a simple sudo it can fix the issue

sudo git pull origin master

You have it working.


I think the problem may be with the ownership to the folder so set it to the current user ownership

sudo chown -R your_login_name /path/to/folder
You can find the solution [here][1]

sudo chown -R $USER:$USER .

Did the job for me.


sudo chmod -R ug+w .;

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


Some files are write-protected that even git cannot over write it. Change the folder permission to allow writing e.g. sudo chmod 775 foldername

And then execute

git pull 

again


Also remember to check permission of root directory itself!

You may find:

drwxr-xr-x  9 not-you www-data  4096 Aug  8 16:36 ./
-rw-r--r--  1     you www-data  3012 Aug  8 16:36 README.txt
-rw-r--r--  1     you www-data  3012 Aug  8 16:36 UPDATE.txt

and 'permission denied' error will pop up.


This is an old question, but this may help Mac users.

If you are copying files from Time Machine manually, instead of restoring them through Time Machine, it'll add ACLs to everything, which can mess up your permissions.

For example, the section in this article that says "How to Fix Mac OS X File Permissions" shows that "everyone" has custom permissions, which messes it all up:

Bad permissions, from http://dreamlight.com/how-to-fix-mac-os-x-file-permissions

You need to remove the ACLs from those directories/files. This Super User answer goes into it, but here's the command:

sudo chmod -RN .

Then you can make sure your directories and files have the proper permissions. I use 750 for directories and 644 for files.


I get this error, and other strange git errors, when I have a server running (in Intellij). Stopping the server and re-trying the git command frequently fixes it for me.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-push

Fix GitLab error: "you are not allowed to push code to protected branches on this project"? ! [rejected] master -> master (fetch first) Git - What is the difference between push.default "matching" and "simple" error: src refspec master does not match any Issue pushing new code in Github Can't push to GitHub because of large file which I already deleted Changing the Git remote 'push to' default What does '--set-upstream' do? Git push hangs when pushing to Github? fatal: 'origin' does not appear to be a git repository

Examples related to git-checkout

How can I switch to another branch in git? Git checkout - switching back to HEAD What is git tag, How to create tags & How to checkout git remote tag(s) How can I move HEAD back to a previous location? (Detached head) & Undo commits error: pathspec 'test-branch' did not match any file(s) known to git git checkout all the files How can I check out a GitHub pull request with git? "Cannot update paths and switch to branch at the same time" error: Your local changes to the following files would be overwritten by checkout Git command to checkout any branch and overwrite local changes