在ECS上搭建 linux git环境

举报
tengyun 发表于 2019/07/21 23:57:19 2019/07/21
【摘要】 在linux下搭建git环境

最近参加华为云的“ModelArts-Lab AI实战营”,经常会用到git下载代码,并进行分享,下面重新整理了如何在linux下搭建git环境:

1、在Linux上安装Git

首先,你可以试着输入git,看看系统有没有安装Git.

如果你使用的时Debian或Ubuntu Linux,通过一条sudo apt-get install git就可以直接完成Git的安装,

如果是Centos,可以用yum install git,非常简单。

如果是其他Linux版本,可以直接通过源码安装。先从Git官网下载源码,然后解压,依次输入:

./config,

make,

sudo make install

这几个命令就安装好了。

2、创建Github账号,https://github.com

3、Linux创建SSH密钥:

ssh-keygen  ##一直默认就可以了


[root@ecs-9983 ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

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:

SHA256:3:*********************************************root@ecs-9983

The key's randomart image is:

+---[RSA 2048]----+

|             oOEX|

|              X=^|

|             +.@+|

|            oo= =|

|        S  ..o=.o|

|         . ..= =o|

|          . + o.=|

|             . oo|

|              .  |

+----[SHA256]-----+

在/root/.ssh 下得到 id_rsa 和 id_rsa.pub 文件,说明生成成功


3、将公钥加入到Github账户信息Account Settings->SSH Key

Title--》添加描述

key-->将刚才~目录下.ssh/id_rsa.pub 的内容copy 到剪切板,再copy 到key 输入框中


4、测试验证是否成功。


[root@ecs-9983 .ssh]# ssh -T git@github.com

The authenticity of host 'github.com (52.74.223.119)' can't be established.

RSA key fingerprint is SHA256:*********************************************.

RSA key fingerprint is MD5::*********************************************.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.

Hi hellfire0820-ty! You've successfully authenticated, but GitHub does not provide shell access.



同步github到本地

1、复制项目到本地:

git clone git://github.com/huaweicloud/ModelArts-Lab.git ##以gitreadonly方式克隆到本地,只可以读

git clone git@github.com/huaweicloud/ModelArts-Lab.git  ##以SSH方式克隆到本地,可以读写

git clone https://github.com/huaweicloud/ModelArts-Lab.git ##以https方式克隆到本地,可以读写

git fetch git@github.com/huaweicloud/ModelArts-Lab.git  ##获取到本地但不合并

git pull git@github.com/huaweicloud/ModelArts-Lab.git ##获取并合并内容到本地



##以https方式克隆到本地,可以读写


本地提交项目到github

1、本地配置

git config --global user.name 'hellfire'

git config --global user.email 'hellfirexx@gmail.com' #全局联系方式,可选

2、新建Git项目并提交到Github。

mkdir testdir & cd testdir

touch README.md

git init #初始化一个本地库

git add README.md #添加文件到本地仓库

git rm README.md #本地倒库内删除

git commit -m "first commit" #提交到本地库并备注,此时变更仍在本地。

git commit -a  ##自动更新变化的文件,a可以理解为auto

git remote add xxx git@github.com:xxx/xxx.git  #增加一个远程服务器的别名。

git remote rm xxx   ##删除远程版本库的别名

git push -u remotename master #将本地文件提交到Github的remoname版本库中。此时才更新了本地变更到github服务上。


分支版本操作

1、创建和合并分支

git branch #显示当前分支是master

git branch new-feature  #创建分支

git checkout new-feature  #切换到新分支

vi page_cache.inc.php

git add page_cache.inc.php

git commit -a -m "added initial version of page cache"

git push origin new-feature  ##把分支提交到远程服务器,只是把分支结构和内容提交到远程,并没有发生和主干的合并行为。

2、如果new-feature分支成熟了,觉得有必要合并进master

git checkout master  #切换到新主干

git merge new-feature  ##把分支合并到主干

git branch #显示当前分支是master

git push  #此时主干中也合并了new-feature的代码


Git虽然极其强大,命令繁多,但常用的就那么十来个,掌握好这十几个常用命令,你已经可以得心应手地使用Git了。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。