代码管理平台—git_gitlab_github
文章目录
一.单机上使用git
- 安装git
[[email protected] ~]# yum install -y git
- 建立版本仓库
[[email protected] ~]# mkdir /data/gitroot
- 初始化仓库
[[email protected] ~]# cd /data/gitroot/
[[email protected] gitroot]# git init
Initialized empty Git repository in /data/gitroot/.git/
- 添加邮箱和用户名,否则会报错
[[email protected] gitroot]# git config --global user.email "填写你的邮箱"
[[email protected] gitroot]# git config --global user.name "用户名"
刚刚定义的邮件和用户名存放在/root/.gitconfig
5. 仓库里面新建文件,并添加标签
添加标签的格式:git add 文件
[[email protected] gitroot]# echo 123 > 1.txt
[[email protected] gitroot]# git add 1.txt
-
提交添加标签后的文件:
格式:git commit -m "备注内容"
[[email protected] gitroot]# git commit -m "add 1.txt"
[master (root-commit) 13bfc36] add 1.txt
1 file changed, 1 insertion(+)
create mode 100644 1.txt
-
查看所有提交的记录
格式:git log
[[email protected] gitroot]# git log
commit d18ab1a3358273c49e48ba5ba2dc6fd82a7c73b2
Author: zhaoyujie <[email protected]>
Date: Thu Nov 22 22:20:43 2018 +0800
add 1.txt
commit 6762919fa7b24c0fde86d0765a337719ef15963f
Author: zhaoyujie <[email protected]>
Date: Thu Nov 22 22:18:40 2018 +0800
add 1.txt
-
单行显示所有提交的记录
格式:git log --pretty=oneline
[[email protected] gitroot]# git log --pretty=oneline
72683d59d575e27eaf18b3d7759ce5fc5d5b2744 ch 1.txt agin
13bfc36bf4465ebb339b5b9cf004ae00e8e1ce72 add 1.txt
第一列表示版本的IP号,用于版本的回滚 第二列表示版本的备注 版本越下越早
9. 回滚到某个版本:
格式:git reset --hard 版本IP号
[[email protected] gitroot]# git reset --hard 13bfc36
HEAD is now at 13bfc36 add 1.txt
-
查看历史版本记录:
格式:git reflog
[[email protected] gitroot]# git reflog
72683d5 [email protected]{0}: reset: moving to 72683d5
13bfc36 [email protected]{1}: reset: moving to 13bfc36
72683d5 [email protected]{2}: commit: ch 1.txt agin
bbf2058 [email protected]{3}: commit: ch 1.txt
13bfc36 [email protected]{4}: commit (initial): add 1.txt
[[email protected] gitroot]#
-
如果使用命令
rm -f
删除了1.txt,怎么恢复呢
从本地仓库中拉取文件
格式:git checkout -- 文件名
[[email protected] gitroot]# ls
1.txt
[[email protected] gitroot]# rm -f 1.txt
[[email protected] gitroot]# git checkout -- 1.txt
[[email protected] gitroot]# ls
1.txt
[[email protected] gitroot]#
-
如果文件修改,但没有提交。怎么回退到上一次的状态:
去掉add标签
格式:git reset HEAD 文件
[[email protected] gitroot]# cat 1.txt
123
shald
dcb
hahah
[[email protected] gitroot]# echo *** > 1.txt
[[email protected] gitroot]# git add 1.txt
[[email protected] gitroot]# git reset HEAD 1.txt
Unstaged changes after reset:
M 1.txt
[[email protected] gitroot]# git checkout -- 1.txt
[[email protected] gitroot]# cat 1.txt
123
shald
dcb
hahah
git rm
删除文件无法使用checkout恢复时
:
[[email protected] gitroot]# git rm 1.txt
rm '1.txt'
[[email protected] gitroot]# git commit -m "rm 1.txt"
[master 696db6e] rm 1.txt
1 file changed, 5 deletions(-)
delete mode 100644 1.txt
##此时的删除用checkout无法恢复,如下提示无法恢复:
[[email protected] gitroot]# git checkout -- 1.txt
error: pathspec '1.txt' did not match any file(s) known to git.
##此时可以使用,reset恢复:
[[email protected] gitroot]# git log --pretty=oneline
696db6e8bc327f14e08c97a317b6aac35c25ab63 rm 1.txt
72683d59d575e27eaf18b3d7759ce5fc5d5b2744 ch 1.txt agin
bbf2058fa98c7f74c53aa20d1e11bbe12d903c96 ch 1.txt
13bfc36bf4465ebb339b5b9cf004ae00e8e1ce72 add 1.txt
[[email protected] gitroot]# git reset --hard 72683
HEAD is now at 72683d5 ch 1.txt agin
[[email protected] gitroot]# ls
1.txt
[[email protected] gitroot]# cat 1.txt
123
shald
dcb
hahah
-
查看文件的变更
格式:git diff 文件
[[email protected] gitroot]# git diff 1.txt
diff --git a/1.txt b/1.txt
index c60a9ae..21f27d1 100644
--- a/1.txt
+++ b/1.txt
@@ -1,3 +1,4 @@
hello world
111111
22222222
+33333333
- git rm 与rm区别
用 git rm
来删除文件,同时还会将这个删除操作记录下来;该记录用于commit
来提交。
用 rm
来删除文件,仅仅是删除了物理文件,没有将其从 git 的记录中剔除。 需要在执行commit
的时候,多加一个-a参数。
[[email protected] gitroot]# rm -rf 1.txt
[[email protected] gitroot]# git commit -am "rm 1.txt"
二、建立远程仓库
更改使用的命令都是在本机上的,要想使用远程仓库来托管代码,可以去github的官网上注册帐号并建立仓库
官网 https://github.com/
-
注册了帐号后可以新建仓库,点右上角 —
new repository
-
填写其他仓库信息
-
添加**
点击头像 —-
Setting
—-SSH and GPG keys
—-New SSH key
- 然后在本机上使用ssh-******生成的公钥保存到github上,用于本机连接github时验证通讯
根据创建完仓库的提示输入命令并提交到GitHub仓库上
…or create a new repository on the command line
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin [email protected]:mkozhaozaici/test.git
git push -u origin master
[[email protected] tmp]# mkdir test
[[email protected] tmp]# cd test/
[[email protected] test]# echo "# test" > README.md
[[email protected] test]# git init
[[email protected] test]# git add README.md
[[email protected] test]# git commit -m "first commit"
[[email protected] test]# git remote add origin [email protected]:mkozhaozaici/test.git ##把本地仓库推送到远程仓库上
[[email protected] test]# git push -u origin master ##推送当前分支到远程上
此时查看远程仓库,就会看到刚刚创建的README.md的文件
- 把本机文件推送到远程上
格式:git push
[[email protected] test]# echo "hello world" > 1.txt
[[email protected] test]# git add 1.txt
[[email protected] test]# git commit -m "test"
[[email protected] test]# git push
第一次推送文件使用
git push -u origin master
此时回去远程仓库可以发现多了1.txt
三、克隆远程仓库
克隆远程仓库可以把整个远程仓库克隆到本机上。使用git clone
类似于svn
的 svn checkout
,先到远程仓库上点击Clone or download
复制远程仓库地址
- 在本机上克隆远程仓库
格式:git clone 远程仓库地址
[[email protected] ~]# git clone [email protected]:mkozhaozaici/test.git
-
在远程仓库上编辑代码文件
-
本机更新为远程仓库的最新版本,将远程仓库的文件拉取到本机。
格式:git pull
[[email protected] test]# git pull
[[email protected] test]# cat 1.txt
hello world
zyj
sxy
zzh
四、分支管理
当某个版本的代码已经上线了正在开发新功能时,发现了此版本有bug,为了不影响新功能的开发会使用分支来处理bug,再把处理bug的分支融合到主分支上(开发新功能)。分支之间是互不影响的。
- 查看分支
格式:git branch
[[email protected] gitroot]# git branch
* master
*号表示当前所在的分支
- 新建分支
格式:git branch 新分支名字
[[email protected] gitroot]# git branch test
[[email protected] gitroot]# git branch
* master
test
- 切换分支
格式:git checkout 切换的分支名
[[email protected] gitroot]# git checkout test
切换到分支 'test'
[[email protected] gitroot]# git branch
master
* test
- 分支之间是互不影响的,内容可以不一样
在test分支上新建文件
[[email protected] gitroot]# echo "aaaa" > 2.txt
[[email protected] gitroot]# ls
1.txt 2.txt
[[email protected] gitroot]# git add 2.txt ##添加新文件并提交
[[email protected] gitroot]# git commit -m "add 2.txt"
回去master分支上并没发现新建的文件
[[email protected] gitroot]# git checkout master
切换到分支 'master'
[[email protected] gitroot]# ls
1.txt
- 合并分支
格式:git merge 要合并的分支名
合并分支前提是先切换到目标分支(旧分支)上 ,在把合并的分支(新编辑分支)合并到目标分支上
[[email protected] gitroot]# git checkout master
切换到分支 'master'
[[email protected] gitroot]# git merge test ##把test分支合并到master分支上
[[email protected] gitroot]# ls
1.txt 2.txt
合并分支只是合并内容,分支本身不会消失。
- 分支冲突
如果master分支和test分支都对2.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。
解决冲突的方法是在master分支下,编辑2.txt,改为test分支里面2.txt的内容。然后提交2.txt,再合并test分支。
但是这样有一个问题,万一master分支更改的内容是我们想要的呢?
可以编辑2.txt内容,改为想要的,然后提交。切换到test分支,然后合并master分支到test分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。
- 删除分支
格式:git branch -d 要删除的分支
[[email protected] gitroot]# git branch -d test
- 强制删除分支
当分支没有合并,删除之前会提示,那就不合并,可以强制删除
格式:git branch -D 要删除的分支
[[email protected] gitroot]# git branch -D test
- 分支的原则
对于分支的应用,建议大家以这样的原则来:
o master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
o 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master
o 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到dev分支
例如dev分支合并bob分支的命令是:
git checkout dev //先切换到dev分支
git merge bob //合并bob分支到dev
五、 远程分支管理
在远程仓库点Branch:master可以创建新的分支
但到了本地要克隆仓库时
默认只把master分支克隆下来,如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支git pull
- 克隆远程分支到本机
格式:git checkout -b 本地分支名 origin/远程分支名
[[email protected] test]# git checkout -b dev origin/dev
本地和远程分支的名称要一致
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 ‘dev’
- 查看远程分支情况
格式:git ls-remote origin
[[email protected] test]# git ls-remote origin
586eeea82f05a0ee598d344ee127eb5ba7c38b40 HEAD
d989d94cc5989bfa6c806022d33012e1f84cdcd4 refs/heads/dev
586eeea82f05a0ee598d344ee127eb5ba7c38b40 refs/heads/master
- 推送本地分支到远程上
格式:git push origin 本地分支名
- 对于推送分支有两种情况:
- 当本地分支和远程分支一致时,git push会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用
git push origin branch-name
[[email protected] test]# git push origin dev
##几个分支都有更改,只指定推送dev分支到远程上
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 318 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:zyj0769/test.git
83a8cc2..2c3ba25 dev -> dev
- 当本地分支比远程分支多,git push只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用
git push origin branch-name
[[email protected] test]# git push origin dev2 ##远程上没有dev2 这里推送本地dev2上去
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 249 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 [email protected]:zyj0769/test.git
* [new branch] dev2 -> dev2
远程机器上多了dev2分支
六、标签管理
标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态。也可以随时恢复到该状态。一般打标签都是给master打标签。
- 新建标签
格式:git tag 标签名
[[email protected] test]# git checkout master ##先切换到master分支上
切换到分支 'master'
[[email protected] test]# git tag v1.0
- 查看所有标签
格式:git tag
[[email protected] test]# git tag
v1.0
- 查看标签具体信息
格式:git show 标签名
[[email protected] test]# git show v1.0
commit 292e13521e6726a733b3548748639d1d422ce8dd
Author: zyj <[email protected]>
Date: Fri Aug 31 07:35:53 2018 +0800
add 2.txt
diff --git a/2.txt b/2.txt
new file mode 100644
index 0000000..d72af31
--- /dev/null
+++ b/2.txt
@@ -0,0 +1 @@
+asd
- tag是针对commit来打标签的,所以可以针对历史的commit来打tag
先查看所有提交的记录
[[email protected] test]# git log --pretty=oneline
292e13521e6726a733b3548748639d1d422ce8dd add 2.txt
586eeea82f05a0ee598d344ee127eb5ba7c38b40 test
74adc57a965e35c23e673d059280b7d509f20713 first commit
或者
[[email protected] test]# git log --pretty=oneline --abbrev-commit ##简写记录ID号
292e135 add 2.txt
586eeea test
74adc57 first commit
-
针对commit打标签
格式:git tag 标签名 记录ID号
1 [[email protected] test]# git tag v0.9 586eeea -
针对commit打标签并描述
格式:git tag -a 标签名 -m "描述内容" 记录ID号
[[email protected] test]# git tag -a v0.1 -m "1st" 74adc57a
[[email protected] test]# git show v0.1 ##查看标签v0.1的信息
tag v0.1
Tagger: zyj <[email protected]>
Date: Fri Aug 31 20:51:22 2018 +0800
1st
commit 74adc57a965e35c23e673d059280b7d509f20713
- 删除本地标签
格式:git tag -d 标签名
[[email protected] test]# git tag -d v0.1
已删除 tag 'v0.1'(曾为 1a4e9c9)
- 推送指定标签到远程
格式:git push origin 标签名
[[email protected] test]# git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:zyj0769/test.git
* [new tag] v1.0 -> v1.0
可以发现远程仓库上已经有了更改推送的标签
- 推送所有标签到远程
格式:git push --tag origin
[[email protected] test]# git push --tag origin
Counting objects: 1, done.
Writing objects: 100% (1/1), 146 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To [email protected]:zyj0769/test.git
* [new tag] v0.1 -> v0.1
* [new tag] v0.9 -> v0.9
远程仓库也更新了其他标签
- 删除远程的标签
格式:git push origin :refs/tags/标签名
[[email protected] test]# git tag -d v0.1 ##删除本地标签
已删除 tag 'v0.1'(曾为 f6f2e61)
[[email protected] test]# git push origin :refs/tags/v0.1
To [email protected]:zyj0769/test.git
- [deleted] v0.1
远程仓库上已经没有了v0.1的标签了
七、git别名
-
git上可以用别名来提高我们的工作效率
-
创建别名
格式:git config --global alias.别名 用命令名
[[email protected] test]# git config --global alias.ci commit ##ci是commit别名
[[email protected] test]# echo "1a" > 3.txt
[[email protected] test]# git add 3.txt
[[email protected] test]# git ci -m "add 3.txt"
[master a9e376d] add 3.txt
1 file changed, 1 insertion(+)
create mode 100644 3.txt
- 查看所有别名
格式:gif config --list |grep alias
[[email protected] test]# git config --list |grep alias
alias.ci=commit
alias.br=branch
- 别名的配置在目录/root/.gitconfig
[[email protected] test]# cat /root/.gitconfig
[user]
email = [email protected]
name = zyj
[alias]
ci = commit
br = branch
- 取消别名
格式:git config --global --unset alias.别名
[[email protected] test]# git config --global --unset alias.br
- 小技巧
使用颜色和别名git lg来替代git log
[[email protected] test]# 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"
[[email protected] test]# git lg
* a9e376d - (HEAD, master) add 3.txt (11 分钟之前) <zyj>
* 292e135 - (tag: v1.0, origin/master, origin/HEAD) add 2.txt (15 小时之前) <zyj>
* 586eeea - (tag: v0.9) test (2 天之前) <zyj>
* 74adc57 - first commit (2 天之前) <zyj>
八、搭建git服务器
- github毕竟是公开的,而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的,只自己公司使用的。可以使用Gitlab,也可以自己搭建一个命令行的git服务器。
步骤
- 在服务端上
- yum安装git
[[email protected] ~]# yum install -y git
- 创建git用户
[[email protected] ~]# useradd -s /usr/bin/git-shell git
设置/usr/bin/git-shell,目的是为了不让git用户远程登陆
- 进入git用户目录中创建存放公钥的目录和文件并设置权限
[[email protected] ~]# cd /home/git/
[[email protected] git]# mkdir .ssh
[[email protected] git]# touch .ssh/authorized_keys
[[email protected] git]# chmod -R 600 .ssh/authorized_keys
[[email protected] git]# chown -R git:git .ssh/
- 把客户端的公钥存放在服务端的
/authorized_keys
上
[[email protected] ~]# ssh [email protected]
##远程登录服务端,显示不允许登录,但实际上已经可以和服务端通讯
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.80.102 closed.
- 创建存储git的仓库
[[email protected] git]# mkdir /data/gitroot
- 在该仓库下创建裸仓库
[email protected] gitroot]# git init --bare sample.git ##创建裸仓库sample
仓库一般用.git结尾
初始化空的 Git 版本库于 /data/gitroot/sample.git/
- 更改裸仓库的属组
[email protected] gitroot]# chown -R git:git sample.git/
- 在客户端上
平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时操作都是在我们自己的pc上做的
- 克隆服务端的仓库到本地
[[email protected] ~]# git clone [email protected]:/data/gitroot/sample.git
正克隆到 'sample'...
warning: 您似乎克隆了一个空版本库。
- 新建文件并推送到远程服务端
[[email protected] sample]# echo "aaa" > 1.txt
[[email protected] sample]# git add 1.txt
[[email protected] sample]# git commit -m "add 1.txt"
[[email protected] sample]# git push origin master ##指定默认托送master分支
九、安装gitlab
-
一般公司都是使用gitlab来托管代码的。官网 https://about.gitlab.com/
-
我们可以把代码托管在他的平台上,也可以在服务器上使用gitlab搭建自己平台
-
官网要求服务器内存不少于4G
-
国内镜像安装方法 https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/
步骤
- 使用国内镜像安装
- 创建gitlab-ce.repo
[[email protected] ~]# vim /etc/yum.repos.d/gitlab-ce.repo
添加下面代码
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
- 安装makecache和gitlab-ce
[[email protected] ~]# yum makecache
[[email protected] ~]# yum install -y gitlab-ce
3. 自动加载配置
[[email protected] ~]# gitlab-ctl reconfigure
- gitlab服务操作
格式:gitlab-ctl stop/restart/start/status
[email protected] ~]# gitlab-ctl status
run: alertmanager: (pid 31211) 39s; run: log: (pid 23701) 3848s
run: gitaly: (pid 31219) 39s; run: log: (pid 23647) 3851s
run: gitlab-monitor: (pid 31229) 39s; run: log: (pid 23666) 3850s
run: gitlab-workhorse: (pid 31232) 38s; run: log: (pid 23628) 3851s
run: logrotate: (pid 31238) 38s; run: log: (pid 23670) 3850s
run: nginx: (pid 31244) 37s; run: log: (pid 23627) 3851s
run: node-exporter: (pid 31248) 37s; run: log: (pid 23646) 3851s
run: postgres-exporter: (pid 31255) 36s; run: log: (pid 23714) 3848s
run: postgresql: (pid 31260) 36s; run: log: (pid 23608) 3852s
run: prometheus: (pid 31270) 35s; run: log: (pid 23680) 3850s
run: redis: (pid 31275) 35s; run: log: (pid 23609) 3852s
run: redis-exporter: (pid 31280) 34s; run: log: (pid 23665) 3850s
run: sidekiq: (pid 31282) 34s; run: log: (pid 23607) 3852s
run: unicorn: (pid 31290) 34s; run: log: (pid 23606) 3852s
-
浏览器上访问192.168.80.108来访问gitlab,管理员用户root,第一次登录需要我们填写密码
-
输入root 新密码后就可以进入gitlab页面
-
gitlab命令, 参考: https://www.cnyunwei.cc/archives/1204
十、使用gitlab
- 一般公司都是使用域名来登录gitlab,可以使用带公网ip的Nginx来代理内网的gitlab服务器。因为安装gitlab的时候就已经自动安装了Nginx,Nginx的配置路径在
/var/opt/gitlab/nginx/conf/
[[email protected] ~]# ls /var/opt/gitlab/nginx/conf/
gitlab-http.conf nginx.conf nginx-status.conf
nginx.conf是主配置文件 gitlab-http.conf是gitlab的配置文件,可以更改监听端口和域名
- 添加**
点击头像 —- Settings —- SSH Kyes
- 添加组
点击小扳手 —- New group
- 添加用户
点击小扳手 —- New user
- 这里会有连接给你输入的邮箱,但邮箱暂时不能使用 等创建完用户后点击
Edit
来修改密码
-
当新用户第一次登录时候会提示要你重新修改密码
-
添加仓库
点击小扳手 —- New project
十一、gitlab备份和恢复
-
gitlab的备份是在线备份的,不需要停止服务。gitlab的备份是不能跨版本的,例如9的备份数据不能恢复到10版本上。
-
gitlab备份
格式:·gitlab-rake gitlab:backup:create`
[[email protected] ~]# gitlab-rake gitlab:backup:create
备份的文件默认在/var/opt/gitlab/backups/
[[email protected] ~]# ls /var/opt/gitlab/backups/
1535869554_2018_09_02_11.2.3_gitlab_backup.tar
- gitlab备份恢复
格式:·gitlab-rake gitlab:backup:restore BACKUP=备份数据的前缀(日期版本)·
- 停止unicorn服务和sidekiq服务
[[email protected] ~]# gitlab-ctl stop unicorn
[[email protected] ~]# gitlab-ctl stop sidekiq
- 恢复数据
[[email protected] ~]# gitlab-rake gitlab:backup:restore BACKUP=1535869554_2018_09_02_11.2.3
- 重启gitlab
[[email protected] ~]# gitlab-ctl start