将现有项目添加到git

当我们使用git的时候 也许项目已经使用很久其他的版本控制比如svn

网上现有的很多教程创建git服务器都是从裸仓库开始的

今天学会了怎么用git给当前已有的项目添加到远程仓库

//进入到项目内
cd /home/www/myapp
//执行初始化
git init
//切换到项目外执行
cd ..
git clone --bare myapp myapp.git
//此出会在www目录下创建 myapp.git目录
//将myapp.git 目录移动到你的git仓库的目录
remove /home/www/myapp.git /home/git/repositories/myapp.git
//在你的项目目录添加远程服务器
cd /home/www/myapp
git remote add origin /home/git/repositories/myapp.git 

//修改一些文件内容 尝试提交然后push到仓库
vim test.php
git add .
git commit -m 'add test.php'
git push
//提交成功 暂时没有遇到其他问题
//然后你就可以在你本地clone你的项目了 以下命令是在本机git shell执行
git clone [email protected]:/home/git/repositories/myapp.git

//添加钩子文件 本地push代码的时候会将修改的内容提交到/home/git/repositories/myapp.git
//钩子文件会自动跳到/home/www/myapp进行pull代码 再次访问你的网站代码就已经被更新了
//首先添加钩子文件
cd /home/git/repositories/myapp.git/hooks
//创建钩子文件
vim post-receive
//钩子文件内容
#!/bin/sh

unset GIT_DIR

NowPath=`pwd`
DeployPath="/home/www/myapp"

cd $DeployPath
git pull origin master

cd $NowPath
exit 0
//然后给钩子文件添加执行权限
chmod a+x post-receive
//修改仓库与项目的所属组与所属用户为git 不然钩子文件是以git用户执行的,进入项目后pull会没有文件修改权限
chown -R git:git /home/git/repositories/myapp.git
chown -R git:git /home/www/myapp

//修改地址文件 然后提交 再次查看服务器的项目目录 就会发现文件是你刚刚提交的了

猜你喜欢

转载自blog.csdn.net/hoooooly/article/details/81429378