gitee远程仓库
gitee远程仓库
git是一个分布式版本控制系统,同一个git仓库可以分布在不同的机器上,但是开发团队必须保证在同一个网络中,且必须有一个项目的原始版本,通常的办法就是让一台电脑充当服务器的角色,每天24小时开机,其他每个人都可以在这台"服务器"仓库里克隆一份代码到自己的电脑上。
并且也可以把各自的代码提交到代码仓库里,也能从代码仓库拉取别人的提交。
这样的代码仓库服务器,我们可以自由的搭建,也可以选择使用免费的托管平台。
Git代码托管平台,首先推荐的是Github,世界范围内的开发者都在使用Github托管代码,可以找到大量优秀的开源项目,缺点就是访问可能会卡一点。
其次选择的就是Gitee,国内的代码托管平台,以及自建Gitlab服务器。
Gitee 提供免费的 Git 仓库,还集成了代码质量检测、项目演示等功能。对于团队协作开发,Gitee 还提供了项目管理、代码托管、文档管理的服务。
官网地址
https://gitee.com/ 注册后登录即可
创建空仓库
点击右上角的➕,新建仓库
创建完毕空仓库后,页面出现如下仓库使用方式,我们可以选择HTTPS和SSH形式下载代码的方式
到这里,我们仓库就已经创建好了,接着就是要将本地客户端和服务端连接起来,存在于两种情况
- 本地已经有一个git仓库了
- 本地还没有git仓库
配置客户端连接gitee仓库(HTTPS)
推送本地仓库到远程
HTTPS协议也就指的是使用账号密码连接
1.git全局设置
git config --global user.name "pyyu" git config --global user.email "877348180@qq.com"
2.创建git仓库
mkdir gitee_learn cd gitee_learn git init touch README.md git add README.md git commit -m "first commit" # 为本地仓库gitee_learn添加远程仓库别名origin,地址是如下链接 git remote add origin https://gitee.com/yuco/gitee_learn.git git push -u origin master # 输入正确gitee账号密码之后即可正确把代码推送到远程仓库 [yuchao@yumac ~/gitee_learn]$git push -u origin master Username for 'https://gitee.com': 877348180@qq.com Password for 'https://877348180@qq.com@gitee.com': Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 203 bytes | 203.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-5.0] To https://gitee.com/yuco/gitee_learn.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
3.此时可以访问远程代码仓库了,发现本地创建的文件已经推送上去
https://gitee.com/yuco/gitee_learn
免密推送代码(SSH)
可以直接访问地址 https://gitee.com/profile/sshkeys
我们要在客户端生成key,结合gitee实现无密码登录,在linux和windows均可以使用ssh-keygen命令生成,需要注意的是在windows下只能生成rsa加密方式的key。
# 使用如下命令,生成公私钥对 [yuchao@yumac ~/gitee_learn]$ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/Users/yuchao/.ssh/id_rsa): /Users/yuchao/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/yuchao/.ssh/id_rsa. Your public key has been saved in /Users/yuchao/.ssh/id_rsa.pub. The key fingerprint is: SHA256:PRbwYXXO1gVueXRb4K9LdNK2a7CXVOvm3fQ2bTCe8 yuchao@yumac The key's randomart image is: +---[RSA 3072]----+ | . o.. o+X| | + . = =o| | o O o| | . o + o | | S = .o+| | . o..=Xo| | .. B+=| | .=.E*| | o++o*| +----[SHA256]-----+ # 我们把公钥信息复制到gitee中 [yuchao@yumac ~/gitee_learn]$cat /Users/yuchao/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDBmqRVCYkYg6PHxugxTjBcAkl1WrMkx3OkMD/ltQAwNM+R/wKozlNwbPLY1ZMUx2A6nKmBoTdvy0rKyX2OhxzbOwoUJsW+InhDoBuY8CGUyFoqhN96lVVxa+9v8/+LYNJOXePS7FArSbo0B23yeRnL389fPVLiHXIiLtFpxFRUEv3pEUcQNdZKeKLKcdboGjHxDNZQpHmVSgWSy039oGd7f8uC4VaM6Lk6qYq4i8lUwR3CudHxcgzkjnfiMGmZdxCdnPHcWdzQvjv4AmyeBSNfrjgaNYnHxrw/OF8GwfJsqfv+xhCG9XINQQgzVp+METIu0qE4GnyXiGB/7KgFeb3t/tR0gI40s19MlgCsS6uCQrHF8Z9qc0qbhdVCVjvk/PYuFWidZHzsz25o1Ay2s5b/yoxUYDaG+drvA95G7uKti6TJ9YLM+cAJMUlgQCmLHKiwxbeKdZuboKG9Z3JzAtLqBSgqgoIFB8b/03S6y6dozciNYEBal6XSOPy+Pltv8= yuchao@yumac
此时我们就得修改代码仓库的远程别名,修改如下
# 查看当前远程配置 [yuchao@yumac ~/gitee_learn]$git remote -v origin https://gitee.com/yuco/gitee_learn.git (fetch) origin https://gitee.com/yuco/gitee_learn.git (push) # 修改为ssh协议的url [yuchao@yumac ~/gitee_learn]$git remote set-url origin git@gitee.com:yuco/gitee_learn.git [yuchao@yumac ~/gitee_learn]$git remote -v origin git@gitee.com:yuco/gitee_learn.git (fetch) origin git@gitee.com:yuco/gitee_learn.git (push) # 可以新增一个文件,用git管理提交到本地仓库后,推送到gitee # -u指定服务器地址 # 代码推送到远程主机的master主干 [yuchao@yumac ~/gitee_learn]$git push -u origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 313 bytes | 313.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-5.0] To gitee.com:yuco/gitee_learn.git 5c29d38..74c25be master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
克隆远程仓库到本地
如果我们在任意一个其他的客户端想要获取仓库的代码,我们只需要克隆一份远程仓库代码即可,在克隆之前,仍然要配置好客户端和gitee的认证关系,或是使用账号密码下载即可。
此时可以再准备一台linux机器,模拟另一个客户端
# 以配置好了ssh-key为准,直接使用ssh协议的下载 [yuchao@yumac /tmp]$git clone git@gitee.com:yuco/gitee_learn.git Cloning into 'gitee_learn'... 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. # 克隆好了之后,我们本地已然有了代码 [yuchao@yumac /tmp]$ls gitee_learn/ README.md git.txt # 此时该用户也可以继续开发新功能,然后推送到远程代码仓库,供他人下载 [yuchao@yumac /tmp/gitee_learn]$echo "我是程序员小于,我新建了一个文件" >> cc.txt [yuchao@yumac /tmp/gitee_learn]$ [yuchao@yumac /tmp/gitee_learn]$git add . [yuchao@yumac /tmp/gitee_learn]$git commit -m "add cc.txt by 小于" [master bfcf9d4] add cc.txt by 小于 1 file changed, 1 insertion(+) create mode 100644 cc.txt [yuchao@yumac /tmp/gitee_learn]$ [yuchao@yumac /tmp/gitee_learn]$ [yuchao@yumac /tmp/gitee_learn]$git push -u origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 360 bytes | 360.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-5.0] To gitee.com:yuco/gitee_learn.git 74c25be..bfcf9d4 master -> master Branch 'master' set up to track remote branch 'master' from 'origin'. # 此时远程仓库又有了新的代码 https://gitee.com/yuco/gitee_learn
git fetch使用
上述案例,我们在另一个客户端推送了新的代码,此时代码仓库中是最新的,但是其他客户端代码是不是已经旧了,我们得更新代码对把。需要把代码仓库里的更新取回本地,这就得用到git fetch命令。
git fetch
作用是取回所有分支(branch)的更新,如果只想更新特定的分支代码,可以指定分支名,比如取回远程origin仓库的master分支代码,即可这么写
git fetch origin master
案例
[yuchao@yumac ~/gitee_learn]$git fetch remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From gitee.com:yuco/gitee_learn 74c25be..bfcf9d4 master -> origin/master
取回来的更新,要在本地主机上采用远程主机名/分支名
的形式读取,比如origin主机上的master,那就是origin/master来读取。
可以采用git branch -r选项,查看远程分支,-a查看所有分支 [yuchao@yumac ~/gitee_learn]$git branch -r origin/master [yuchao@yumac ~/gitee_learn]$git branch -a * master remotes/origin/master
取回远程主机的更新以后,可以在其基础上,使用git checkout命令创建一个新分支,待会用于合并。
[yuchao@yumac ~/gitee_learn]$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' # 此时查看的是远程分支上的数据 [yuchao@yumac ~/gitee_learn]$ls README.md cc.txt git.txt [yuchao@yumac ~/gitee_learn]$git branch master * remote-master
使用git merge命令
合并远程分支的代码到master主干
# 回到master,合并分支 # 切换分支时,git已经提示你,master分支的代码已经落后了,可以更新了 [yuchao@yumac ~/gitee_learn]$git checkout master Switched to branch 'master' Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) # 将分支代码合并到主干,结束操作 [yuchao@yumac ~/gitee_learn]$git merge remote-master Updating 74c25be..bfcf9d4 Fast-forward cc.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 cc.txt