Installation of git under centos and automatic code synchronization

Posted by imranlink on Sat, 25 May 2019 20:28:30 +0200

Reference articles

CentOS Installation Git for Multi-Person Synchronized Development
GIT Server Construction and Key Connection in centos

Sketch

1. Installing Git Dependency and Git on Server

2. Create Git users and their groups

3. Initialization of Git Warehouse on Server

4. Install Git Client and Generate Public Key

5. Create certificate login

6. Cloning an empty warehouse on a Git Bash server

7. Push the local library project to the server

1. Installing Git on the server and dependencies

1.1 Installation of Git Dependencies

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel

1.2. Install Git

yum install -y git

2. Create User Groups and Users to run git services

2.1 Create User Groups

groupadd git

2.2 Adding users under git user group

adduser phper -g git

2.3 Set Password for User Name phper

   passwd phper
   Changing password for user git.
   New password: 
   Retype new password: 
   passwd: all authentication tokens updated successfully.

3. Establishment of git warehouse

mkdir gitroot
chmown phper:git gitroot
cd /gitroot
git init --bare project.git
chmown -R phper:git project.git
chmod 774 -R project.git
cd ../
chmod 750 gitroot

4. Install Git Client and Generate Public Key

4.1 Right-click on git GUI Here - > Help - > Show SSH Key when the git client is installed

You can get the private key and the public key.


Open Puttygen

Private keys generated before load
Get the formatted private key and click Save the private key

Configuration of Little Tortoise
4.2 Create Certificate Logon
Switch to phper directory

cd /home/phper
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys

Then upload the client's public key to the. ssh directory

cd .ssh
rz

Add the public key to authorized_keys

cat id_rsa.pub >> authorized_keys

5. Cloning locally
Right-click to select git clone
In this way, the empty warehouse of the server is pulled down.

Now we can submit code to the server's git normally, but it can't be synchronized automatically. We also need to modify the server's hooks/post-receive file. Specific post-receive content

#!/bin/sh
unset GIT_DIR  
DeployPath="/alidata/www/project"
LogPath="/alidata/gitroot/project.git/hooks"  

echo -e "\n=================  `date +"%Y-%m-%d %H:%M:%S"`  ===============\n" >> $LogPath/gitsync.log 2>&1 
cd $DeployPath  

#git stash  
#Pull and take before merge
git pull origin master  >> $LogPath/gitsync.log 2>&1 

#Mandatory synchronization with remote servers, not merged locally, can only modify the code by submitting client submissions.
#git fetch --all  
#git reset --hard origin/master  

#time=`date`  
#echo "web server pull at webserver at time: $time."  
echo "================================================"  >> $LogPath/gitsync.log 2>&1 

Change the owner and permissions of post-receive

chmod -R 774 post-receive
chown phper:git post-receive

Finally, under www

mkdir project
chown -R phper:git project
chmod -R 774 project
cd /alidata/www
git clone /gitroot/project.git

The synchronization of git is done.

If synchronization is not available, open hooks/gitsync.log to view the error log

Possible mistake

1,fatal: /usr/libexec/git-core/git-pull cannot be used without a working tree.
Synchronized project Folder Not Established --- Solution: Establish project Project Folder under www

2,fatal: Not a git repository (or any of the parent directories): .git
There is no git initialization in the project file ----- solution: execute git clone /alidata/gitroot/project.git in the www path

3,error: cannot open .git/FETCH_HEAD: Permission denied
Git does not write permissions in the project directory - Solution: Modify the owner and permission chown-R phper: git project/chmod-R 774 project

4. Enter your password every time you pull and pushimage.png
The secret key doesn't work ------------- solution: / var / log / security check the log, is it. ssh's permission problem?
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

Topics: git ssh CentOS yum