CentOS7安装使用github上传/更新项目

CentOS7上传文件至github

  1. 安装

CentOS7: [root@k8s-master ~]# yum install -y git

Ubuntu: sudo apt-get install git(普通用户采用sudo,在root下可省略) 
  1. 配置用户名,邮箱(告诉github你是谁)

[root@k8s-master ~]# git config --global user.name "hrr520gzr"
[root@k8s-master ~]# git config --global user.email "huangrr17@lzu.edu.cn"
  1. 配置登录公钥,与github互联

[root@k8s-master ~]# ssh-keygen -t rsa -C "huangrr17@lzu.edu.cn"
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:lqR+hOt9obsVjEljwOrKiaHYHHmdK+FXNzRk4DxxWKg huangrr17@lzu.edu.cn
The key's randomart image is:
+---[RSA 2048]----+
|     .. o=o      |
|      .+ooo      |
|     . .Oo       |
|    . E* Bo      |
|   o .o.S.o.     |
|. o +.o+. +.     |
|o* * .oo.o.o     |
|+ * o.oo...      |
|     o. ++       |
+----[SHA256]-----+
[root@k8s-master ~]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCSj3YzFyh8iCR9h72FJYs8EcF2QLbBTqUCBlMNh+SoDCyDZ66dCdA0XzUQ5LOAkKaKR1Azlmb+3ucq+B3Axt8baxdisHGlVm3i6oa2Bm9oXPXp9Cia6kVM4A+Y+wyeZB7HgN6JvR/5dZffaFmSOUbhbS/LHDs6vxuuMcz+LP6BSi7rQF08/QkPjFW+40JmS+o88z0VPV1jUDGh42QI7iUg9a1+6C9vnubOM7bBRoAtdmT7/qgF0Cz0O+z/7k7smyDslhUsWHcUhe3biZDTJ69CeHI/r1EFHYe4j8LDGg932gCz2gnDWlJvlYgExgbZDMzbGoUZPCHdjfZoc2q+S2XH huangrr17@lzu.edu.cn




注意:将 id_rsa.pub 中的内容($ cat ~/.ssh/id_rsa.pub #此命令可查看key内容)粘贴到 Key 文本框中,随意输入一个 title**(不要有中文)**,点击 Add Key 即可。

  1. 测试连接

[root@k8s-master ~]# ssh -T git@github.com
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi hrr520gzr! You've successfully authenticated, but GitHub does not provide shell access.
# 显示"Hi hrr520gzr! You've successfully authenticated, but GitHub does not provide shell access."表示连接成功。
  1. 上传本地代码到远程仓库

# 进入你要上传文件的文件项目目录下,我的项目在test下
[root@k8s-master ~]# cd test/
[root@k8s-master test]# ls
anaconda-ks.cfg  k8s.sh  kube-flannel.yml  kubernetes-dashboard.yaml
# 初始化git
[root@k8s-master test]# git init
Initialized empty Git repository in /root/test/.git/
# 添加需要上传的文件,如果需要上传项目目录下的所有文件,可以采用 . 代替。
[root@k8s-master test]# git add .
# 远程添加 其中https://github.com/hrr520gzr/ebusiness为你仓库的地址。
[root@k8s-master test]# git remote add origin https://github.com/hrr520gzr/ebusiness
# 开始生成待上传的模型,“firstCommit”
[root@k8s-master test]# git commit -m “firstCommit”也可以改成其他的说明性的文字,最终会显示在 github 界面文件名的后边.
[master (root-commit) 17b766b] “firstCommit”
 4 files changed, 706 insertions(+)
 create mode 100644 anaconda-ks.cfg
 create mode 100644 k8s.sh
 create mode 100644 kube-flannel.yml
 create mode 100644 kubernetes-dashboard.yaml
# 由于是第一次push项目,所以得先git pull下,生成路径
[root@k8s-master test]# git pull https://github.com/hrr520gzr/ebusiness
From https://github.com/hrr520gzr/ebusiness
 * branch            HEAD       -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md
# 正式push项目,但是出现HTTP 503代码响应错误
[root@k8s-master test]# git push -u origin master
Username for 'https://github.com': hrr520gzr   
Password for 'https://hrr520gzr@github.com': 
fatal: unable to access 'https://github.com/hrr520gzr/ebusiness/': The requested URL returned error: 503

# 这是因为如果要对远程代码具有读和写的权限,有两种途径,一种是ssh,一种是用带有用户名的远程仓库地址进行push。这里只介绍第一种做法。
[root@k8s-master test]# git remote set-url origin ssh://git@github.com/hrr520gzr/ebusiness.git
# 其中此处该https为ssh开头的地址,github.com改为了git@github.com, hrr520gzr是你的用户名,ebusiness为你的项目名。
# 然后重新输入用户名和密码,一般第一次要求输入,后面无需重复。
[root@k8s-master test]# git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 4.13 KiB | 0 bytes/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To ssh://git@github.com/hrr520gzr/ebusiness.git
   afa8500..bf2736b  master -> master
Branch master set up to track remote branch master from origin.

一般来说,新建仓库的时候,把README勾选上,它的作用可以看下面英文描述。

# 那么第一次上传过项目过后,没有必要再经过这么繁杂的上传程序了。
直接从第5步开始就行。
# 进入到需要上传项目的目录,并初始化git
[root@k8s-master test]# git init
# 添加该目录下的所有文件为git mode
[root@k8s-master test]# git add .
# 添加远程git地址,注意此处已经采用了ssh协议上传
[root@k8s-master test]# git remote add origin ssh://git@github.com/hrr520gzr/test.git
# 生成mode,名字可以随便取,最好有标识性。
[root@k8s-master test]# git commit -m “test”
# 先拉取README文件,生成路径。
[root@k8s-master test]# git pull ssh://git@github.com/hrr520gzr/test.git
# 开始上传,第一个项目后无需再输入用户名和密码
[root@k8s-master test]# git push -u origin master
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 4.12 KiB | 0 bytes/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To ssh://git@github.com/hrr520gzr/test.git
   17589a7..74d927e  master -> master
Branch master set up to track remote branch master from origin.



  1. 更新项目

# 如果本地项目文件,发生变化了,可以采用本地远程同步的方式更改,也可以采用单独文件上传提交的方式
# 比如,在test项目目录下,增加了nginx-deployment.yaml ,如果项目下的所有文件都有变动,那么可以采用同步的方式,或者采用git add .的方式重新上传一遍。
[root@k8s-master test]# git add nginx-deployment.yaml
# 用-m 标识上传者.
[root@k8s-master test]# git commit -a -m "gzr"
[master 3dbed3a] gzr
 1 file changed, 22 insertions(+)
 create mode 100644 nginx-deployment.yaml
[root@k8s-master test]# git push origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 458 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 ssh://git@github.com/hrr520gzr/test.git
   74d927e..3dbed3a  master -> master
全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务