远程仓库
环境
主机名 | IP地址 | 角色 |
---|---|---|
gitlab | 192.168.100.33 | gitlab远程仓库 |
git | 192.168.100.128 | git本地仓库 |
-
gitlab远程仓库的搭建请看gitlab
-
新建本地仓库
[root@git ~]# mkdir /opt/code
[root@git ~]# cd /opt/code
[root@git code]# git init
Initialized empty Git repository in /opt/code/.git/
- 本次仓库生成密钥
[root@git ~]# ssh-keygen -t rsa -C "2471269000@qq.com"
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:0oy0LxViv1pqsRtzhmcMADyPosslCiZu7MHvzhhgX/o 2471269000@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| .. |
| o. |
| +. + . |
|. . .+ B . |
|oo .= S |
|Bo..o .B . |
|O=oo =oX |
|o=* . o% |
|ooo= E+. |
+----[SHA256]-----+
\\拷贝公钥
[root@git ~]# cat ./.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvZOyCU+iyJX9TPoM+sHFTsjwvt/blsDv6C/to4EUr+11OGAPX15HA1xLZ1JtnOxW9vgXpv1VBbHx/ikrKxulN86IXr6BzRDC7C2x9xVv1cBHW4XzzhULMD3RhBz1Bo3Hgf5TWQGyDXW3mWPrHDmPRcCUSKr1fpaNWbwgFee50OU9xtF8jj/RCi99ZhlWkYjJSPCudtne+0V2ZM/sLcWBkzks1ZHHPbg2IeL1E/Nd72RgIC56K34rYg8lY6Ki6mRze1sfUryXgrumfJEkubJSc5Qun9xhUI2Vb8yEH1P8ZxXv6lYT3uBz1TGgl2H+ZdxcXuAlsuNxY1ZZVU67qwXSD 2471269000@qq.com
-
创建新项目
-
在本地远程关联远程库
[root@git code]# cat test.txt
dingxiaoke
[root@git code]# git add test.txt
[root@git code]# git commit -m "new"
[root@git code]# git remote add origin git@192.168.100.33:dxk/dsb.git
- 把本地库的所有内容推送到远程库上
[root@git code]# git push -u origin master
The authenticity of host '192.168.100.33 (192.168.100.33)' can't be established.
ECDSA key fingerprint is SHA256:Jv8hH7gJNX60Ul1azgTQ1WBmlgdBlyynRjJPZfjnnmA.
ECDSA key fingerprint is MD5:bd:28:0f:12:27:e5:7c:01:f3:3a:ce:20:23:ec:59:c9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.33' (ECDSA) to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.33:dxk/dsb.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
\推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样:
小结:
关联远程库 :git remote add origin git@server-name:PATH/*.git
第一次推送master分支的所有内容 :git push -u origin master
此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改
远程库克隆
登陆GitLab,创建一个新的仓库,名字叫ddsb
\创建完毕后,可以看到README.md文件
- 现在,远程库已经准备好了,下一步是用命令git clone克隆一个本地库
[root@git code]# git clone git@192.168.100.33:dxk/bbq.git
Cloning into 'bbq'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
\\然后进入gitskills目录看看,已经有README.md文件了
[root@git code]# cd bbq/
[root@git bbq]# ls
README.md
1.要克隆一个仓库,首先必须知道仓库的地址,然后使用git clone命令克隆。
2.Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。