CI/CD上传项目到Github

一.安装git

  • yum安装
[root@cicd-gitlab ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@cicd-gitlab ~]# yum -y install git-core

[root@cicd-gitlab ~]# git --version
git version 1.8.3.1
  • 源码安装

最新源码包下载地址:https://git-scm.com/download
安装系统的依赖包

[root@cicd-gitlab ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
  • 解压下载的源码包
[root@cicd-gitlab ~]# tar -zxf git-1.7.2.2.tar.gz
[root@cicd-gitlab ~]# cd git-1.7.2.2
[root@cicd-gitlab ~]# make prefix=/usr/local all
[root@cicd-gitlab ~]# make prefix=/usr/local install

二.Github远程仓库

1.本地服务器生成公钥

[root@cicd-git git-root]# ssh-keygen
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:
SHA256:m61pBo46kjJ+6kakfYP7rhfRRNcxeHJqVlwWcruNp3o root@cicd-git
The key's randomart image is:
+---[RSA 2048]----+
|     .. .++o=.   |
|      ..o *= .   |
|     o   *  .    |
| .  . . +    +   |
|o. . . oS   o o  |
|..o + .  +   o   |
|.. o = .o . .    |
|=.o.o . oo .E    |
|=*=Bo  oo ..     |
+----[SHA256]-----+
  • 查看本地的公钥
[root@cicd-git git-root]# cat /root/.ssh/id_rsa.pub

ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQCY3APi47otRbj8tOVqebvvgYvE7gHV8+bGFT6zGwqFxXUH+uVSzd+JTZ9SSj/YHYSOY2M7sTa/QWbBvBDvMElJjAx+fbQEFT4ZPTqQBZKtyscuQOup4gRC5txYvksYDceelADQ70yf49VsbQ4OFybRyCvjYEGxn5QlPMevOBHTrDwQqps1Rc3bWvKRUtd62Bbs2TI68jscgZElhmb1jEP4CieXE14TVxZXP1tyYc6FqQEcTCiMdqySFKAc3uWD8FOaA5YSQp7iA+T6wrjecjj4SpQRNg1OriXi1U9X7UQzooUueB0fWBNlUiO3eIoiRzRfyHPzsR71cXxnckOjyFuH
root@cicd-git

2.注册一个Github

  • Github界面
    在这里插入图片描述

3.将公钥添加到Github中

在这里插入图片描述
在这里插入图片描述

  • 点击添加,然后确定

在这里插入图片描述

4.测试连接

在这里插入图片描述

  • 测试
[root@cicd-git git-root]# ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Hi Huang-yan-hao! You've successfully authenticated, but GitHub does not provide shell access.

三.连接远程仓库

方法一

  • 创建一个目录,直接将项目克隆下来
[root@cicd-git ~]# mkdir test
[root@cicd-git ~]# cd test/
[root@cicd-git test]# git clone git@github.com:Huang-yan-hao/git-root.git  #克隆项目
正克隆到 'git-root'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 12 (delta 1), reused 9 (delta 1), pack-reused 0
接收对象中: 100% (12/12), done.
处理 delta 中: 100% (1/1), done.
[root@cicd-git test]# cd git-root/
[root@cicd-git git-root]# ls
a.txt  b.sh  c.sh  README.md
[root@cicd-git git-root]# vim hello.sh
[root@cicd-git git-root]# ls
a.txt  b.sh  c.sh  hello.sh  README.md
[root@cicd-git git-root]# git add .
[root@cicd-git git-root]# git commit -m "hello"
[master 57c4d32] hello
 1 file changed, 1 insertion(+)
 create mode 100644 hello.sh
[root@cicd-git git-root]# git push -u origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 268 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:Huang-yan-hao/git-root.git
   85915f3..57c4d32  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

在这里插入图片描述

方法二

[root@cicd-gitlab ~]# cd /opt
[root@cicd-git opt]# mkdir git-root
[root@cicd-git opt]# cd git-root/
[root@cicd-git git-root]# git init           #给目录初始化
初始化空的 Git 版本库于 /opt/git-root/.git/
[root@cicd-git git-root]# ls -a              #初始化产生的.git文件
.  ..  .git
[root@cicd-git git-root]# touch a.txt        #随便创建一个文件
[root@cicd-git git-root]# git add .          # 将文件夹下的所有的文件添加到暂存区 
[root@cicd-git git-root]# git commit -m 'a'  # 将暂存区中的文件保存成为某一个版本 
[master(根提交) d7e1ae7] a 
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
[root@cicd-git git-root]# git remote add origin git@github.com:Huang-yan-hao/git-root.git
                                              #git@github.com:Huang-yan-hao/git-root.git为上图图五中SSH中的内容,复制即可

[root@cicd-git git-root]# cat .git/config     #查看内容,已经添加
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github.com:Huang-yan-hao/git-root.git
        fetch = +refs/heads/*:refs/remotes/origin/*

[root@cicd-git git-root]# git push -u origin master
To git@github.com:Huang-yan-hao/git-root.git
 ! [rejected]        master -> master (fetch first)
error: 无法推送一些引用到 'git@github.com:Huang-yan-hao/git-root.git'
提示:更新被拒绝,因为远程版本库包含您本地尚不存在的提交。这通常是因为另外
提示:一个版本库已推送了相同的引用。再次推送前,您可能需要先合并远程变更
提示:(如 'git pull')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。

[root@cicd-git git-root]# git pull --rebase origin master  #提交到远端仓库,错误执行下条命令
warning: no common commits
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 10 (delta 0), reused 7 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
来自 github.com:Huang-yan-hao/git-root
 * branch            master     -> FETCH_HEAD
首先,重置头指针以便在上面重放您的工作...
[root@cicd-git git-root]# git pull --rebase origin master  #先将仓库中的东西拉下来
来自 github.com:Huang-yan-hao/git-root
 * branch            master     -> FETCH_HEAD
当前分支 master 是最新的。

[root@cicd-git git-root]# ls
a.txt  b.sh  README.md

[root@cicd-git git-root]# vim c.sh
[root@cicd-git git-root]# git add .
[root@cicd-git git-root]# git commit -m 'a'
[master 85915f3] a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c.sh
[root@cicd-git git-root]# git push -u origin master  #添加到远端仓库成功
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 224 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:Huang-yan-hao/git-root.git
   685f446..85915f3  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

在这里插入图片描述

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页