git是一个分布式版本控制系统,同一个git仓库可以分布在不同的机器上,但是开发团队必须保证在同一个网络中,且必须有一个项目的原始版本,通常的办法就是让一台电脑充当服务器的角色,每天24小时开机,其他每个人都可以在这台"服务器"仓库里克隆一份代码到自己的电脑上。
并且也可以把各自的代码提交到代码仓库里,也能从代码仓库拉取别人的提交。
这样的代码仓库服务器,我们可以自由的搭建,也可以选择使用免费的托管平台。
Git代码托管平台,首先推荐的是Github,世界范围内的开发者都在使用Github托管代码,可以找到大量优秀的开源项目,缺点就是访问可能会卡一点。
其次选择的就是Gitee,国内的代码托管平台,以及自建Gitlab服务器。
Gitee 提供免费的 Git 仓库,还集成了代码质量检测、项目演示等功能。对于团队协作开发,Gitee 还提供了项目管理、代码托管、文档管理的服务。
官网地址
https://gitee.com/
注册后登录即可
点击右上角的➕,新建仓库
创建完毕空仓库后,页面出现如下仓库使用方式,我们可以选择HTTPS和SSH形式下载代码的方式
到这里,我们仓库就已经创建好了,接着就是要将本地客户端和服务端连接起来,存在于两种情况
HTTPS协议也就指的是使用账号密码连接
1.git全局设置
git config --global user.name "Josen"
git config --global user.email "[email protected]"
2.创建git仓库
cd /opt
git init my_crm
cd my_crm/
echo "超哥带你学linux,git" > git_linux.txt
git add .
git commit -m "首次提交txt文件"
# git和码云的远程绑定操作
# 为本地仓库gitee_learn添加远程仓库别名origin,地址是如下链接
git remote add origin https://gitee.com/JosenGitee/tech_git.git
#推送代码命令
git push -u origin master
3.此时可以访问远程代码仓库了,发现本地创建的文件已经推送上去
https://gitee.com/JosenGitee/tech_git
# cd /opt/my_crm
cd existing_git_repo
# 如果该代码仓库,还未和远程仓库绑定,执行如下命令
git remote add origin https://gitee.com/JosenGitee/tech_git.git
git push -u origin master
对于码云,或者github,已经创建好代码仓库的,后续用法分为2个场景:
输入正确gitee账号密码
cd /opt
git init my_crm
cd my_crm/
echo "超哥带你学linux,git" > git_linux.txt
git add .
git commit -m "首次提交txt文件"
# git和码云的远程绑定操作
# 为本地仓库gitee_learn添加远程仓库别名origin,地址是如下链接
# 注意,可以选择ssh协议,或https协议
[root@teach_linux2 my_crm]# git remote add origin https://gitee.com/JosenGitee/tech_git.git
#如果有需要,可以删除远程别名(这里无需执行)
[root@teach_linux2 my_crm]# git remote remove origin
# 推送代码命令
# 需要输入码云的账号密码
[root@teach_linux2 my_crm]# git push -u origin master
Username for 'https://gitee.com': JosenGitee
Password for 'https://[email protected]':
Counting objects: 3, done.
Writing objects: 100% (3/3), 259 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/JosenGitee/tech_git.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
可以直接访问地址
https://gitee.com/profile/sshkeys
我们要在客户端生成key,结合gitee实现无密码登录,在linux和windows均可以使用ssh-keygen命令生成,需要注意的是在windows下只能生成rsa加密方式的key。
# 使用如下命令,生成公私钥对
[root@teach_linux2 my_crm]# ssh-keygen -t rsa
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:7BqFINcyVfFba3V7iCZQt01Y0qvxvCtvbLonVn16rmk root@teach_linux2
The key's randomart image is:
+---[RSA 2048]----+
| ..o.. o+o |
| o o ..=. |
| . = . . . o o..|
| o + o . o.+.o.|
| . S o ==.o.|
| o +. + +|
| . . o o.|
| o + E..|
| . .o@==.|
+----[SHA256]-----+
# 我们把公钥信息复制到gitee中
[root@teach_linux2 my_crm]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGAAfLNcSk4WWGBkyB+gRYlywxOyQS4qRTW8dKS52l3RoPPPRwTJ0EBLAN/hXO0GjInG8FS9IgywgE1taLnRLjoLRTr4XD6PbXFGNA6pAjkAqEWIgcHdgXcGaAenflIYq7J5wiHwzu5d7yoNj1SbCSOuae2DtDbQP0DwBQWOjaiMbtsFVrYZRbkLc7QQQpGxjBbyFI6/AF5jMl/rDOO8iERWZCB+qhEqAMig6nIDcasnmOKCfy/e82ulO2b2ljMjigoZEZm9Zw9c/pVPxkZAQxpYZY2mt0ddtGCe/5PL63MhOloSBaM2tR6UKtB229D8lMbbZqtMXl6WqNnl6Us6F7 root@teach_linux2
此时代码可以通过ssh协议,直接推送了
# 提交新版本的文件
[root@teach_linux2 my_crm]# echo "祝大家牛年快乐" > niu.sh
[root@teach_linux2 my_crm]# git add .
[root@teach_linux2 my_crm]# git commit -m "第二次提交niu.sh"
[master 40b8248] 第二次提交niu.sh
1 file changed, 1 insertion(+)
create mode 100644 niu.sh
# 删除远程别名
[root@teach_linux2 my_crm]# git remote remove origin
# 修改为ssh协议的url
[root@teach_linux2 my_crm]# git remote add origin [email protected]:JosenGitee/tech_git.git
# # 可以新增一个文件,用git管理提交到本地仓库后,推送到gitee
# -u指定服务器地址
# 代码推送到远程主机的master主干
[root@teach_linux2 my_crm]# git push -u origin master
The authenticity of host 'gitee.com (180.97.125.228)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 317 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To [email protected]:JosenGitee/tech_git.git
8d1f4be..40b8248 master -> master
Branch master set up to track remote branch master from origin.
如果我们在任意一个其他的客户端想要获取仓库的代码,我们只需要克隆一份远程仓库代码即可,在克隆之前,仍然要配置好客户端和gitee的认证关系,或是使用账号密码下载即可。
此时可以再准备一台linux机器,模拟另一个客户端
# 以配置好了ssh-key为准,直接使用ssh协议的下载
[root@teach_linux3 ~]# git clone [email protected]:JosenGitee/tech_git.git
Cloning into 'tech_git'...
The authenticity of host 'gitee.com (180.97.125.228)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
# 克隆好了之后,我们本地已然有了代码
[root@teach_linux3 ~]# ls
anaconda-ks.cfg ks-pre.log original-ks.cfg tech_git
分别在两台linux机器上,都clone下载好码云的代码
1.在机器A上修改,推送代码仓库的内容
cd /
git clone [email protected]:JosenGitee/tech_git.git
cd tech_git/
2.在机器B上,下载代码仓库的内容
cd /
git clone [email protected]:JosenGitee/tech_git.git
cd tech_git/
3.在机器A上,添加新的文件,提交新的版本
echo "超哥带你学git pull 命令" > git_pull.txt
git add .
git commit -m "学习git pull"
git push -u origin master
4.在机器B上,下载代码仓库的新内容
git pull origin master
5.此时机器B客户端本地的代码,就和远程仓库保持一致了
#机器A的代码
[root@teach_linux2 tech_git]# ls
git_linux.txt git_pull.txt niu.sh
#机器B的代码
[root@teach_linux3 tech_git]# ls
git_linux.txt git_pull.txt niu.sh
#远程仓库的代码