sudo git init --bare wp-content.git // 成功创建 返回如下 Initialized empty Git repository in /www/dongshushu/git/wp-content.git/
mkdir wp-content.git cd wp-content.git git init --bare // 成功创建 返回如下 Initialized empty Git repository in /www/dongshushu/git/wp-content.git/
查看一下wp-content.git都有那些文件
drwxr-xr-x 2 root root 4096 Aug 1 15:35 branches -rw-r--r-- 1 root root 66 Aug 1 15:35 config -rw-r--r-- 1 root root 73 Aug 1 15:35 description -rw-r--r-- 1 root root 23 Aug 1 15:35 HEAD drwxr-xr-x 2 root root 4096 Aug 1 15:35 hooks drwxr-xr-x 2 root root 4096 Aug 1 15:35 info drwxr-xr-x 4 root root 4096 Aug 1 15:35 objects drwxr-xr-x 4 root root 4096 Aug 1 15:35 refs
新建用户和用户组
因为这个git服务器是整个团队维护,所以就不使用root用户了,改用git用户
chown -R git:git wp-content.git
查看wp-content.git的用户和用户组
ll wp-content.git
wp-content.git下文件用户和用户组变为git
drwxr-xr-x 2 git git 4096 Aug 1 15:49 branches -rw-r--r-- 1 git git 66 Aug 1 15:49 config -rw-r--r-- 1 git git 73 Aug 1 15:49 description -rw-r--r-- 1 git git 23 Aug 1 15:49 HEAD drwxr-xr-x 2 git git 4096 Aug 1 15:49 hooks drwxr-xr-x 2 git git 4096 Aug 1 15:49 info drwxr-xr-x 4 git git 4096 Aug 1 15:49 objects drwxr-xr-x 4 git git 4096 Aug 1 15:49 refs
//进入桌面 cd ~/desktop git clone git@dongshushu.com:/www/dongshushu/git/wp-content.git //返回如下 Cloning into 'wp-content'... //输入git用户的密码 git@dongshushu.com's password: warning: You appear to have cloned an empty repository. Checking connectivity... done.
温馨提示:如果忘记忘记用户git的密码,在服务器使用passwd git创建新密码
发下桌面有一个wp-content的空文件夹,进入文件夹,创建点儿东西,然后push到服务器
//进入文件夹 cd wp-content //创建hello.txt文件 touch hello.txt //使用git增加注释并push到服务器 git add hello.txt git commit -m 'create hello.txt' git push
第一次提交可能会报错如下
git@dongshushu.com's password: No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master'. fatal: The remote end hung up unexpectedly error: failed to push some refs to 'git@dongshushu.com:/www/dongshushu/git/wp-content.git'
用如下代码提交
git push origin master
进入相关路径,因为是在同一个服务器所以使用相对路径或绝对路径就可以直接clone
clone /www/dongshushu/git/wp-content.git
不出意外,我们会看到wp-content文件夹,并在其中找到hello.txt文件。以后再wp-content文件夹下执行git pull就可以更新了
如果每次本地push,都在服务器上去pull,那会累死人的,还好git提供了hook(钩子)
cd wp-content.git/hooks ll //返回,以下都是官方提供的示例,用的话需要去掉后缀.sample -rwxr-xr-x 1 git git 478 Aug 1 15:49 applypatch-msg.sample -rwxr-xr-x 1 git git 896 Aug 1 15:49 commit-msg.sample -rwxr-xr-x 1 git git 189 Aug 1 15:49 post-update.sample -rwxr-xr-x 1 git git 424 Aug 1 15:49 pre-applypatch.sample -rwxr-xr-x 1 git git 1642 Aug 1 15:49 pre-commit.sample -rwxr-xr-x 1 git git 1239 Aug 1 15:49 prepare-commit-msg.sample -rwxr-xr-x 1 git git 1348 Aug 1 15:49 pre-push.sample -rwxr-xr-x 1 git git 4951 Aug 1 15:49 pre-rebase.sample -rwxr-xr-x 1 git git 3610 Aug 1 15:49 update.sample
新建post-receive
vim post-receive
#!/bin/sh DEPLOY_PATH=/www/dongshushu/wp-content unset GIT_DIR #这条命令很重要 cd $DEPLOY_PATH git reset --hard git pull #chown www:www -R $DEPLOY_PATH
注意此文件的用户组,还有权限,并修改
chown -R git:git post-receive
chmod a+x post-receive
还有得改一下/www/dongshushu/git/wp-content这个文件夹的用户组
chown -R git:git wp-content
01 Aug 2017