gitee远程仓库
git是一个分布式版本控制系统,同一个git仓库可以分布在不同的机器上,但是开发团队保证在同一个网络中,且必须有一个项目的原始版本,通常的办法就是让一台电脑充当服务器的角色,每天24小时开机,其他每个人都可以在这台“服务器”仓库里克隆一份代码到自己的电脑上。
并且也可以把各自的代码提交到代码仓库里,也能从代码仓库拉取到别人的提交
这样的代码仓库服务器,我们可以自由的搭建,也可以选择使用免费的托管平台
Git代码托管平台,首先推荐的是gitub,世界范围内的开发者都在使用gitub托管代码,可以找到大量优秀的开源项目缺点就是访问可能会卡一点。
其次选择的就是gitee,国内的代码托管平台,以及自检gitlab服务器
Git提供免费的git仓库,还集成了代码质量检测,项目延时等功能。对于天对写作开发,gitee还提供了项目管理、代码托管、文档管理的服务
官方网址https://gitee.com/注册后登录即可
创建空仓库
点击右上角的+,新建仓库
创建完毕空仓库后,页面出现如下仓库使用方式。我们可以选择HTTPS形式下载代码的方式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tpnBuYN7-1614647073718)(file:///C:/Users/DIAHAO~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg)]
将本地客户端和服务端连接起来,存在于两种情况
● 本地已经有一个git仓库了
● 本地还没有git仓库
配置客户端连接gitee仓库(HTTPS)
推送到本地仓库远程
HTTOS协议也就是使用账号密码连接
1.git全局设置
git config --global user.name "daihao"
git config --global user.email "437196165@qq.com"
2.创建git仓库
mkdir gitee
cd gitee
git init
touch README.md
git add README.md
#为本地仓库gitee_learn添加远程仓库别名origin,地址是如下链接
git commit -m "first commit"
git remote add origin https://gitee.com/dai4371/gitee.git
#输入正确gitee账号密码之后即可正确把代码推送到远程仓库
git push -u origin master
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YIDKxNjP-1614647073722)(file:///C:/Users/DIAHAO~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg)]
3.此时可以访问远程代码仓库了,发现本地创建的文件已经推送上去了
https://gitee.com/dai4371/gitee.git
免密推送代码(ssh)
可以直接访问地址
https://gitee.com/profile/sshkeys
在客户端生成key,结合gitee实现无密码登录,在Linux和Windows均可使用ssh-keygen命令生成,需要主要的是在Windows下只能rsa加密方式的key
1. 使用如下命令,生成公私钥对
[root@lb02 gitee]# ssh-keygen -t rsa
2. 把公钥信息复制到gitee中
[root@lb02 gitee]# cat ~/.ssh/id_rsa.pub
ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAABAQC4gD7SLaz5ZzkxUsXSMo8dWracYIX6d27KNwksjqN66fe6+WP3MOgr7CsDHYnYPFHHTiw++ydMnLFfL5h9ClxSRAJqEOJ/iXjRJ7S2O9Qcqf2adKCbXkRIsKDvLM7r1wQtsr6V0RIORYyp0ZlavkQaA5og9EyUTEbPaid0SlO4y4SJaP5prUJpNUPwBt1kB7d5NR3cB+9Q62+F6NeISNhJK3TiVG6PjDGAoWF63CiKad5fFuDHeeETKCOYtIHyxcUMuCjvCeOlIt8jMNcDxYtVbQK+fNaU1ZXNeklZT+WAw9gq98BO5n7vfdGEwkFo0PJQEPwLwpJUBYPzZcfA5/RT root@lb02
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SKuOLoXK-1614647073724)(file:///C:/Users/DIAHAO~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg)]
此时我们旧的修改代码仓库的远程别名,修改如下
1. 查看当前远程配置
[root@lb02 gitee]# git remote -v
origin https://gitee.com/dai4371/gitee.git (fetch)
origin https://gitee.com/dai4371/gitee.git (push)
2. 修改为ssh协议的url
[root@lb02 gitee]# git remote set-url origin git@gitee.com:dai4371/gitee.git
[root@lb02 gitee]# git remote -v
origin git@gitee.com:dai4371/gitee.git (fetch)
origin git@gitee.com:dai4371/gitee.git (push)
3.可以新增一个文件,用git管理提交到本地仓库后,推送到gitee
# -u指定服务器地址
# 代码推送到远程主机的master主干
root@lb02 gitee]# touch data.py
[root@lb02 gitee]# git add .
[root@lb02 gitee]# git commit -m "touch data.py"
[master 2cbba67] touch data.py
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 data.py
[root@lb02 gitee]# git push -u origin master
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 239 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To git@gitee.com:dai4371/gitee.git
b8d0428..2cbba67 master -> master
Branch master set up to track remote branch master from origin.
克隆远程仓库到本地
如果我们在任意一个其他的客户段思想要获取仓库的代码,我们只需要克隆一份远程仓库代码即可
在克隆之前,仍然要配置好客户端和gitee的认证关系,或是使用账号密码下载即可。
此时可以在准备一台Linux机器,模拟另一个客户端
1.配置好客户端和gitee的认证关系,并将密钥添加至gitee
[root@web01 ~]# ssh-keygen -t rsa
[root@web01 ~]# cat ~/.ssh/id_rsa.pub
拷贝至gitee, https://gitee.com/profile/sshkeys
2. 以配置好了ssh-key为准,直接使用ssh协议的下载
[root@web01 ~]# git clone git@gitee.com:dai4371/gitee.git
Cloning into 'gitee'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), done.
3. 克隆好了之后,我们本地已经有了代码
[root@web01 ~]# ls gitee/
data.py README.md
4. 此时该用户也可以继续开发新功能,然后推送到远程代码仓库,供他人下载
[root@web01 tmp]# git config --global user.name "xiaohao"
[root@web01 tmp]# git config --global user.email "xiaohao@163.com"
[root@web01 tmp]# echo "我是程序员,我新建了一个文件" >> cc.txt
[root@web01 tmp]# git init
Initialized empty Git repository in /tmp/.git/
[root@web01 tmp]# git add .
[root@web01 tmp]# git commit -m "add cc.txt by xd"
[master (root-commit) fb69045] add cc.txt by hao
1 file changed, 1 insertion(+)
create mode 100644 cc.txt
#当出现如下报错时执行如下命令原因是没有绑定远程仓库
[root@web01 tmp]# git push -u origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
[root@web01 tmp]# git remote add origin git@gitee.com:dai4371/gitee.git
#出现这个问题时因为gitee中的README.文件不在本地代码目录可以通过如下命令进行代码合并
[root@web01 tmp]# git push -u origin master
To git@gitee.com:dai4371/gitee.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:dai4371/gitee.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[root@web01 tmp]# git pull --rebase origin master
#然后再次执行上述推送命令即可
[root@web01 tmp]# git push -u origin master
Counting objects: 8, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 669 bytes | 0 bytes/s, done.
Total 7 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To git@gitee.com:dai4371/gitee.git
2cbba67..4b61519 master -> master
Branch master set up to track remote branch master from origin.
5.远程仓库有了新的代码
https://gitee.com/dai4371/gitee
git fetch使用
上述案例,我们在另一个客户端推送了新的代码,此时代码仓库中是最新的,但是其他客户端代码是不是已经旧了。我们的更新代码对吧。需要把代码仓库里的更新取回本地,这就得用到git fetch命令
git fetch作用是娶回所有分支(branch)的更新,如果只想更新代码特定的分支代码,可以指定分支别名,比如却会远程origin仓库得master分支代码,即可这么写:
git fetch origin master
案例
[root@lb02 gitee]# git fetch #此时是没有更新的数据的
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (7/7), done.
From gitee.com:dai4371/gitee
2cbba67..4b61519 master -> origin/master
取回来的更新,要在本地机器上采用远程主机名/分支名的形式读取,比如origin主机上的master,那就是origin/master来读取
1.可以采取git branch -r选项,查看远程分支,-a查看所有分支
[root@lb02 gitee]# git branch -r
origin/master
[root@lb02 gitee]# git branch -a
* master
remotes/origin/master
2.娶回远程主机更新以后,可以在其他基础上,使得git checkout命令创建一个新的分支,待会用于合并。
[root@lb02 gitee]# git checkout -b remote-master origin/master
Branch remote-master set up to track remote branch master from origin.
Switched to a new branch 'remote-master'
3.此时查看的是远程分支上的数据,注意*符号位置
[root@lb02 gitee]# ls
cc.py data.py README.md
[root@lb02 gitee]# git branch
master
* remote-master
4.使用git metge命令,合并远程分支的代码到master主干
# 回到master,合并分支
# 切换分支时,git已经提示你,master分支的代码已经落后了,可以更新了
[root@lb02 gitee]# git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
5.将分支代码合并到主干,结束操作
[root@lb02 gitee]# git merge remote-master
Updating 2cbba67..4b61519
Fast-forward
cc.py | 1 +
1 file changed, 1 insertion(+)
create mode 100644 cc.py
(use “git pull” to update your local branch)
5.将分支代码合并到主干,结束操作
[root@lb02 gitee]# git merge remote-master
Updating 2cbba67…4b61519
Fast-forward
cc.py | 1 +
1 file changed, 1 insertion(+)
create mode 100644 cc.py