【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地

远程连接阿里云服务器,确保安全组内ssh端口打开(如果更改ssh连接端口的话),按照下面代码依次敲

root用户下登录
[root@iz2ze8w7169ei61c5c1il5z /]cd ~
[root@iz2ze8w7169ei61c5c1il5z ~]mkdir git-demo
[root@iz2ze8w7169ei61c5c1il5z ~]cd git-demo
[root@iz2ze8w7169ei61c5c1il5z git-demo]git init
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git init
Reinitialized existing Git repository in /root/git-demo/.git/
[root@iz2ze8w7169ei61c5c1il5z git-demo]# echo 'Hello World' >README.MD
[root@iz2ze8w7169ei61c5c1il5z git-demo]# ll -a 
total 16
drwxr-xr-x 3 root root 4096 Jun 19 11:06 .
drwxr-xr-x 4 root root 4096 Jun 16 16:51 ..
drwxr-xr-x 8 root root 4096 Jun 19 11:04 .git
-rw-r--r-- 1 root root   12 Jun 19 11:06 README.MD
[root@iz2ze8w7169ei61c5c1il5z git-demo]# more README.MD 
Hello World
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git add .
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git commit -m "first commit"

键入https://github.com/

【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第1张图片

【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第2张图片

【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第3张图片

[root@iz2ze8w7169ei61c5c1il5z git-demo]# git remote add first https://github.com/Drzhu/test.git
first可随意指定,后面对应即可
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git push -u first master
Username for 'https://github.com': Drzhu
Password for 'https://[email protected]': 
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 434 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/Drzhu/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from first.

再刷新页面— OK
【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第4张图片

上述方式每次都需要输入用户名和密码。
下面的方法设置免密码上传代码:
依次键入

 ssh-keygen -t rsa -C "[email protected]" //邮箱地址为github上注册的邮箱
 之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key
[root@iz2ze8w7169ei61c5c1il5z git-demo]# more ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWf+/pDrURgdmKdXxgI+D4+I6QQpS/2UF7mgth5PbDMOaEZ7o7XEtWq9Sa2IxkVumXdDt7wb8/JYcKPiQWaJ70PGVqzxjeKRy3VEaOSRJ78PzY0QYVby3xuiKOqJMTgOVdpgt8Z1IGOeYY08TYb9xjSHLrzTCRVzZTjLZjtEJLG0xCAK+THHIERTYxtuYrSisqtTl8MscN+kJCX7IIt5O51duEVzmycXsD3u/Noc
3yRqzgwmECg7dt+gs6W6opu9yAmub0p/p4GpYH36DjnRYByFTgQ6IAsyi+jvx+kz6Op3wiQs/cicVYY1QHZCCQQjfPDylqxE8iGa+otC6qMbZj [email protected]

【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第5张图片

把对应的ssh-key复制到对应区域

【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第6张图片

然后键入:

ssh -T [email protected]
如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。
然后配置你的全局变量
git config --global user.name "your name"
git config --global user.email "[email protected]"
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git config --list
查看你的全局参数列表

配置成功之后:

[root@iz2ze8w7169ei61c5c1il5z git-demo]# git remote add nologin [email protected]:Drzhu/test.git//无需输入用户名和密码,表示配置成功
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git remote -v
first   https://github.com/Drzhu/test.git (fetch)
first   https://github.com/Drzhu/test.git (push)
nologin git@github.com:Drzhu/test.git (fetch)
nologin git@github.com:Drzhu/test.git (push)
显示有两个远程连接,配置成功
下面进行验证:
[root@iz2ze8w7169ei61c5c1il5z git-demo]# echo "my name is nologin" > README.MD 
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git add .
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git commit -m "i am nologin"
[master 7c1b386] i am nologin
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@iz2ze8w7169ei61c5c1il5z git-demo]# git push -u nologin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:Drzhu/test.git
   0908acf..7c1b386  master -> master
Branch master set up to track remote branch master from nologin.

刷新页面:

【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第7张图片

更新github代码到本地
我们在github上修改代码:然后点击下面的绿色的提交按钮。

【git】阿里云上传代码到github附SSH-KEY免密码上传&更新github代码到本地_第8张图片

git bash中键入:

[root@iz2ze8w7169ei61c5c1il5z git-demo]# git fetch nologin master:temp-nologin
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:Drzhu/test
 * [new branch]      master     -> temp-nologin
   7c1b386..269aafe  master     -> nologin/master

[root@iz2ze8w7169ei61c5c1il5z git-demo]# git branch
* master
  temp-nologin

[root@iz2ze8w7169ei61c5c1il5z git-demo]# more README.MD 
my name is nologin

[root@iz2ze8w7169ei61c5c1il5z git-demo]# git checkout temp-nologin
Switched to branch 'temp-nologin'

[root@iz2ze8w7169ei61c5c1il5z git-demo]# more README.MD 
my name is nologin
had changed in github [root@iz2ze8w7169ei61c5c1il5z git-demo]# git checkout master Switched to branch 'master' [root@iz2ze8w7169ei61c5c1il5z git-demo]# git merge temp-nologin Updating 7c1b386..269aafe Fast-forward README.MD | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) [root@iz2ze8w7169ei61c5c1il5z git-demo]# more README.MD my name is nologin
had changed in github

OK

你可能感兴趣的:(Linux)