How to Create and Use a Bare Git Repository on a Linux Server

Setting up a bare Git repository on a Linux server is a foundational skill for teams that want to collaborate using a centralized remote repository. A bare repository is essentially a Git repository without a working directory, designed to be a central point for collaboration rather than for editing code directly. In this article, you’ll learn how to set up a bare repository, clone it, and push code changes from your local machine.

Why Use a Bare Repository?

A bare repository contains only the version control information (the .git directory) and does not have a checked-out copy of the project files. It’s intended as a central hub for developers to push and pull changes—ideal for collaborative workflows.

Step 1: Create a Bare Repository on Your Server

Log into your Linux server via SSH:

ssh username@your-server.com

Navigate to or create the directory where you want your repositories:

mkdir -p ~/git/repos
cd ~/git/repos

Now, create a new bare repository:

git init --bare myproject.git

This will create a folder myproject.git containing all the Git version history and configuration. The --bare option ensures this repository does not have a working directory and is optimized for sharing.

Step 2: Clone the Repository Locally

On your local machine, clone the repository using SSH:

git clone username@your-server.com:~/git/repos/myproject.git

You may need to adjust the path or set up SSH keys if you haven’t already.

Step 3: Push Commits to the Bare Repository

Once you have cloned the repository, you can make changes locally. To share your changes:

  1. Add and commit your changes:
    git add .
    git commit -m "Describe your changes"
    
  2. Push changes to the server:
    git push origin main
    

    (Use main or master depending on your branch name.)

Step 4: Collaborate

Other developers can also clone the remote repository in the same way. Everyone can push and pull code as needed, and the bare repository keeps everything in sync.

Final Tips

  • Make sure the server user has proper permissions where the repo resides.
  • For additional security, consider restricting user shell access or using shared Git accounts.
  • Use a descriptive name for your repo and consider organizing multiple projects in sub-directories.

By following these steps, you’ve set up a simple, secure, and effective Git workflow for your projects hosted on a Linux server.

Comments

One response to “How to Create and Use a Bare Git Repository on a Linux Server”

  1. Maddie Avatar
    Maddie

    Comment from Maddie:

    Great article! 👏 Setting up a bare Git repository is such a core skill for collaborative development, especially for teams that want a self-hosted, lightweight alternative to platforms like GitHub or GitLab. I appreciate how clearly you broke down each step—from initializing the repo on the server to collaborating with teammates.

    One tip for anyone integrating this setup with app development workflows (like Angular or other frontend stacks): you can easily use bare repositories alongside CI/CD pipelines or deployment scripts for a streamlined process. Also, organizing your bare repositories in subdirectories (as mentioned) is a huge help when you’re maintaining multiple projects or microservices.

    If you ever want to take this further, you could show how to automate repo creation, set up Git hooks, or even integrate with tools like Gitea for a minimal self-hosted UI.

    Thanks for making Git basics so approachable for everyone!
    — Maddie

Leave a Reply to Maddie Cancel reply

Your email address will not be published. Required fields are marked *