How to make a git repository read-only?

Viewed 74399

I have some git repositories accessed remotely through SSH and I want to make some of them read-only to prevent more pushes. Some people have remotes pointing to these repositories.

These bare repositories were initialised --shared=group, so is setting file permissions to 660 for all files good enough to still allow SSH access, but disallow writes? Or is there an easier way?

Cheers.

10 Answers

A pre-receive hook that simply prints an informative message and exits with a non zero status does the job.

Assuming you put some meaningful information in your message, it also cuts down on the queries from frustrated users asking why they can't push:

#!/bin/bash
echo "=================================================="
echo "This repository is no longer available for pushes."
echo "Please visit blah blah yadda yadda ...."
echo "=================================================="
exit 1

Remember to set the executable permission for the script and to make sure is owned by the right user and/or group, or else it will not execute and will not give any warning.

Inspired by this comment:

  1. Update your hooks/pre-receive file with the following content:

    #!/bin/sh
    
    echo "Closed for all pushes" ; exit 1
    

This way, all users trying to push changes to this repo will receive the message above and the push will be rejected.

As I am just a user of our GitLab (and I didn't wanted to bother the admins in the first step), I searched for another way and found one:

  • Open the GitLab webinterface and go to the repository you want to set to read-only
  • Choose Settings > Repository
  • Expand Protected Branches
  • Add the master branch and set Allowed to merge and Allowed to push to No one
  • If the master branch is protected already, you can set these values in the list below
Related