71

I have this problem when I try to push in Git:

error: insufficient permission for adding an object to repository database ./objects

fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To ssh://<repo url>/<repo dir>
 ! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to 'ssh://<repo url>/<repo dir>'

I've had this before sporadically and we've always had to solve it by each user sshing to the repo and setting group permissions on all the files therein with

chmod -R g+w *

This was never a satisfactory solution and now it's bitten us in the arse as one of the guys is away and no-one knows his repo user's password. So, I'm trying to solve it properly.

The error seems to occur when someone tries to push up a change that will alter a repo dir that is owned by another user (hence setting the group write option above). I've done a bit of googling around this and have found a couple of solutions being discussed (neither of which worked for me)

  1. make sure that the group that the repo dirs are shared with is each users' primary group (i believe that is the case already: each user has only one group so that must be their primary group, right?)

  2. git repo core.sharedRepository setting, as detailed here: Git: Can't push from one computer I changed this but it didn't make any difference. Do I need to reload the config or something to actually effect the change?

Here's what my repo config looks like atm:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = true
        sharedRepository = all
[receive]
        denyNonFastForwards = True

Grateful for any advice or suggestions! max

3
  • Can you provide minimal test repo that produces that problem? I can get it always if I have a .GIT directory (uppercase) in the repository. Dec 20, 2014 at 8:14
  • This can also be caused by the disk being full, and is not necessarily a permissions problem!
    – GH05T
    Oct 3, 2019 at 21:35
  • This works fine for me: stackoverflow.com/questions/39362918/…
    – akbar
    Sep 26, 2021 at 11:15

16 Answers 16

41

I had this error for two weeks, and the majority of the solutions stated 'chmod -R' as the the answer, unfortunately for me my git repos (local / remote / shared - with team) were all on Windows OS, and even though chmod -Rv showed all the files changed to 'rwxrwxrwx', a subsequent 'ls -l' still showed all files as 'rwxr-xr-x' and the error repeated itself. I eventually saw this solution by Ariejan de Vroom. It worked and we were all able to pull and push again.

On both local (the local that is having trouble pushing) and remote repos, run the following commands:

$ git fsck
$ git prune
$ git repack
$ git fsck

On a side note, I tried using Windows' native file permissions / ACL and even resorted to elevating the problem user to Administrator, but none of that seemed to help. Not sure if the environment is important, but it may help someone with a similar setup - problem team member and remote (Windows Server 2008 R2 Standard), my local (Windows 7 VM).

3
  • 2
    There's a case for this error when there is git filesystem corruption and these instructions helped correct it. Thanks
    – nkadwa
    Sep 18, 2013 at 19:50
  • @nkadwa I'm glad this could help you Sep 20, 2013 at 20:48
  • 1
    since that is the only answer also refering also to Windows. I had the exact same issue on Windows 10 as a non-privileged user. A simple git pull resolved it.
    – Markus
    Jun 20, 2017 at 9:13
30

A simpler way to do this is to add a post-receive script which runs the chmod command after every push to the 'hub' repo on the server. Add the following line to hooks/post-receive inside your git folder on the server:

chmod -Rf u+w /path/to/git/repo/objects
4
  • Thanks for this answer, I've been dealing with the same problem and just wasn't willing to set up an entire repo management package just to deal with it.
    – Kelly
    Oct 20, 2011 at 18:20
  • 3
    This post-recieve script worked for me: chown -R git:git /home/git/repositories/myrepo.git/objects/
    – fjsj
    Nov 3, 2011 at 19:32
  • 4
    could be owner problem too, if some folders/files in remote repository were modified/created by an another remote user, different from pusher Dec 21, 2011 at 3:40
  • I personally always used the same user for admin but some objects owner seemed to have been tampered with. I chowned -R to fix it Oct 7, 2014 at 8:12
9

It is a permission error. The way that was most appropriate and secure for me was adding users to a supplementary group that the repo. is owned by (or vice versa):

groupadd git
chgrp -R git .git
chgrp -R git ./
usermod -G -a git $(whoami)
3
  • 3
    Shouldn't that be usermod -G -a ... in order to avoid that the user is removed from all groups except git?
    – chris
    Feb 24, 2014 at 7:58
  • 1
    Woah...I can't believe I missed that and I'm hopeful there weren't confusing repercussions for the up-voters. Thanks, @chris
    – Alastair
    Feb 24, 2014 at 16:14
  • 1
    On my system after doing usermod -a -G ..., I had to log out and log back in again to see the change.
    – Andy J
    Jul 6, 2021 at 3:16
8

In case anyone else is stuck with this: it just means the write permissions are wrong in the repo that you’re pushing to. Go and chmod -R it so that the user you’re accessing the git server with has write access.

3
  • 9
    Please post external answers's content on stackoverflow: for the case when that external url does down. May 22, 2014 at 16:50
  • 4
    As @ThorSummoner mentioned, it is better to put the content from the blog to this post. The link is now outdated.
    – colidyre
    Dec 17, 2019 at 22:08
  • 1
    Link now entirely dead.
    – Andy J
    Jul 6, 2021 at 5:33
5

For me, this error occurred when I was out of space on my remote.

I just needed to read the rest of the error message:

error: file write error (No space left on device)
fatal: unable to write sha1 file
error: unpack failed: unpack-objects abnormal exit
4

For the permission error using git repository on AWS instance, I successfully solved it by creating a group, and assigning it to the repository folder recursively(-R), and give the written right to this group, and then assign the default aws instance user(ec2-user or ubuntu) to this group.

1. Create a goup name share_group or something else

     sudo groupadd share_group

2. change the repository folder from 'root' group to 'share_group'

     sudo chgrp -R share_group /path/to/your/repository

