常见的代码托管平台,国外的有github,国内的有码云、coding.net等。这里介绍码云代码托盘平台使用(其它平台方法类似)。


一、注册码云帐号

https://gitee.com/signup


二、创建项目



三、客户端创建ssh key

ssh key可以让客户端与码云服务器安全加密连接,而且不需要输入密码。

1、客户端生成公钥和私钥。

[root@localhost ~]# ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
64:78:e9:5d:72:d0:d5:0c:51:f9:dc:25:ff:b5:5b:d9 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|          .. .+*o|
|       . . .. ..+|
|      . = . o  ++|
|       = . +    *|
|        S .     *|
|               oE|
|                o|
|               . |
|                 |
+-----------------+


2、查看生成的公钥。

[root@localhost ~]# cat ~/.ssh/id_rsa.pub


3、将公钥复制到码云这里。


4、测试是否可以连接到码云服务器。

[root@localhost ~]# ssh -T [email protected]
The authenticity of host 'git.oschina.net (116.211.167.14)' can't be established.
RSA key fingerprint is e3:ee:82:78:fb:c0:ca:24:65:69:ba:bc:47:24:6f:d4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'git.oschina.net,116.211.167.14' (RSA) to the list of known hosts.
Welcome to Gitee.com, 赛里! -----看到这句表示成功



四、客户端(本地)初始化一个项目

1、首先设置你的姓名和邮箱地址,提交代码的时候会记录这些信息。

[root@localhost ~]# git config --global user.name "gxm"
[root@localhost ~]# git config --global user.email "[email protected]"


2、创建目录并初始化成版本库

[root@localhost ~]# mkdir gxmscript
[root@localhost ~]# cd gxmscript
[root@localhost gxmscript]# git init
Initialized empty Git repository in /root/gxmscript/.git/


3、运行如下命令(支持https和ssh方式)。

[root@localhost gxmscript]# git remote add origin [email protected]:null_803_3682/shellscripts.git


备注1:如果输入错了,可以用如下命令删除,然后重新运行上面的命令(没输错不要看以下灰色的几行)。

[root@localhost gxmscripts]# git remote -v
[root@localhost gxmscripts]# git remote rm origin
[root@localhost gxmscripts]# git remote -v
[root@localhost gxmscript]# git remote add origin [email protected]:null_803_3682/shellscripts.git

备注2:如果要克隆项目运行git clone 项目地址


4、进入已经初始化或者克隆项目的目录

因为码云服务器上有README.md这个文件,而本地没有,所以提交的时候可能会冲突。这个时候需要选择是保留码云服务器上这个文件,还是舍弃?如果舍弃用这个命令强制推送(git push origin master -f)。而如果需要保留先执行git pull origin master从码云服务器拉过来(或者用git clone克隆下来)。我这里选择保留的方法。

[root@localhost gxmscript]# git pull origin master
The authenticity of host 'gitee.com (116.211.167.14)' can't be established.
RSA key fingerprint is e3:ee:82:78:fb:c0:ca:24:65:69:ba:bc:47:24:6f:d4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com' (RSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From gitee.com:null_803_3682/service_montir
 * branch            master     -> FETCH_HEAD
[root@localhost gxmscript]# ll
总用量 4
-rw-r--r-- 1 root root 81 5月  22 03:26 README.md


5、提交一个程序(脚本)。

[root@localhost gxmscript]# vi service_montir.sh
[root@localhost gxmscript]# git add service_montir.sh 
[root@localhost gxmscript]# git commit -m "提交服务监控脚本"
[master 95ce665] 提交服务监控脚本
 1 files changed, 112 insertions(+), 0 deletions(-)
 create mode 100644 service_montir.sh
 
[root@localhost gxmscript]# git status
# On branch master
nothing to commit (working directory clean)

[root@localhost gxmscript]# git log
commit 95ce665342fff8a14d50293877c635e35700ed92
Author: gxm 
Date:   Sun May 22 03:30:00 2016 +0800
    提交服务监控脚本
commit e819f6818e1bd10f730278028603df81183ca30c
Author: 赛里 <[email protected]>
Date:   Sat Feb 3 10:48:42 2018 +0800
    Initial commit
    
[root@localhost gxmscript]# git push origin master
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.28 KiB, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:null_803_3682/service_montir.git
   e819f68..95ce665  master -> master


6、登录到码云服务器进程验证,提交成功。