一、大纲

二、简介
2.1 简史

2.2 仓库结构

- 工作区:workspace。
- 暂存区:英文名:stage或index。一般存放在.git/index中,所以暂存区也叫作索引(index)。
- 版本库:工作区有一个隐藏目录.git,不算工作区,而是Git的版本库。
补充:
- .git/objects,包含创建的各种对象及内容。
三、安装
3.1 Gitlab安装
3.1.1 环境准备
- 打开HTTP和SSH防火墙
yuminstall-y curl policycoreutils-python openssh-server cronie
lokkit-s http -s ssh - 电子邮件安装
yuminstallpostfix
servicepostfix start
chkconfigpostfix on - yum镜像仓库
touch/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
3.1.2 安装
- 安装
yummakecache
yuminstallgitlab-ce - 配置
vim/etc/gitlab/gitlab.rbexternal_url 'http://ioceye.com'gitlab-ctlreconfigure # 更新配置 - 启动
gitlab-ctlstart
3.2 Git签名配置
## 01. 系统级配置
## 配置文件路径为:/etc/gitconfig
git config --system user.name ioteye
git config --system user.email ioteye.szu@hotmail.com
## 02. 用户级配置
## 配置文件路径为:~/.gitconfig
git config --global user.name ioteye
git config --global user.email ioteye.szu@hotmail.com
## 03. 项目级配置
## 配置文件路径为:.git/config
git config user.name ioteye
git config user.email ioteye.szu@hotmail.com
## 04. 查看配置信息
git config --list
## 注意:
## 优先级:项目级 > 用户级 > 系统级
四、仓库管理

