Git配置远程仓库(密匙链接)
主机名 | IP | 备注 |
---|---|---|
git01 | 192.168.200.31 | git测试客户端一 |
git02 | 192.168.200.32 | git测试客户端二 |
1.建立远程仓库
首先在浏览器中输入网址https://github.com
- #服务器创建密匙
- [root@git01 ~]# 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:hqeoMxJx57FYYqSsryd3KD6DpAXCMgLXFM8G/LqfACY root@git01
- The key's randomart image is:
- +---[RSA 2048]----+
- | ++. |
- |. o o+ |
- |++ .+ |
- |Oo+ +... |
- |E*o* +. S |
- |o+o.+. + |
- |++ .o.. |
- |*oB.o. . |
- |oO++ .o |
- +----[SHA256]-----+
- #查看密匙
- [root@git01 ~]# cat /root/.ssh/id_rsa.pub
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfJK+sdi8thBg+0VpbuBYyJIm5u4Mzrrk9jmu36/m9gEJiIsVkzWR4F51uuNwYwqEWuCl4f7BY/rDGe/eRlv4c2Qn0C0APNjNqRNXQXEfm7HFnWrjc75TybX3OkM6CEskKwAqB7YTNwFSt0DTic8sOuQKp+Y1o32n9tr/gjUCPZkx3+NLbXODws/Lroew0p21XVNH4Fd8H/k6/uciLm5vpXmBhy5a49uikkn0Jyx/wRUNZfRAhkmRKBTVgzdrcZ8W0yQjyFP7DjiCwnBXzP9NKyAhOVXlpEnfpGfqUBFZPl8c/SzHPDNbedbdM9CTbly0cm0yDgrO+TUjD6b7a87E7 root@git01
2.yum安装git客户端
- [root@git01 ~]# yum -y install git
-
- [root@git01 ~]# git --version
- git version 1.8.3.1
3.init初始化GIT工作目录
- [root@git01 ~]# mkdir -p /mycode
- [root@git01 ~]# cd /mycode/
- [root@git01 mycode]# git init
- 初始化空的 Git 版本库于 /mycode/.git/
4.add将变更添加进入暂存区
- [root@git01 mycode]# touch test.txt
- [root@git01 mycode]# echo "这是一次测试" > test.txt
- [root@git01 mycode]# cat test.txt
- 这是一次测试
-
- [root@git01 mycode]# git add test.txt
- [root@git01 mycode]# git status #查看git工作目录的暂存区状态
- # 位于分支 master
- #
- # 初始提交
- #
- # 要提交的变更:
- # (使用 "git rm --cached <file>..." 撤出暂存区)
- #
- # 新文件: test.txt
- #
5.Git全局配置
- [root@git01 mycode]# git config --global user.name "Mr.yang"
- [root@git01 mycode]# git config --global user.email "1773464408@qq.com"
-
- #说明:如果没有提前设置Git的全局配置,那么在第一次进行代码提交的时候,会要求输入使用者的邮箱和姓名
6.commit将变更从暂存区提交到本地仓库
- [root@git01 mycode]# git commit -m "第一次测试"
- [master(根提交) 84a5a58] 第一次测试
- 1 file changed, 1 insertion(+)
- create mode 100644 test.txt
7.remote add添加一个远程仓库的URL
- [root@git01 mycode]# git remote add test git@github.com:ywb971108/ceshi.git
-
- [root@git01 mycode]# git remote -v
- test git@github.com:ywb971108/ceshi.git (fetch)
- test git@github.com:ywb971108/ceshi.git (push)
8.push将本地仓库的变更推送到远程仓库的某个分支
命令格式: git push -u <远程仓库的名字> <远程仓库的某一分支名字>
- [root@git01 mycode]# git push -u test master
- 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.
- Counting objects: 3, done.
- Writing objects: 100% (3/3), 246 bytes | 0 bytes/s, done.
- Total 3 (delta 0), reused 0 (delta 0)
- To git@github.com:ywb971108/ceshi.git
- * [new branch] master -> master
- 分支 master 设置为跟踪来自 test 的远程分支 master。
9.刷新浏览器查看
10.clone克隆一个现有仓库到本地
我们在另一台git02上来模拟其他客户端进行对远程仓库克隆到本地仓库的操作
git02服务器需要添加密匙到github
- [root@git02 ~]# mkdir -p /mycode2
- [root@git02 ~]# cd /mycode2/
-
- [root@git02 mycode2]# git clone git@github.com:ywb971108/ceshi.git
- 正克隆到 'ceshi'...
- 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
- 接收对象中: 100% (3/3), done.
- [root@git02 mycode2]# ls
- ceshi
-
- [root@git02 mycode2]# cd ceshi/
- [root@git02 ceshi]# ls
- test.txt
-
- [root@git02 ceshi]# cat test.txt
- 这是一次测试
11.修改仓库里的文件内容,并提交变更到本地,推送变更到远程仓库
- [root@git02 ceshi]# echo "客户端测试" >> test.txt
- [root@git02 ceshi]# cat test.txt
- 这是一次测试
- 客户端测试
-
- [root@git02 ceshi]# git add test.txt
- [root@git02 ceshi]# git commit -m "增加了一行内容"
- [master 1c90ca0] 增加了一行内容
- 1 file changed, 1 insertion(+)
12.客户端推送到远程仓库
- #添加对远程仓库的管理
- [root@git02 ceshi]# git remote add test git@github.com:ywb971108/ceshi.git
-
- #推送到远程仓库
- [root@git02 ceshi]# git push test master
- Counting objects: 5, done.
- Writing objects: 100% (3/3), 299 bytes | 0 bytes/s, done.
- Total 3 (delta 0), reused 0 (delta 0)
- To git@github.com:ywb971108/ceshi.git
- 84a5a58..1c90ca0 master -> master
13.web页面查看推送结果