270

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?

pocesar
  • 6,860
  • 6
  • 56
  • 88
rfc1484
  • 9,441
  • 16
  • 72
  • 123

17 Answers17

438

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

Jan Marek
  • 10,390
  • 3
  • 21
  • 19
  • 84
    Indeed that was the problem, I fixed it using `sudo chmod -R g+w` over the guilty folders. – rfc1484 Aug 02 '12 at 10:02
  • 1
    OMG thanks. Was so annoyed with thinking permissions were correct on the the file. Makes sense the updates are actually more like `mv` actions than just overwrites. – doublejosh Nov 24 '12 at 01:01
  • 1
    Changing dir permissions did work for me (thanks!) but it's odd because I could manually overwrite the files in question via sftp without any trouble. Strange that when git tried to do same it couldn't. – Jonathan Stark Jul 30 '14 at 18:13
  • 4
    Also keep in mind that if you still have the file opened this error will appear as well. Had the same error and that was why i was not able to push my changes in. – Matias Nov 03 '16 at 14:05
  • what is g+w ? @rfc1484 – Fai Zal Dong Nov 16 '16 at 07:45
  • 2
    The first character of the `ls -l` display indicates the file type and is not related to permissions. The remaining nine characters are in three sets, each representing a class of permissions as three characters. The first set represents the user class. The second set represents the group class. The third set represents the others class. The `g+w` in chmod gives the group set (the `g` parameter) permission to write (the `w` parameter) – rfc1484 Dec 04 '16 at 09:59
  • In addition to `w` permission on a directory, a user has to own or belong to group that has access to that directory – Lukasz Dynowski Apr 06 '18 at 13:08
  • In windows, uncheck "Read-Only" attributes of files. – Hessam J.E Sep 01 '19 at 09:23
  • 1
    for me, I use this: `find . -type d -exec chmod 775 {} \;` – ddwolf May 08 '20 at 09:02
  • In my case, I had to run `sudo chgrp ` – fungusanthrax Jun 23 '21 at 17:28
  • How on earth does this incoherent sentence have 400+ upvotes? – Andor Németh Feb 02 '22 at 12:22
101
sudo chmod -R ug+w .;

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

Rajendra kumar Vankadari
  • 2,247
  • 1
  • 16
  • 16
50

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.

Krishna Gollapudi
  • 1,609
  • 16
  • 13
49
sudo chown -R $USER:$USER .

Did the job for me.

BARJ
  • 1,543
  • 3
  • 20
  • 28
  • yes, it did. My current user did not have permission to the directory but the `root` so. – Decoy Mar 10 '23 at 07:20
38

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]
pylipp
  • 181
  • 3
  • 15
Soumitra Sarkar
  • 610
  • 6
  • 9
18

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

wcyn
  • 3,826
  • 2
  • 31
  • 25
  • Thanks, nothing else was working for me either, deleting it seemed to be the only option. – math0ne Feb 05 '18 at 18:15
  • In my case, that offending folder is .git – Tushar Kathuria Apr 10 '19 at 10:10
  • Are you mad? By following your suggestion i have totally lost my current work. It was crazy for me. totally bad idea. – S.M.Fazle Rabbi Dec 14 '21 at 12:37
  • Well, you can't blame the author because you possibly had so much uncommitted code and hadn't made any recent pushes to save your work @S.M.FazleRabbi. Best to not get so attached to your code, anyone can come and rewrite it anyway... – NotoriousPyro Mar 08 '22 at 08:47
12

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.

cellepo
  • 4,001
  • 2
  • 38
  • 57
  • 1
    Similarly I've gotten this error when a version controlled CSV file was open in Excel. Simply closing Excel resolved it. This is probably true of other applications too on windows and probably depends upon how the program marks the file as open during editing. – Carel Feb 15 '19 at 11:55
  • 1
    You put me on the right track, thank you. I had a console with a `git diff` sitting open. q out of that and the issue went away. – rosscova Jun 26 '23 at 05:10
9

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.

Community
  • 1
  • 1
kylesimmonds
  • 183
  • 2
  • 11
  • Wonderful answer. I was looking exactly this for mac ACL problem when files are manually copied. – Ankit Bhatnagar Sep 22 '20 at 06:18
  • 1
    In addition to file permissions and ACLs, there are also locks in macOS. Locks can happen to be applied when copying files from a USB flash drive. They can be recursively unlocked with `chflags -R nouchg /path/`, as described at: https://superuser.com/a/40754/199930, and found with `find . -type f -flags +uchg`, as described at: https://coderwall.com/p/-3hwvg/find-locked-files-in-osx-terminal – 0 _ May 13 '21 at 08:33
  • Thank you! Ioannis and kylesimmonds answers are the fix – sherrmann Oct 13 '21 at 13:02
4

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

Carmela
  • 687
  • 1
  • 8
  • 15
3

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.

cadavre
  • 1,334
  • 1
  • 18
  • 34
3

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.

Phil Carter
  • 1,265
  • 1
  • 10
  • 9
3
git reset --hard

Worked for me

k4dima
  • 6,070
  • 5
  • 41
  • 39
3

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

user2858738
  • 520
  • 4
  • 15
  • it's all about permission of the local files there's nothing to do with git I have just run command with sudo and it worked so didn't require all these steps – raviramani Sep 27 '19 at 06:34
3

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.

pensebien
  • 506
  • 4
  • 16
2

In my case, I had to give read, write and execute permissions to all group members on the folder. This is because the Git user is a group member but not owner.

sudo chmod -R g=rwx <folder>
Nate311
  • 91
  • 7
0

The easiest way on windows:

1)Go to Task Manager

2)End File Explorer tasks

3)In task manager, go to "File" tab and then click on "Run new Task"

4)in the popup write explorer.exe

Issue resolved.

KaramJaber
  • 851
  • 5
  • 13
  • 24
0

Make sure the file in question isn't opened in another application (I had this with Excel)!

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120