Bare git repos Set up your own Git server using bare repositories for seamless development workflow
So you want to have your own Git server, huh? Maybe you’re tired of pushing to GitHub for every little change, or you want to keep some code private and local. Well, bare repositories are exactly what you need for this. They’re like a mini-GitHub that lives right on your server.
A bare repository is essentially a Git repository without a working tree
– it only contains the .git
directory contents. This makes it perfect
for acting as a central repository that other repositories can push to
and pull from. Think of it as the “server” part of Git, but running on
your server filesystem.
Let’s start with the basics. You’ll want to create a directory structure
for your bare repositories on your server. I usually keep them in
~/git-repos
or some similar location:
mkdir -p ~/git-repos
cd ~/git-repos
Now, to create a bare repository, you use the --bare
flag:
git init --bare my-project.git
Notice the .git
suffix – this is a convention that makes it clear
this is a bare repository. The directory structure will look like this:
my-project.git/
├── HEAD
├── config
├── description
├── hooks/
├── info/
├── objects/
└── refs/
Now you can clone from this bare repository just like you would from GitHub. Let’s say you want to work on your project from your local machine:
cd ~/projects
git clone user@your-server:~/git-repos/my-project.git
cd my-project
Now you can work normally in this cloned repository. When you’re ready to push your changes:
git add .
git commit -m "Initial commit"
git push origin main
The beauty is that origin
now points to your server’s bare repository,
so pushes go directly to your server!
Happy hacking!