整个操作流程可以理解为:先在本地创建仓库,然后在仓库中添加文件(A、B、C)和文件夹(config.xml),然后在GitHub上建立远程库并关联,把本地仓库上的文件打包并推送到GitHub,然后克隆下来,并部署在/app/www中。即可以理解为开发提交代码到远程仓库GitHub中,然后测试clone代码的过程。
准备工作:1、以root用户登陆自己的Linux服务器;2、git已安装并配置好。
Git安装配置见:http://www.jianshu.com/writer#/notebooks/12459620/notes/13565149
一、本地创建git仓库并在仓库下新建文件及文件夹
1、新建git仓库并初始化
[root@iZwz945po7bqabsr6de22fZ ~]mkdir -p /home/git/repositories/test.git
[root@iZwz945po7bqabsr6de22fZ ~]# cd /home/git/repositories/test.git
[root@iZwz945po7bqabsr6de22fZ test.git]# git init
Initialized empty Git repository in /home/git/repositories/test.git/
2、仓库下创建文件夹及xml文件
[root@iZwz945po7bqabsr6de22fZ test.git]# mkdir A
[root@iZwz945po7bqabsr6de22fZ test.git]# mkdir B
[root@iZwz945po7bqabsr6de22fZ test.git]# mkdir C
[root@iZwz945po7bqabsr6de22fZ test.git]# vi config.xml
3、编辑config.xml文件
[root@iZwz945po7bqabsr6de22fZ test.git]# vim config.xml
查看编辑后的内容:
[root@iZwz945po7bqabsr6de22fZ test.git]# cat config.xml
hello world
~
update finish!
4、打包文件及文件夹
[root@iZwz945po7bqabsr6de22fZ test.git]# tar -zcvf test.tar.gz A B C config.xml
二、建立远程仓库并关联
1、打开Github,注册账号后,在右上角找到New repository,建立一个新的仓库,如下图:

2、点击步骤1中的“Create repository”后,并按照github网上的提示输入

3、推送test.tar.gz压缩包到github
[root@iZwz945po7bqabsr6de22fZ test.git]# git add test.tar.gz
[root@iZwz945po7bqabsr6de22fZ test.git]# git commit -m "first commit"
[root@iZwz945po7bqabsr6de22fZ test.git]# git remote add origin git@github.com:jrainyang/istest.git
[root@iZwz945po7bqabsr6de22fZ test.git]# git push -u origin master
在推送时报错了,如下:
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
这个应该是很多github新手经常出错的问题,这个就是没有在你的github上添加一个公钥。
解决方法如下:
1)可以用 ssh -T git@github.com去测试一下

图上可以明显看出缺少了公钥
2)生产公匙,如下图

3)cat 一下 把出现的key 复制下来
[root@iZwz945po7bqabsr6de22fZ ~]# cat /root/.ssh/id_rsa.pub

4)在github上添加刚刚生成的公钥




4、再次推送test.tar.gz压缩包到github
[root@iZwz945po7bqabsr6de22fZ test.git]# git add test.tar.gz
[root@iZwz945po7bqabsr6de22fZ test.git]# git commit -m "first commit"
[root@iZwz945po7bqabsr6de22fZ test.git]# git remote add origin git@github.com:jrainyang/istest.git
[root@iZwz945po7bqabsr6de22fZ test.git]# git push -u origin master

5、在GitHub页面可看到本地库一样的内容

6、克隆远程库到app/www,并部署
[root@iZwz945po7bqabsr6de22fZ app]# mkdir www
[root@iZwz945po7bqabsr6de22fZ www]# git clone git@github.com:jrainyang/istest.git
[root@iZwz945po7bqabsr6de22fZ www]# tar -zxvf test.tar.gz

网友评论