五、本地仓库管理
5.1 常用命令
## 1.在本地目录中创建一个Git本地仓库
git init
## 2. 查看工作区、暂存区状态
git status
## 3. 将工作区"新建、修改"文件添加到暂存区
git add
## 4. 将暂存区的文件提交到本地库
git commit -m 'commit comment" <file>
## 5. 查看历史记录
git log
git log --pretty=oneline
git log --oneline
git reflog
## 6. 本地仓库版本前进和后退
git reset
## 7. 比较文件差异
git diff
## 8. 分支管理
git branch
git checkout
## 9. 切换到指定分区
## git checkout -b 本地分支名 远程分支名
git checkout -b dev origin/dev
## 10. 分支合并
git merge
5.2 本地仓库初始化
## 01. 在目录中创建新的Git仓库。
$ git init
Reinitialized existing Git repository in d:/project/git-demo/.git # 重新初始化现有的Git存储库
## 02. 查看工作区和暂存区的状态
$ git status
On branch master (分支为:master)
Initial commit (初次提交)
nothing to commit (create/copy files and use "git add" to track)
# 无需提交(创建/复制文件并使用"git add"进行跟踪,追踪也就是让Git进行管理的意思)
5.3 空文件提交
## 01. 初始化本地仓库
$ git init
## 02. 创建文件
$ touch readme.md
## 03. 查看工作区和暂存区的状态
$ git status
On branch master(分支为:master)
Initial commit(初次提交)
Untracked files(未跟踪的文件):
(use "git add <file>..." to include in what will be committed)
(使用"git add <file> ..."将内容添加到暂存区)
readme.md
nothing added to commit but untracked files present (use "git add" to track)
# 没有添加任何提交的内容,但存在未跟踪的文件(使用"git add"进行跟踪)
## 04. 添加所有文件到暂存区
$ git add .
## 05. git add <file>...
## 或者使用命令添加文件到暂存区
$ git add readme.md
## 06. 查看工作区和暂存区状态
$ git status
On branch master(分支为:master)
Initial commit(初次提交)
Changes to be committed(所做的更改):
(use "git rm --cached <file>..." to unstage) # (使用"git rm --cache <file>..."将文件从暂存区移除到工作区)
new file: readme.md
## 07. git rm --cached <file>...
## 将文件从暂存区移除到工作区
$ git rm --cached readme.md
rm 'readme.md'
## 08. 添加文件到暂存区
$ git add readme.md
## 10. git commit -m 'commit comment'
## 提交文件到本地仓库
$ git commit -m 'initial commit'
## 11. git commit -m 'commit comment' <file>
## 将暂存区或者工作区的文件提交到Git本地仓库,目标文件只能是修改内容的文件,不支持初次提交的文件
$ git commit -m 'initial commit'
[master (root-commit) 08d94ea] initial commit
# master为分支,初次提交为"root-commit"(即根提交),08d94ea为提交md5号前7位,"initial commit"为提交信息
1 file changed, 0 insertions(+), 0 deletions(-) # 1个文件改变,0行插入,0行删除
create mode 100644 readme.md # 本地仓库创建readme.md文件模式为100644
5.4 内容文件提交
## 01. 初始化本地仓库
$ git init
## 02. 新建文件
$ touch newfile.md
$ echo 'first' > newfile.md
## 03. 添加文件到暂存区
$ git add .
warning: LF will be replaced by CRLF in newfile.md. # 警告:newfile.md中的LF将被为CRLF。
The file will have its original line endings in your working directory. # 在工作区中,该文件具有原始文件转行符(LF)。
## 04. 将暂存区的文件提交到本地仓库
$ git commit -m 'commit newfile to repostory' newfile.md
[master 6664579] add newfile
1 file changed, 1 insertion(+)
create mode 100644 newfile.md
## 05. 直接将工作区的文件提交到本地仓库
$ echo first >> newfile.md
$ git commit -m 'commit second' newfile.md
warning: LF will be replaced by CRLF in newfile.md. # 警告:newfile.md中的LF将被为CRLF。
The file will have its original line endings in your working directory. # 在工作区中,该文件具有原始文件转行符(LF)。
[master fb48a82] commit fourth
1 file changed, 1 insertion(+)
5.5 查看提交日志
$ git init
## 描述:文件提交内容如下,每次提交一行内容
## first -> -m 'commit first'
## second -> -m 'commit second'
## third -> -m 'commit third'
## fourth -> -m 'commit fourth'
## 01. 查看文件提交日志
$ git log
commit fb48a826d39fb6ed274072d11aebc7e76e1a12f1 (HEAD -> master) # fb48a82...:版本索引值,(指针 -> master版本)
Author: ioteye <ioteye.szu@hotmail.com> #(作者:用户 <邮箱>)
Date: Sat May 30 16:05:20 2020 +0800 #日期:时间
commit fourth #(提交信息)
. . .
commit 100187fb9744eac1100207f159689927c59a0ef3
Author: ioteye <ioteye.szu@hotmail.com>
Date: Sat May 30 16:02:06 2020 +0800
initial commit
## 02. 查看日志简洁模式
$ git log --pretty=oneline
fb48a826d39fb6ed274072d11aebc7e76e1a12f1 (HEAD -> master) commit fourth
1f9c189dbca3c09d95d17df9c96d14dc3398437f commit third
8d8bd3e737bf1327b669d58b12de7132ad537b58 commit second
ecc7a588a13ab6e032078776a1a0f2d421fc7d65 commit first
100187fb9744eac1100207f159689927c59a0ef3 initial commit
## 03. 查看日志简洁模式
$ git log --oneline
fb48a82 (HEAD -> master) commit fourth
fb48a82:是提交到本地仓库MD5的前7位,即局部索引值
1f9c189 commit third
8d8bd3e commit second
ecc7a58 commit first
100187f initial commit
## 04. 查看日志简洁模式
$ git reflog
fb48a82 (HEAD -> master) HEAD@{0}: commit: commit fourth
HEAD@{0}:表示移动到版本需要的步数
1f9c189 HEAD@{1}: commit: commit third
8d8bd3e HEAD@{2}: commit: commit second
ecc7a58 HEAD@{3}: commit: commit first
100187f HEAD@{4}: commit (initial): initial commit
5.6 版本回退和前进
## 01. 查看版本指针日志文件
$ git reflog
fb48a82 (HEAD -> master) HEAD@{0}: commit: commit fourth
1f9c189 HEAD@{1}: commit: commit third
8d8bd3e HEAD@{2}: commit: commit second
ecc7a58 HEAD@{3}: commit: commit first
100187f HEAD@{4}: commit (initial): initial commit
## 02. 版本回退到"commit second"
$ git reset --hard 8d8bd3e
HEAD is now at 8d8bd3e commit second
## 03. 查看版本指针日志文件
$ git reflog
8d8bd3e (HEAD -> master) HEAD@{0}: reset: moving to 8d8bd3e #(指针指向:8d8bd3e)
fb48a82 HEAD@{1}: commit: commit fourth
1f9c189 HEAD@{2}: commit: commit third
8d8bd3e (HEAD -> master) HEAD@{3}: commit: commit second #(指针位置)
ecc7a58 HEAD@{4}: commit: commit first
100187f HEAD@{5}: commit (initial): initial commit
$ cat readme.md
first
second
## 04.后退一步
$ git reset --hard HEAD^
HEAD is now at ecc7a58 commit first
$ git reflog
ecc7a58 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
8d8bd3e HEAD@{1}: reset: moving to 8d8bd3e
8d8bd3e HEAD@{2}: reset: moving to 8d8bd3e
fb48a82 HEAD@{3}: commit: commit fourth
1f9c189 HEAD@{4}: commit: commit third
8d8bd3e HEAD@{5}: commit: commit second
ecc7a58 (HEAD -> master) HEAD@{6}: commit: commit first #(指针位置)
100187f HEAD@{7}: commit (initial): initial commit
## 05. 后退n步
$ git reset --hard HEAD~n
5.7 比较文件差异
# 01. 将工作区和暂存区的文件进行比较
$ git diff <file>
# 02. 将工作区中的文件和本地库中的文件进行比较
$ git diff <local repostory version> <file>
$ git reflog
ecc7a58 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
8d8bd3e HEAD@{1}: reset: moving to 8d8bd3e
8d8bd3e HEAD@{2}: reset: moving to 8d8bd3e
fb48a82 HEAD@{3}: commit: commit fourth
1f9c189 HEAD@{4}: commit: commit third
8d8bd3e HEAD@{5}: commit: commit second
ecc7a58 (HEAD -> master) HEAD@{6}: commit: commit first
100187f HEAD@{7}: commit (initial): initial commit
$ git diff fb48a82 readme.md
diff --git a/readme.md b/readme.md
index cf59613..9c59e24 100644
--- a/readme.md
+++ b/readme.md
@@ -1,4 +1 @@ #(-1,4中,-表示第一个文件,1~4表示:从第1行到第4行减少。+1表示第二个文件,后面没操作)
first
-second
-third
-fourth
5.8 分支管理
## 01. 创建分支
$ git branch [branch-name]
## 02. 查看分支
$ git branch -v
## 03. 切换分支
$ git branch [branch-name]
## 04. 删除本地仓库分支
$ git branch -d [branch-name]
## 05. 删除远程仓库分支
$ git push origin --delete [branch-name]
5.9 合并分支和解决冲突
## 01.切换到接受修改的分支上
$ git checkout [branch-one]
## 02.执行合并,将[branch-two]分支的内容合并到[branch-one]
$ git merge [branch-two]
## 03. 解决冲突,冲突的表现
1. first
2. <<<<<<< HEAD
3. first edit # 注释:当前分支的内容
4. =======
5. second edit # 注释:冲突的分支内容
6. >>>>>>> master
7. end
## 步骤:
## 1. 修改文件
## 2. git add <file>
## 3. git commit -m 'edit content'
## 注意:此时commit不能带任何文件名
六、团队协作
## 01. 创建远程Github仓库
git@github.com:lolog/hadoop.git
## 02. 创建远程仓库
## 03. 邀请成员加入
## 04. 创建远程仓库别名
$ git remote add [远程仓库别名] [远程仓库地址]
## 05. 移除远程仓库别名
$ git remote remove [远程仓库别名]
## 06. 查看当前所有远程地址别名
$ git remote -v
## 07. 克隆
$ git clone [远程地址]
$ git clone -b [指定分支名] [远程地址]
$ git clone -b [指定分支名] [远程仓库别名]
## 08. 拉取
## 注释:
## pull = fetch + merge
## git fetch [远程仓库别名] [本地分支名称]
## git merge [远程仓库别名] [本地分支名称]
## git pull [远程仓库别名] [本地分支名称]
## 09. 推送
$ git push [远程仓库别名] [本地分支名称]
## 10. 关联推送
## 如果本地分支没有关联到远程分支, 则关联之后再push
## git push --set-upstream [远程仓库别名] [本地分支名称]
七、跨团队协作
## 01. Github fork
## 02. 完成任务
## 03. 推送到远程:new pull request
八、标签
8.1 附注标签
附注标签是存储在Git数据库中的一个完整对象, 它们是可以被校验的,其中包含打标签者的名字、电子邮件地址、日期时间, 此外还有一个标签信息,并且可以使用 GNU Privacy Guard (GPG)签名并验证。 通常会建议创建附注标签,这样你可以拥有以上所有信息。但是如果你只是想用一个临时的标签, 或者因为某些原因不想要保存这些信息,那么也可以用轻量标签。
## 01. 列出标签
$ git tag
$ git tag <-l partern>/<--list partern>
## 02. 添加附注标签
$ git tag -a tagname -m "tag comment"
## 03. 查看标签信息和对应的提交信息
$ git show tagname
## 04. 推送标签到远程仓库
$ git push origin tagname
## 05. 推送多个标签到远程仓库
$ git push origin --tags
## 06. 删除本地仓库标签
$ git tag -d tagname
## 06. 删除远程仓库标签,需要删除本地仓库标签之后,才能删除远程仓库标签
$ git tag -d tagname
$ git push origin :refs/tags/tagname
8.2 轻量级标签
轻量标签本质上是将提交校验和存储到一个文件中——没有保存任何其他信息。
## 01. 添加轻量级标签
$ git tag tagname
## 02. 查看标签信息和对应的提交信息
$ git show tagname
九、Gitflow工作流程
9.1 Gitflow常用的分支
- Master分支
跟随者仓库一起创建,有且只有一个
Master分支上的代码总是稳定的,随时可以发布出去
一般不在Master分支上操作,当Release和Hotfix分支合并代码到Master分支上时,Master上代码才更新 - Develop 分支
有且只有一个,用于新特性的开发,但不直接在Develop分支上开发,新特性开发是在Feature分支上进行
当Develop分支上的特性足够多以至于可以进行新版本的发布时,可以创建一个对应的Release分支作为发布
Develop是主开发分支,包含所有要发布到下一个Release的代码,该分支主要用于Feature分支合并 - Feature 分支
主要是用来开发一个新的特性,一旦开发完成合并回Develop分支,以及进入下一个Release - Release分支
当需要发布一个新Release的时候,基于Develop分支创建一个Release分支,完成Release后,合并回Master和Develop分支 - Hotfix分支
当在Production发现新的Bug时候,需要创建一个Hotfix,完成Hotfix后,合并回Master和Develop分支,所以Hotfix的改动会进入下一个Release
9.2 初始分支
所有在Master分支上的Commit应该打上Tag。

9.3 Feature分支
Feature分支开发完成后,必须合并回Develop分支, 合并完分支后一般会删这个Feature分支,但是也可以保留。

9.4 Release分支
Release分支基于Develop分支创建,可以在Release分支上测试,修改Bug等。同时,其它开发人员可以基于开发新的Feature (记住:一旦打了Release分支之后,不要从Develop分支上合并新的改动到Release分支)
发布Release分支时,合并Release到Master和Develop, 同时在Master分支上打个Tag记住Release版本号,然后可以删除Release分支了。

9.5 维护分支Hotfix
Hotfix分支基于Master分支创建,开发完后需要合并回Master和Develop分支,同时在Master上打一个Tag。

十、补充
10.1 reset命令
## 01. 仅仅移动Git本地仓库的指针位置
$ git rest --soft
## 02. 移动Git本地仓库的指针位置,以及重置暂存区到Git本地仓库指针位置
$ git rest --mixed
## 03. 移动Git本地仓库的指针位置,以及重置暂存区和工作区到Git本地仓库指针位置
$ git rest --hard
468

被折叠的 条评论
为什么被折叠?



