Linux下使用git命令及github项目

简介:

Linux下使用git命令及github项目

说明:于2017年8月26日测试ok(金工)

  

在linux下搭建git环境

1、注册Github账号,网站:https://github.com

2、Linux创建SSH密钥:

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


方法二:ssh-keygen -t rsa -C "12700696@qq.com" //生成密钥,邮箱同上

cat  ~/.ssh/id_rsa.pub //提交密钥,复制里面的密钥


方法三:非交互式生成密钥:ssh-keygen  -t rsa -N '' -f  ~/.ssh/id_rsa -C "12700696@qq.com"    


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

方法:登录到github网页中登陆自己的账号,然后再account setting中,找到SSH KEY讲复制的密钥加入(需要再次输入github的密码)


4、测试验证是否成功。

ssh -T git@github.com     提示successfully说明成功

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


同步github到本地

1、复制项目到本地:

git clone git@github.com:rshare/docker-installer.git   ##以SSH方式克隆到本地,可以读写  

git clone https://github.com/rshare/docker-installer.git ##以https方式克隆到本地,可以读写  


git同步的其他命令:

git clone git://github.com:xxxx/test.git ##以gitreadonly方式克隆到本地,只可以读  

git clone git@github.com:xxx/test.git  ##以SSH方式克隆到本地,可以读写  

git clone https://github.com/xxx/test.git ##以https方式克隆到本地,可以读写  

git fetch git@github.com:xxx/xxx.git  ##获取到本地但不合并  

git pull git@github.com:xxx/xxx.git ##获取并合并内容到本地  


本地提交项目到github

1、本地配置

git config --global user.name 'onovps'   #设置用户名标识

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

git config --list    #查看git环境设置


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 xx git@github.com:rshare/docker.git  #增加一个远程服务器的别名。  

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

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


分支版本操作

1、创建和合并分支

git branch #显示当前分支是master  

git branch new-feature  #创建分支  

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

vi  index.php  

git add  index.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的代码  


--------------------------------------------

实例:rshare个人github同步数据实战。

第1步,登录github:

1、注册Github账号,网站:https://github.com   我的账号:rshare  密码:pass000

2、Linux创建SSH密钥:

方法一:非交互式生成密钥:ssh-keygen  -t rsa -N '' -f  ~/.ssh/id_rsa -C "12700696@qq.com"    

cat  ~/.ssh/id_rsa.pub //提交密钥,复制里面的密钥(第3步要粘贴)

说明:ssh-keygen是ssh密钥生成器,-t指定算法为rsa,-N指定密码为空,-f指定私钥位置,-C指定注释信息(不指定则为主机名)。

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

方法:登录到github网页中登陆自己的账号,然后再account setting中,找到SSH KEY讲复制的密钥加入(需要再次输入github的密码)


4、测试验证是否成功。

ssh -T git@github.com     提示successfully说明成功

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


##同步github到本地

1、复制项目到本地:

mkdir  /test

cd  /test

git clone git@github.com:rshare/mydocker.git   ##以SSH方式克隆到本地,可以读写  

git clone https://github.com/rshare/docker-installer.git ##以https方式克隆到本地,可以读写  

ls  查看文件列表


2、创建新文件,并上传到github中。

cd  /test/mydocker

ls -a

git  remote

cp  -v  /etc/{hosts,group,passwd}  ./

git  add  hosts  group  passwd

git  status

git  commit  -m  '3files'

git  push  -u  origin  master

验证:在github个人网站的mydocker仓库中查看是否多了三个文件(hosts,group,passwd)。


git其他命令:

git branch   #查看分支(即目录结构) 



3、删除远程仓库中的文件。

cd  /test/mydocker

ls  -a

git rm --cached  hosts  group  passwd    #删除缓存数据,并不删除本地文件

git  status

git commit -m "hehe" 

git push origin 

验证:在github个人网站的mydocker仓库中查看是否少了三个文件(hosts,group,passwd)。

wKioL1mhLzLyag8jAABtT2HzZkY894.png


====网络Docker  hub的使用=============

flyer520docker镜像下载和上传实例:

1,注册docker-hub账号。在https://hub.docker.com/网站注册。

注:我的账号是flyer520,邮箱12700696@qq.com

 

2步,linux登录docker测试。

我的账号:docker login --username=flyer520  --email=12700696@qq.com  (登录OK)

查看登录信息:cat  ~/.docker/config.json

 

3步,下载docker镜像。

docker  pull busybox

docker  pull nginx

docker  images

 

4步,修改标签(重设标签)

测试1:修改nginx标签,并push上传到flyer520docker-hub中。

docker  tag  nginx  flyer520/nginx

docker  push flyer520/nginx

 

测试2:修改busybox标签,并push上传到flyer520docker-hub中。

docker  tag   busybox  flyer520/busybox

docker  push flyer520/busybox

 

验证:登录到docker-hub,查看是否多了nginx镜像。

 wKioL1mhLtbTn3VpAACk9xJHD2Q721.png


##搭建内部git仓库服务器

wKiom1mlX9OSC-8TAAJG87GPEtE090.png


待续....


到此,本实验操作完毕。







      本文转自rshare 51CTO博客,原文链接:http://blog.51cto.com/1364952/1959524,如需转载请自行联系原作者



相关文章
|
3天前
|
SQL 缓存 监控
|
3天前
|
前端开发 Linux Shell
|
3天前
|
Shell 网络安全 开发工具
GIT常用命令
GIT常用命令
|
3天前
|
Linux
Linux系统ps命令
这些是一些常见的 `ps`命令选项和用法,用于查看系统中运行的进程及其相关信息。您可以根据需要选择合适的选项以满足您的任务要求。
11 0
|
3天前
|
存储 Linux Shell
linux课程第二课------命令的简单的介绍2
linux课程第二课------命令的简单的介绍2
|
3天前
|
Linux C语言 数据安全/隐私保护
linux课程第二课------命令的简单的介绍3
linux课程第二课------命令的简单的介绍3
|
3天前
|
监控 Unix Linux
如何使用 Linux less 命令?
【4月更文挑战第25天】
13 1
如何使用 Linux less 命令?
|
3天前
|
JSON 网络协议 Linux
Linux ip命令:网络的瑞士军刀
【4月更文挑战第25天】
9 1
|
3天前
|
安全 Linux C语言
linux课程第一课------命令的简单的介绍
linux课程第一课------命令的简单的介绍
|
3天前
|
网络协议 Linux Shell