3. add the write authority to share_group

     sudo chmod -R g+w /path/to/your/repository

4. The last step is to assign current user--default user when login (by default ec2 is 'ec2-user', user of ubuntu instance is 'ubuntu' in ubuntu on aws) to share_group. I am using ubuntu insance on aws, so my default user is ubuntu.

     sudo usermod -a -G share_group ubuntu

By the way, to see the ownership of the folder or file just type:

    ls -l  /path/to/your/repository

'

Output:

    drwxr-x--x  2 root shared_group
(explanation please see:https://wiki.archlinux.org/index.php/File_permissions_and_attributes).

After step 3, you will see

    drwx--x--x  2 root root

changed to

    drwxr-x--x  2 root share_group 

In this case, I did not assign user 'ubuntu' to root group, for the consideration of security. You can just try to assign you default user to root according to step 4 (just skip the first 3 steps


In another way, tried the solution by :

    chmod -Rf u+w /path/to/git/repo/objects
It did not work for me, I think it should be the reason that my repository folder belong to the root user, not to Ubuntu user, and 'git' by default use the default user(ec2-user or Ubuntu user. You can try to change the user and test it.

Finally, below code definitely work for me, but 777 is not good for security

    sudo chmod -R 777 /path/to/your/repo
1
  • sudo chmod -R 777 /path/to/your/repo this worked :)
    – Rakesh K
    Nov 18, 2020 at 22:04
3

I use gitosis for managing this kind of stuff. Gitosis has a single user (usually called "git") that owns all the repositories, and it uses public-key-based access control to each repo. It might not suit your setup but is probably worth checking out (no pun intended).

3
  • 1
    There's also gitolite (github.com/sitaramc/gitolite), which is kind of an updated and improved version of gitosis.
    – ebneter
    Oct 26, 2010 at 19:23
  • Thanks guys. But do i need to rebuild my repo from scratch using gitosis/gitolite? Oct 27, 2010 at 8:41
  • 1
    No. Just push your existing head into the gitosis repo, or copy your repo directory into the one created by gitosis. Oct 27, 2010 at 9:46
3

I was getting similar error and please see below how I resolved it.

My directory structure: /opt/git/project.git and git user is git

$ cd /opt/git/project.git
$ sudo chown -R git:git .

chown with -R option recursively changes the ownership and and group (since i typed git:git in above command) of the current directory. chown -R is necessary since git changes many files inside your git directory when you push to the repository.

2

This problem can also occur after Ubuntu upgrades that require a reboot.

If the file /var/run/reboot-required exists, do or schedule a restart.

1

I was having trouble with this too, thinking my remote gitolite-admin was corrupted or something wrong.

My setup is Mac OS X (10.6.6) laptop with remote Ubuntu 10 server with gitolite.

It turned out that the problem was with my local checkout of gitolite-admin.

Despite the "unpack failed" error, it turned out the the problem was local.

I figured this out by checking it out again as gitolite-admin2, making a change, and the pushing.

Voila! It worked!

1
  • 1
    For me the problem was also in the local repository (probably because I used an older version of git on a newer version's .git structures). git push didn't work, but git clone did, so I cloned my local repository and then transplanted the new .git into the local repository. Thanks for the hint! Jan 18, 2013 at 12:22
1

For what it worth, I had the same problem over my own VPS and it was caused by my low hard disk space on VPS. Confirmed by df -h command and after i cleaned up my VPS' hard disk; the problem was gone.

Cheers.

0
0

Where I work we have been using this method on all of our repositories for a few years without any problems (except when we create a new repository and forget to set it up this way):

  1. Set 'sharedRepository = true' in the config file's '[core]' section.
  2. Change the group id of the repository to a group shared by all users who are allowed to push to it:

    chgrp -R shared_group /git/our_repos
    chmod -R g+w /git/our_repos
    
  3. Set the setgid bit on all directories in the repository so that new files/directories keep the same group:

    find /git/our_repos -type d -exec chmod g+s {} +
    
  4. Add this line to the pre-receive hook in the repository to ensure new file permissions allow group read/write:

    umask 007
    
0

For me its a permissions issue:

On the git server run this command on the repo directory

sudo chmod -R 777 theDirectory/
1
  • Never use 777. Instead, create a user with the command adduser [username], then make this user an owner of the directory with chown -R [user]:[group] [directory], then set the directory so that it can be written to by the owner, so chmod -R 775 or something like that. The first digit governs user permissions, the second governs group permissions, and the third governs the permissions of everyone else.
    – kloddant
    Jun 6, 2018 at 13:28
0

A git configuration mistake can also product this error. I give my students and example of configuration like this:

git config --global user.name "John Doe"
git config --global user.email [email protected]

One of my students was getting the unpacker error. Other students were fine, but I still did a double check on the git server's permissions and to be sure the student was in proper group.

Finally, I had the student do a git log and saw that he had John Doe for his configuration, but his branch was his own name.

Setting up his configuration properly eliminated the error .

0

I had similar problem like this before:

 ! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to 'https://mywebsite.com/my-git-directory.git'

In my case, I have checked wrong directory ownership with ls -l. I change the directory owner to www-data to solve the problem like this:

sudo chown -R www-data:www-data my-git-directory.git/

But in this case I not use SSH method, I use HTTP method.

Maybe when we make sure the directory owner is correct, it can solve the problem.

0

The repack error might happen if you run out of memory when compressing large files.

Would you please try executing the below commands to limit the pack memory and threads, and try again?

git config --global pack.windowMemory "100m"
git config --global pack.packSizeLimit "100m"
git config --global pack.threads "1"
git fsck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.