因为项目需求,自己就尝试在阿里云服务器上创建了项目的代码仓库,创建过程中遇到了一些问题,经过一番折腾,终于把这个代码仓库在远程服务器上创建成功了,可以正常进行代码版本维护了.以下是自己在创建过程中的一些心得和总结.(建议先在远程服务器上添加个git用户组,并在git用户组里添加个git用户,后面的操作都是使用git这个用户身份操作的)
1)根据ssh协议远程登录服务器,并切换到git用户目录下,在该位置建立个项目的空仓库.相关命令如下:
ssh root@服务器地址
cd /home/git
git init --bare xiangmu.git
2)进入刚创建的xiangmu.git文件夹里的hooks文件夹中,里面有post-receive和post-update这两个文件(如果没有的话需要自己新建)
cd hooks
ls -al
如下是hooks目录下的文件:
drwxr-xr-x 2 git git 4096 10月 25 11:16 .
drwxr-xr-x 7 git git 4096 10月 25 10:01 ..
-rwxr-xr-x 1 git git 452 6月 28 09:52 applypatch-msg.sample
-rwxr-xr-x 1 git git 896 6月 28 09:52 commit-msg.sample
-rwxr-xr-x 1 git git 589 10月 25 11:11 post-receive
-rwxr-xr-x 1 git git 231 10月 25 11:16 post-update
-rwxr-xr-x 1 git git 189 6月 28 09:52 post-update.sample
-rwxr-xr-x 1 git git 398 6月 28 09:52 pre-applypatch.sample
-rwxr-xr-x 1 git git 1704 6月 28 09:52 pre-commit.sample
-rwxr-xr-x 1 git git 1239 6月 28 09:52 prepare-commit-msg.sample
-rw-r--r-- 1 git git 1348 6月 28 09:52 pre-push.sample
-rwxr-xr-x 1 git git 4951 6月 28 09:52 pre-rebase.sample
-rwxr-xr-x 1 git git 3611 6月 28 09:52 update.sample
下面是配置好的post-receive里的文件内容:
#!/bin/sh
#
# An example hook script for the "post-receive" event.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".
#. /usr/share/git-core/contrib/hooks/post-receive-email
cd /wwwroot/www/项目文件夹
env -i git pull
下面是配置好的post-update里的文件内容:
#!/bin/sh
#
# An example hook script for the "post-receive" event.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".
#. /usr/share/git-core/contrib/hooks/post-receive-email
cd /wwwroot/www/项目文件夹
env -i git pull
3)在远程服务器上切换到服务器的web目录下,自己这个服务器的web目录是/wwwroot/www/,故切换到这个位置,然后新建个文件夹,这里的文件夹名要和hooks里提到的上述两个文件中的项目文件夹名保持一样.
4)在/wwwroot/www/项目文件夹下将git目录下刚创建的空仓库的内容clone过来,命令如下:
git clone /home/git/xiangmu.git
5)在本地clone远程服务器上的空仓库,命令如下:
git clone 远程仓库地址
6)修改本地仓库中的.gitignore文件内容(git不用push,pull的那一部分文件),参考如下:
Runtime/
*.swp
*.swo
Public/Uploads
gitAuto.sh
Application/Runtime
7)在本地仓库添加代码等文件,并提交到远程仓库,命令如下:
git add .
git commit -m 's'
git push
这个时候git目录下的代码会得到更新,同时由于钩子的作用,web目录下的代码也得到了同步更新.如果web目录下的代码没有更新,可能是版本产生冲突了怎么的,可以在web目录下的项目文件夹下git pull一下,这样代码就可以正常维护了.
本文详细介绍如何在阿里云服务器上创建代码仓库,并通过SSH协议完成远程登录及代码版本维护。涉及步骤包括创建空仓库、配置post-receive及post-update钩子脚本,确保web目录与git仓库同步。
631

被折叠的 条评论
为什么被折叠?



