1、github 与 gitlab
github,是一个web界面的git管理平台,也就是说它底层的技术驱动依然是git。一句话区分,git是管理工具,github是在线的基于git的平台(或者叫做服务)。
github是公开的,谁都可以注册账号,并创建自己的项目,大部分项目以代码为主,比如wordpress、discuz都是托管在github里面的,
当然还有好多好多开源的软件。
既然github是一个公共的,在线的提供服务的平台,那企业就可以把公司内部的代码托管在这里,为了安全我们可以选择付费服务,定制个性化方案。
但毕竟github在国外,要想获得更好的功能还需要花钱,小企业或者个人用户自然是不愿意去开销了。
还好又一款开源的软件可以帮我们搭建一套类似github的平台,这就是接下来我要介绍的gitlab。 gitlab适合企业内部构建私有代码管理平台,
它有健全的权限控制系统和各种强大的功能,很赞。
所以,gitlab和github一样,也是一个基于git的提供web界面的代码管理平台;
2、github 建立远程仓库; https://github.com
1、首先需要去官网 https://github上去注册一个账号,创建自己的git,点击左上角的加号,然后点击列出的 new repository;
名称自定义,比如叫 资源,选择 public,点击 create repository;
注释:Private 私有有可能是要收费的;
2、添加key,用来互动,做远程服务端,加秘钥做验证;
点击右侧头像,向下三角,选择 setings ----- > SSH and GPG keys(左侧下拉) -----> New SSH key(左侧)
3、然后需要再linux上生成一个公钥,然后放到这里上面; ssh-keygen ---- 回车
[root@localhost_02 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
如上,秘钥验证就完成好了;接下来再本地创建文件并通过秘钥的方式上传到 github 上了;
3、然后创建完存储库后 repositories,可以有三种方式来快速使用;如下:
(1)、命令行创建一个新的存储库,然后把本地仓库的文件推送到远端 github 上; 这里有 ssh 和 https 两种方式可选; 本次 ssh 方式;
echo“#apelearn”>> README.md
git init
git add README.md
git commit -m“first commit”
git remote add origin git@github.com:yuanhh104 / apelearn.git
git push -u origin master
(2)、再命令直接推送现有存储库到远程 github 上;
git remote add origin git@github.com:yuanhh104 / apelearn.git
git push -u origin master
(3)、从另一个存储库导入代码;
下面案例讲述的是第一个方式:创建新的存储库,并推送到远程 github 上;
[root@localhost_02 tmp]# mkdir apelearn #新建一个目录
[root@localhost_02 tmp]# cd apelearn/
[root@localhost_02 apelearn]# echo "# apelearn" >> README.md #写入一个readme.md文件
[root@localhost_02 apelearn]# git init #初始化
初始化空的 Git 版本库于 /tmp/apelearn/.git/
[root@localhost_02 apelearn]# ls -la
总用量 8
drwxr-xr-x 7 root root 111 4月 9 02:43 .git #生成一个.git的文件
-rw-r--r-- 1 root root 11 4月 9 02:43 README.md
[root@localhost_02 apelearn]# git add README.md #把readme.md 提交到仓库里去
[root@localhost_02 apelearn]# git commit -m "first commit" #把readme.md 提交到仓库里去
[master(根提交) a23c110] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
#把当前仓库里的东西给推送到远程上去;
[root@localhost_02 apelearn]# git remote add origin git@github.com:yuanhh104/apelearn.git
[root@localhost_02 apelearn]# git push -u origin master #下次推送有一个报错
fatal: unable to access 'https://github.com/yuanhh104/apelearn.git/': Peer reports incompatible or unsupported protocol version.
[root@localhost_02 apelearn]# yum update -y nss curl libcurl #安装这三个包
[root@localhost_02 apelearn]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 205 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:yuanhh104/apelearn.git
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
4、可以再 /tmp/apearn/目录下再创建一个文件,并推送到远端试试下: 不要忘记添加到版本库,并推送到远端哟;
[root@localhost_02 apelearn]# git add 1.txt
[root@localhost_02 apelearn]# git commit -m "add 1.txt"
[master 758a592] add 1.txt
1 file changed, 2 insertions(+)
create mode 100644 1.txt
[root@localhost_02 apelearn]# git push
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:yuanhh104/apelearn.git
a23c110..758a592 master -> master
注释:此时再刷新浏览器界面,会发现这两个文件已经更新了;
5、克隆远程仓库,也就是说可以把远端主机的内容拉到本地了; git clone
##工作常用:以后可能会克隆远程的项目到本地;
首先再 github 上查找到这个URL地址,然后 git clone URL地址 如下:
然后在命令行下: clone git@github.com:yuanhh104/fenye.git
注释:即使没有配置公钥,也是可以克隆这个文件的;
[root@localhost_02 home]# git clone git@github.com:yuanhh104/apelearn.git
正克隆到 'apelearn'...
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 6 (delta 0), pack-reused 0
接收对象中: 100% (6/6), done.
[root@localhost_02 home]# ls
apelearn fenye
[root@localhost_02 home]# cd apelearn/
[root@localhost_02 apelearn]# ls
1.txt README.md
注释:这时候就把项目克隆完成了;
注释:加入这时候 远程 github 上的文件 1.txt 更新,而我在本地可以使用 git pull 把更新的文件拉下来;
git push 本地推送文件到远端 github
git pull 远端 github 拉文件 到本地
5、分支管理:
查看仓库里的分支: git branch 注释:下面的 星号表示当前所在的分支是哪一个
[root@localhost_02 apelearn]# git branch
* master
test
创建一个新的分支: git branch yuanhh
[root@localhost_02 apelearn]# git branch yuanhh #创建一个新的分支 yuanhh
[root@localhost_02 apelearn]# git branch #查看当前所在的分支是哪一个
* master
test
yuanhh
[root@localhost_02 apelearn]# git checkout yuanhh #切换到这个分支下
切换到分支 'yuanhh'
[root@localhost_02 apelearn]# git branch #查看当前所在分支是再yuanhh下
master
test
* yuanhh
创建一个文件 2.txt,并提交到 yuanhh , 然后切换到 master 分支; 发现分支之间是相互隔离开; 不收影响;
[root@localhost_02 apelearn]# git branch
master
test
* yuanhh
[root@localhost_02 apelearn]# ls
1.txt README.md
[root@localhost_02 apelearn]# vim 2.txt
[root@localhost_02 apelearn]# git add .
[root@localhost_02 apelearn]# git commit -m "add 2.txt"
[yuanhh d8dedab] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@localhost_02 apelearn]# git checkout master #切换到 mster 分支后;
切换到分支 'master'
[root@localhost_02 apelearn]# ls
1.txt README.md
那么这时候就涉及到了分支的合并, 注释: 合并分支时: merge 后面跟着分支名字一定是最新的分支
git checkout master #合并分支前,需要先切换到目标分支下;
git merge yuanhh #把yuanhh分支内容合并到 master 分支下;
[root@localhost_02 apelearn]# git checkout master #切换到master分支
已经位于 'master'
[root@localhost_02 apelearn]# git branch #查看当前所在分支
* master
test
yuanhh
[root@localhost_02 apelearn]# git merge yuanhh #合并yuanhh 到master
更新 758a592..d8dedab
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@localhost_02 apelearn]# ls
1.txt 2.txt README.md
分支冲突,也就是说 master 分支和 yuanhh 分支分别对2.txt 进行修改内容,不一致,当合并时会提示,当合并时会提示冲突,需要先解决冲突,才能进行合并;
[root@localhost_02 apelearn]# vim 2.txt #修改master 分支下的文件2.txt
[root@localhost_02 apelearn]# git add 2.txt
[root@localhost_02 apelearn]# git commit -m "ch 2.txt"
[master f988959] ch 2.txt
1 file changed, 1 insertion(+)
[root@localhost_02 apelearn]# git checkout yuanhh #修改 yuanhh 分支下的文件 2.txt
切换到分支 'yuanhh'
[root@localhost_02 apelearn]# vim 2.txt
[root@localhost_02 apelearn]# git add 2.txt
[root@localhost_02 apelearn]# git commit -m "ch 2.txt"
[yuanhh 2ed1e3a] ch 2.txt
1 file changed, 1 insertion(+)
[root@localhost_02 apelearn]# git checkout master #切换到mster 分支下
切换到分支 'master'
[root@localhost_02 apelearn]# git merge yuanhh #合并时会提示冲突
自动合并 2.txt
冲突(内容):合并冲突于 2.txt
自动合并失败,修正冲突然后提交修正的结果。
那么如何解决冲突,可以再master 分支下,编辑 2.txt,内容修改为和 yuanhh 分支里 2.txt 的内容相同,然后提交2.txt ,在合并 yuanhh 分支;
但是这样有一个问题,加入 master 分支下的内容是我们想要的呢,可以编辑2.txt,修改为想要的内容,然后提交;
切换到 yuanhh 分支下,然后合并 master 分支到 yuanhh 分支即可(倒着合并); git merge master
合并原则:那就是要把最新的分支合并到旧的分支; 也就是说 git merge 后面跟的要是最新的分支;
git commit -a 用来冲突时可以查看到一些提示信息
[root@localhost_02 apelearn]# vim 2.txt
[root@localhost_02 apelearn]# git checkout yuanhh #切换不过去 yuanhh 分支
2.txt: needs merge
error: 您需要先解决当前索引的冲突
[root@localhost_02 apelearn]# git commit -a
[master 293641b] Merge branch 'yuanhh'
[root@localhost_02 apelearn]# git add 2.txt
[root@localhost_02 apelearn]# git commit -m "ch 2.txt" #只能先提交
# 位于分支 master
无文件要提交,干净的工作区
[root@localhost_02 apelearn]# git merge yuanhh #合并 yuanhh 分支过来
Already up-to-date.
[root@localhost_02 apelearn]# git checkout yuanhh
切换到分支 'yuanhh'
[root@localhost_02 apelearn]# ls
1.txt 2.txt README.md
[root@localhost_02 apelearn]# cat 2.txt
111111111
333333333
如果master是我们想要的内容,这时候可以切换到 yuanhh 分支下,然后再次合并;
[root@localhost_02 apelearn]# git checkout yuanhh
切换到分支 'yuanhh'
[root@localhost_02 apelearn]# git merge master
更新 2ed1e3a..293641b
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
[root@localhost_02 apelearn]# ls
@ 1.txt 2.txt README.md
[root@localhost_02 apelearn]# cat 2.txt
111111111
222222222
333333333
###这时候可以修改为 yuanhh 分支下的 2.txt 为 master 分支下的内容
6、删除一个分支: git branch -d yuanhh # 删除这个分支时,一定要退出这个分支才可以;
git branch -D yuanhh 强制删除;
[root@localhost_02 apelearn]# git branch -d yuanhh
error: 无法删除您当前所在的分支 'yuanhh'。
[root@localhost_02 apelearn]# git checkout master
切换到分支 'master'
您的分支领先 'origin/master' 共 4 个提交。
(使用 "git push" 来发布您的本地提交)
[root@localhost_02 apelearn]# git branch -d yuanhh
已删除分支 yuanhh(曾为 293641b)。
[root@localhost_02 apelearn]# git branch
* master
test
7、使用分支的原则; master 时非常重要的分支,线上发布代码用这个分支,平时开发代码不要再这个分支上;
创建一个 dev 分支,专门用做开发,只有当发布到线上之前,才会把 dev 分支合并到 master,
应该在 dev 的基础上再分支成个人分支,个人分支再自己的pc上,里面开发代码,然后合并到 dev 分支;
git checkout dev
git merge master
8、远程分支:本地分支如果不推送到远程,对其他人是不可见的, 可以把远程的项目克隆到本地,然后操作后,再推送到远程上去;
查看所有分支:git ls-remote origin
[root@localhost_02 apelearn]# git ls-remote origin
ea940f12053a7daadbb88c3dcf1df206b68a0910 HEAD
ea940f12053a7daadbb88c3dcf1df206b68a0910 refs/heads/dev
ea940f12053a7daadbb88c3dcf1df206b68a0910 refs/heads/master
首先再远程创建一个 dev 分支,然后把 apalearn 克隆下来,发现只克隆下来了master 主分支, dev 分支没有克隆下来;
[root@localhost_02 tmp]# git clone git@github.com:yuanhh104/apelearn.git
正克隆到 'apelearn'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (5/5), done.
接收对象中: 100% (9/9), done.
处理 delta 中: 100% (1/1), done.
remote: Total 9 (delta 1), reused 5 (delta 0), pack-reused 0
[root@localhost_02 tmp]# cd /tmp/
[root@localhost_02 tmp]# cd apelearn/
[root@localhost_02 apelearn]# git branch #查询只有一个 master 分支了;
* master
当如果想把所有分支 dev 克隆下来,怎么弄; git checkout -b dev origin/dev
[root@localhost_02 apelearn]# git checkout -b dev origin/dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'
[root@localhost_02 apelearn]# git branch
* dev
master
在当前的 dev 分支下创建一个文件 2.txt, 然后推送并更新到远端 上;
[root@localhost_02 apelearn]# git branch
* dev
master
[root@localhost_02 apelearn]# ls
1.txt linux README.md
[root@localhost_02 apelearn]# vim 2.txt
[root@localhost_02 apelearn]# git add 2.txt
[root@localhost_02 apelearn]# git commit -m "ch 2.txt"
[dev f2c0cce] ch 2.txt
1 file changed, 2 insertions(+)
create mode 100644 2.txt
[root@localhost_02 apelearn]# git push
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 260 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:yuanhh104/apelearn.git
ea940f1..f2c0cce dev -> dev
注释:如果两个两个 分支都有变更,默认则是两个分支推送;
当如果我只想推送 dev 分支,后面可以加 git push origin dev
还有一个情况,如果本地分支 比 远程 分支多,会提示已经是最新的,不需要更新, 可以只更新其中一个分支: git push origin dev2
[root@localhost_02 apelearn]# git branch dev2 #创建分支
[root@localhost_02 apelearn]# git checkout dev2
切换到分支 'dev2'
[root@localhost_02 apelearn]# git branch
dev
* dev2
master
[root@localhost_02 apelearn]# vim 3.txt
[root@localhost_02 apelearn]# git add 3.txt
[root@localhost_02 apelearn]# git commit -m "add 3.txt"
[dev2 6fb2b06] add 3.txt
1 file changed, 1 insertion(+)
create mode 100644 3.txt
[root@localhost_02 apelearn]# git push origin dev2
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'dev2' on GitHub by visiting:
remote: https://github.com/yuanhh104/apelearn/pull/new/dev2
remote:
To git@github.com:yuanhh104/apelearn.git
* [new branch] dev2 -> dev2
这样远程旧可以更新了;这时候查看远程 github 上已经更新了新的分支了;
标签:标签类似与快照功能,可以给版本库打一个标签,记录某个时刻库的状态,也可以随时恢复到该状态;
标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态。也可以随时恢复到该状态。
git checkout master 先切到master分支上
git tag v1.0 给master打一个标签v1.0
git show v1.0 查看标签信息
git tag 可以查看所有的标签
tag是针对commit来打标签的,所以可以针对历史的commit来打tag
git log --pretty=oneline --abbrev-commit //先查看历史的commit
git tag v0.9 46d3c1a //针对历史commit打标签
git tag -a v0.8 -m "tag just v1.1 and so on" 5aacaf4 //可以对标签进行描述
git tag -d v0.8 //删除标签
git push origin v1.0 //推送指定标签到远程
git push --tag origin //推送所有标签
如果本地删除了一个标签,远程也想要删除需要这样操作:
git tag v1.0 -d //删除本地标签
git push origin :refs/tags/v1.0 //删除远程标签
git 别名
git commit 这个命令是不是有点长? 用别名可以提高我们的工作效率
git config --global alias.ci commit
git config --global alias.co checkout
git config --global alias.br branch
查看git别名使用命令
git config --list |grep alias
查询log小技巧:
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
取消别名
git config --global --unset alias.br
查看远程分支: git ls-remote origin