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:
| |
Create User
Let’s create a Git group and a user to run the Git service.
| |
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:
| |
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.
| |
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:
| |
Clone Repository
| |
Sync Directory
The auto-sync feature uses Git hooks.
Navigate to your repo directory: /home/gitrepo/repo.git
| |
Create and edit the post-receive file:
| |
Add the following content to the file:
| |
For example:
| |
Then set read/write permissions for the file:
| |
Change the owner of the repo.git directory to the git user.
| |
Update Code
If you’re sure nothing’s changed and you just need to update local code, simply use:
| |
The proper workflow:
| |
Other Commands
| |
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:
| |
Change this:
| |
To this:
| |
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: Updating Local Code - ftToday’s Blog - CSDN
How to Set Up a Hexo Blog on a Server - Alibaba Cloud Developer Community
Detailed Analysis of Linux /etc/passwd file - Jtianlin - Blog Garden