Git+GitHub 使用

GitHub 简单使用

在github上面新建好一个存储库,并配置好ssh-key作为连接github的秘钥


新建储存库


给GitHub添加SSH秘钥

GitHub上设置登录秘钥,主要是为了以后在服务器提交代码,可以免密直接上传。

  • 生成ssh秘钥

首先在需要提交代码的服务器上生成秘钥

# 生成ssh秘钥,生成的秘钥文件位置存在 /root/.ssh/ 目录下
$ ssh-keygen -t rsa -f /root/.ssh/id_rsa -P "" -q

github_sshkey-1.png

# 查看并复制秘钥,然后复制到GitHub相关位置
$ cd /root/.ssh
$ cat id_rsa.pub
  • GitHub添加生成的秘钥


代码上传和拉取

  • 初始化git目录
git init
  • 添加远程仓库
git remote add origin git@github.com:wanhebin/test.git
  • 推送代码到远程仓库
git push -u origin master
  • 拉取代码
#克隆
git clone git@github.com:wanhebin/test.git

#直接拉取代码,但本地需要存在一个git仓库
git pull git@github.com:wanhebin/test.git

代码上传测试

  • 在新建目录中上传代码
[root@git ~]# mkdir /tmp/github && cd /tmp/github
[root@git github]# git init
Initialized empty Git repository in /tmp/github/.git/
[root@git github]# echo "# test" > README.md
[root@git github]# git add .
[root@git github]# git commit -m "add README.md"
[master (root-commit) 4c10b74] add README.md
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
[root@git github]# git remote add origin git@github.com:wanhebin/test.git
[root@git github]# git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' 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.229.188.59' (RSA) to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 215 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:wanhebin/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[root@git github]# 

  • 在已经拉取代码的目录中上传代码
[root@git ~]# git clone git@github.com:wanhebin/test.git
Cloning into 'test'...
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@git ~]# cd test/
[root@git test]# ll
total 4
-rw-r--r-- 1 root root 7 May 12 22:28 README.md
[root@git test]# touch test.txt
[root@git test]# git add .
[root@git test]# git commit -m "add test.txt"
[master fe25a8e] add test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@git test]# git push -u origin master 
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 257 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@github.com:wanhebin/test.git
   fe25a8e..cc87145  master -> master
Branch master set up to track remote branch master from origin.
[root@git test]# 

点赞

发表回复