Linux system to build Git server and synchronize project files

There are a few steps the following:

1.Linux install git in service;

2. Create a git user

3. Add ssh authentication mode, the local PC public key into the user's .ssh git (for later use project synchronization server account also needs to correspond feel ashamed, e.g. git user itself, the user switch to git, ssh-keygen - t rsa generating public and private key, public key /home/git/.ssh/id_rsa.pub also placed /home/git/.ssh/authorized_keys)

4. git init -bare initialize a bare repository (with distinction init git, git can not bare warehouse operation work, can git init), and local clone this repository, add new files to upload

mkdir /home/testgit
cd /home/testgit
git init --bare t1.git
chown -R git:git /home/testgit

5. Create a project file synchronization feature

cd /home/testgit/t1.git
cd hooks
//创建post-receive文件
vim post-receive

将一下代码添加进去
#!/bin/sh

unset GIT_DIR
NowPath=`pwd`
echo "now path is :"$NowPath
GitPath="/www/wwwroot/t1"
echo "git path is :"$GitPath
cd $GitPath
echo "cd git path"
git add . -A && git stash # remove local changes
git pull origin master # pull data from master
# the follow line is also ok:
# git add . && git fetch origin && git reset --hard origin/master

echo "git done"
cd $NowPath
echo "fine"
# --- Finished
exit 0

6. go / www / wwwroot file folder, the user is switched to git (for the first time there is a need to confirm the operation authority time)

git clone git@localhost:/home/testgit/t1.git

7. At this point the local computer operation will automatically sync to the project, if the problem, pay attention to see linux permissions for each folder and file.

Released six original articles · won praise 4 · Views 2363

Guess you like

Origin blog.csdn.net/dahai_PHP/article/details/103948067