常见的代码托管平台,国外的有github,国内的有码云、coding.net等。这里介绍码云代码托盘平台使用(其它平台方法类似)。
一、注册码云帐号
<a href="https://gitee.com/signup">https://gitee.com/signup</a>
二、创建项目
三、客户端创建ssh key
ssh key可以让客户端与码云服务器安全加密连接,而且不需要输入密码。
1、客户端生成公钥和私钥。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<code>[root@localhost ~]</code><code># ssh-keygen -t rsa -C "[email protected]"</code>
<code>Generating public</code><code>/private</code> <code>rsa key pair.</code>
<code>Enter </code><code>file</code> <code>in</code> <code>which</code> <code>to save the key (</code><code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>): </code>
<code>Created directory </code><code>'/root/.ssh'</code><code>.</code>
<code>Enter passphrase (empty </code><code>for</code> <code>no passphrase): </code>
<code>Enter same passphrase again: </code>
<code>Your identification has been saved </code><code>in</code> <code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.</code>
<code>Your public key has been saved </code><code>in</code> <code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.pub.</code>
<code>The key fingerprint is:</code>
<code>64:78:e9:5d:72:d0:d5:0c:51:f9:</code><code>dc</code><code>:25:ff:b5:5b:d9 [email protected]</code>
<code>The key's randomart image is:</code>
<code>+--[ RSA 2048]----+</code>
<code>| .. .+*o|</code>
<code>| . . .. ..+|</code>
<code>| . = . o ++|</code>
<code>| = . + *|</code>
<code>| S . *|</code>
<code>| oE|</code>
<code>| o|</code>
<code>| . |</code>
<code>| |</code>
<code>+-----------------+</code>
2、查看生成的公钥。
<code>[root@localhost ~]</code><code># cat ~/.ssh/id_rsa.pub</code>
3、将公钥复制到码云这里。
4、测试是否可以连接到码云服务器。
<code>[root@localhost ~]</code><code># ssh -T [email protected]</code>
<code>The authenticity of host </code><code>'git.oschina.net (116.211.167.14)'</code> <code>can't be established.</code>
<code>RSA key fingerprint is e3:ee:82:78:fb:c0:ca:24:65:69:ba:</code><code>bc</code><code>:47:24:6f:d4.</code>
<code>Are you sure you want to </code><code>continue</code> <code>connecting (</code><code>yes</code><code>/no</code><code>)? </code><code>yes</code>
<code>Warning: Permanently added </code><code>'git.oschina.net,116.211.167.14'</code> <code>(RSA) to the list of known hosts.</code>
<code>Welcome to Gitee.com, 赛里! -----看到这句表示成功</code>
四、客户端(本地)初始化一个项目
1、首先设置你的姓名和邮箱地址,提交代码的时候会记录这些信息。
<code>[root@localhost ~]</code><code># git config --global user.name "gxm"</code>
<code>[root@localhost ~]</code><code># git config --global user.email "[email protected]"</code>
2、创建目录并初始化成版本库
<code>[root@localhost ~]</code><code># mkdir gxmscript</code>
<code>[root@localhost ~]</code><code># cd gxmscript</code>
<code>[root@localhost gxmscript]</code><code># git init</code>
<code>Initialized empty Git repository </code><code>in</code> <code>/root/gxmscript/</code><code>.git/</code>
3、运行如下命令(支持https和ssh方式)。
<code>[root@localhost gxmscript]</code><code># git remote add origin [email protected]:null_803_3682/service_montir.git</code>
备注1:如果输入错了,可以用如下命令删除,然后重新运行上面的命令(没输错不要看以下灰色的几行)。
<code>[root@localhost gxmscripts]</code><code># git remote -v</code>
<code>[root@localhost gxmscripts]</code><code># git remote rm origin</code>
备注2:如果要克隆项目运行git clone 项目地址
4、进入已经初始化或者克隆项目的目录
因为码云服务器上有README.md这个文件,而本地没有,所以提交的时候可能会冲突。这个时候需要选择是保留码云服务器上这个文件,还是舍弃?如果舍弃用这个命令强制推送(git push origin master -f)。而如果需要保留先执行git pull origin master从码云服务器拉过来(或者用git clone克隆下来)。我这里选择保留的方法。
<code>[root@localhost gxmscript]</code><code># git pull origin master</code>
<code>The authenticity of host </code><code>'gitee.com (116.211.167.14)'</code> <code>can't be established.</code>
<code>Warning: Permanently added </code><code>'gitee.com'</code> <code>(RSA) to the list of known hosts.</code>
<code>remote: Counting objects: 3, </code><code>done</code><code>.</code>
<code>remote: Compressing objects: 100% (2</code><code>/2</code><code>), </code><code>done</code><code>.</code>
<code>remote: Total 3 (delta 0), reused 0 (delta 0)</code>
<code>Unpacking objects: 100% (3</code><code>/3</code><code>), </code><code>done</code><code>.</code>
<code>From gitee.com:null_803_3682</code><code>/service_montir</code>
<code> </code><code>* branch master -> FETCH_HEAD</code>
<code>[root@localhost gxmscript]</code><code># ll</code>
<code>总用量 4</code>
<code>-rw-r--r-- 1 root root 81 5月 22 03:26 README.md</code>
5、提交一个程序(脚本)。
23
24
25
26
27
28
<code>[root@localhost gxmscript]</code><code># vi service_montir.sh</code>
<code>[root@localhost gxmscript]</code><code># git add service_montir.sh </code>
<code>[root@localhost gxmscript]</code><code># git commit -m "提交服务监控脚本"</code>
<code>[master 95ce665] 提交服务监控脚本</code>
<code> </code><code>1 files changed, 112 insertions(+), 0 deletions(-)</code>
<code> </code><code>create mode 100644 service_montir.sh</code>
<code> </code>
<code>[root@localhost gxmscript]</code><code># git status</code>
<code># On branch master</code>
<code>nothing to commit (working directory clean)</code>
<code>[root@localhost gxmscript]</code><code># git log</code>
<code>commit 95ce665342fff8a14d50293877c635e35700ed92</code>
<code>Author: gxm <gxm@</code><code>test</code><code>.com></code>
<code>Date: Sun May 22 03:30:00 2016 +0800</code>
<code> </code><code>提交服务监控脚本</code>
<code>commit e819f6818e1bd10f730278028603df81183ca30c</code>
<code>Author: 赛里 <[email protected]></code>
<code>Date: Sat Feb 3 10:48:42 2018 +0800</code>
<code> </code><code>Initial commit</code>
<code> </code>
<code>[root@localhost gxmscript]</code><code># git push origin master</code>
<code>Counting objects: 4, </code><code>done</code><code>.</code>
<code>Compressing objects: 100% (3</code><code>/3</code><code>), </code><code>done</code><code>.</code>
<code>Writing objects: 100% (3</code><code>/3</code><code>), 1.28 KiB, </code><code>done</code><code>.</code>
<code>Total 3 (delta 0), reused 0 (delta 0)</code>
<code>To [email protected]:null_803_3682</code><code>/service_montir</code><code>.git</code>
<code> </code><code>e819f68..95ce665 master -> master</code>
6、登录到码云服务器进程验证,提交成功。
本文转自 sailikung 51CTO博客,原文链接:http://blog.51cto.com/net881004/2068419,如需转载请自行联系原作者