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:
- Add and commit your changes:
git add . git commit -m "Describe your changes"
- Push changes to the server:
git push origin main
(Use
main
ormaster
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.
Leave a Reply to Maddie Cancel reply