Set Up Git Server and Sync to a Specific Directory

📢 This article was translated by gemini-2.5-flash

Introduction

Git makes managing code with friends way easier, and it’s super handy for syncing code too. Since I’m into static sites, using Git to sync web files is really practical.

Install Git

Log in as ROOT, then punch in these commands:

1
2
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
yum install git

Create User

Let’s create a Git group and a user to run the Git service.

1
2
groupadd git
useradd git -g git

Get Your Public Key

This part is about getting your public key from your own machine. We’ll use Windows as an example.

Your id_rsa.pub file is stored in %UserProfile%/.ssh/.

If it’s not there, just open cmd and run ssh-keygen; it’ll generate one for you.

Import Public Key

Import the public key (or keys) into /home/git/.ssh/authorized_keys, one per line. If the file doesn’t exist, create it:

1
2
3
4
5
cd /home/git/
mkdir .ssh
chmod 755 .ssh
touch .ssh/authorized_keys
chmod 644 .ssh/authorized_keys

Then use vi or vim to import the public key. For vim usage, check out: Vim Text Editing

Initialize Git Repository

Pick a directory for your Git repo. Let’s say /home/gitrepo/repo.git.

1
2
3
4
5
cd /home
mkdir gitrepo
chown git:git gitrepo
cd gitrepo
git init --bare repo.git

The commands above create a bare Git repository. Server-side Git repos usually end with .git. Next, change the repo’s owner to the git user:

1
chown -R git:git runoob.git

Clone Repository

1
2
3
git clone git@YourIP:RepoDirectory
// For example
git clone [email protected]:/home/gitrepo/repo.git

Sync Directory

The auto-sync feature uses Git hooks.

Navigate to your repo directory: /home/gitrepo/repo.git

1
2
cd /home/gitrepo/repo.git
cd hooks

Create and edit the post-receive file:

1
vi post-receive

Add the following content to the file:

1
2
#!/bin/sh
git --work-tree=SyncTargetDirectory --git-dir=RepoLocation checkout -f

For example:

1
2
#!/bin/sh
git --work-tree=/home/www/hexo --git-dir=/home/gitrepo/repo.git checkout -f

Then set read/write permissions for the file:

1
chmod +x post-receive

Change the owner of the repo.git directory to the git user.

1
chown -R git:git /home/gitrepo/repo.git

Update Code

If you’re sure nothing’s changed and you just need to update local code, simply use:

1
git pull

The proper workflow:

1
2
3
4
5
6
7
8
9
// Check local branch file status to avoid conflicts during update.
git status
// If files are modified, you can revert them to the original state; if files need to be updated to the server, you should merge to the server first, then update locally.
git checkout – [file name]
// Check current branch status.
git branch
// If it's a local branch, you need to switch to the remote branch on the server.
git checkout remote branch
git pull

Other Commands

1
2
3
4
git branch // Check branches
git checkout aaa // Switch to branch 'aaa'
git branch aaa // Create branch 'aaa'
git checkout -b aaa // Create branch 'aaa' locally and switch to it. The branch will only be created on the server when you push.

Disable Shell Login for Git User

For security, we need to prevent the git user from logging in via shell. You can do this by editing /etc/passwd:

1
vi /etc/passwd

Change this:

1
git:x:1004:1004::/home/git:/bin/bash

To this:

1
git:x:1004:1004::/home/git:/usr/bin/git-shell

This way, the git user can still use Git normally via SSH, but can’t log into the shell.

References

Linux chown Command - Runoob Tutorial

Git Server Setup - Runoob Tutorial

Git - Generating Your SSH Public Key

Introduction to SSH Keys and Their Use in Git - Jianshu

Git - Getting Git on a Server

Git: Updating Local Code - ftToday’s Blog - CSDN

How to Set Up a Hexo Blog on a Server - Alibaba Cloud Developer Community

Linux Permissions Explained (chmod, 600, 644, 700, 711, 755, 777, 4755, 6755, 7755) - Lin20’s Blog - CSDN

Detailed Analysis of Linux /etc/passwd file - Jtianlin - Blog Garden

This post is licensed under CC BY-NC-SA 4.0 by the author.