docs(git): add base

This commit is contained in:
zjkjzj
2021-03-18 17:06:57 +08:00
parent b37ba05b53
commit 967b4b575d
12 changed files with 764 additions and 1 deletions

133
docs/git/base/git-config.md Normal file
View File

@@ -0,0 +1,133 @@
# [git config]配置
## 命令行操作范围
使用命令`git config`进行配置,各级别设置需要加上可选参数
# 系统级别
git config --system ...
# 全局级别
git config --global ...
# 仓库级别
git config --local ...
### 打印配置信息
* 获取配置信息
```
$ git config --list
user.name=xxx
user.email=xxx@163.com
core.editor=code
http.postbuffer=1048576000
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:ZJDoc/GitGuide.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
```
* 获取全局配置信息
```
$ git config --global --list
user.name=xxx
user.email=xxx@163.com
core.editor=code
http.postbuffer=1048576000
```
* 获取本地配置信息
```
$ git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:ZJDoc/GitGuide.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
```
**注意:需要进入一个`Git`仓库才能获取本地信息**
## 常用设置
1. 设置用户信息
2. 设置文本编辑器
3. 移除变量
### 设置用户信息
每次提交时都需加上用户名和邮箱地址,可以设置成全局变量
git config --global user.name user-name
git config --global user.email email-address
*设置节点为`user`,设置属性为`name`和`email`*
也可以在仓库中设置本地用户名和用户邮箱:
```
$ git config --local user.name xxx
$ git config --local user.email xxx
```
### 设置文本编辑器
`git`使用系统默认的编辑器,可以设置指定编辑器
# 未设置
git config --global core.editor vim
# 已设置,先删除再添加
git config --global --unset core.editor
git config --global core.editor vim
### 移除变量
# 移除单个变量
git config --global --unset 属性名
# 移除所有变量
git config --global --unset-all 属性名
## 配置文件地址
`git``3`个级别的配置文件:
1. `local`:本地文件存放在仓库中(`.git/config`),只针对当前仓库
2. `global`:全局文件存放为`~/.gitconfig``~/.config/git/config`,作用于当前用户的所有仓库
3. `system`:系统文件存放为`/etc/gitconfig`,作用于系统的每个用户
其优先级为`local > global > system`
*`windows`环境下的全局配置文件存放为`C:\\User\\$USER\\.gitconfig`*
### 编写格式
所有配置文件的编写格式都一样,可参考仓库级别的`config`文件,按照*section和key*进行配置
# 仓库级别
$ cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:zjZSTU/linux-guide.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
# 全局级别
$ cat .gitconfig
[user]
name = zxxx
email = 50xxxxx.com

30
docs/git/base/git-log.md Normal file
View File

@@ -0,0 +1,30 @@
# [git log]日志查询
使用命令`git log`查询日志,包含了多种快捷查询方式
1. 最简单查询
```
$ git log
```
2. 单行输出
```
$ git log --oneline
```
3. 指定关键字查询
```
$ git log --grep 关键字
```
4. 指定作者查询
```
$ git log --author 作者
```
**注意:上述属性可以组合使用,比如指定关键字和作者并单行输出**

10
docs/git/base/gitkeep.md Normal file
View File

@@ -0,0 +1,10 @@
# .gitkeep
文件`.gitkeep`是一个占位符
>git无法追踪一个空的文件夹当用户需要追踪(track)一个空的文件夹的时候,按照惯例,大家会把一个称为.gitkeep的文件放在这些文件夹里。
## 相关阅读
* [.gitkeep](https://www.jianshu.com/p/2507dfdb35d8)

View File

@@ -0,0 +1,75 @@
# [ssh][http]传输方式切换
通过修改本地文件,切换代码传输方式
## 命令修改
查看当前使用`SSH`还是`HTTP`
$ git remote -v
origin git@gitee.com:zjZSTU/zjzstu.gitee.io.git (fetch)
origin git@gitee.com:zjZSTU/zjzstu.gitee.io.git (push)
当前使用`SSH`协议进行代码拉取和推送
方式一:仅修改拉取协议为`HTTP`
$ git remote set-url --push origin https://gitee.com/zjZSTU/zjzstu.gitee.io.git
$ git remote -v
origin git@gitee.com:zjZSTU/zjzstu.gitee.io.git (fetch)
origin https://gitee.com/zjZSTU/zjzstu.gitee.io.git (push)
方式二:同时修改拉取和推送协议为`HTTP`
# 先增加HTTP
$ git remote set-url --add origin https://gitee.com/zjZSTU/zjzstu.gitee.io.git
$ git remote -v
origin git@gitee.com:zjZSTU/zjzstu.gitee.io.git (fetch)
origin https://gitee.com/zjZSTU/zjzstu.gitee.io.git (push)
# 再删除SSH
$ git remote set-url --delete origin git@gitee.com:zjZSTU/zjzstu.gitee.io.git
$ git remote -v
origin https://gitee.com/zjZSTU/zjzstu.gitee.io.git (fetch)
origin https://gitee.com/zjZSTU/zjzstu.gitee.io.git (push)
## 文件修改
修改文件`.git/config``remote`小节
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@gitee.com:zjZSTU/zjzstu.gitee.io.git
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "dev"]
remote = origin
merge = refs/heads/dev
方式一:仅修改拉取协议为`HTTP`
添加`pushurl = ...``remote`小节
...
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@gitee.com:zjZSTU/zjzstu.gitee.io.git
pushurl = https://gitee.com/zjZSTU/zjzstu.gitee.io.git
[branch "master"]
...
方式二:同时修改拉取和推送协议为`HTTP`
修改`url``HTTP`即可
...
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://gitee.com/zjZSTU/zjzstu.gitee.io.git
...

View File

@@ -0,0 +1,40 @@
# 关于版本控制
## 简介
版本控制(`version control`),或称为修订控制(`revision control`)、源控制(`source control`),其作用是保存源代码、配置文件和工程文件的修改历史,保证开发人员能够定位错误、回滚版本
大致经历`3`个阶段:
1. 保存副本
2. 集中式修订控制(`centralized revision control`)
3. 分布式修订控制(`distributed revision control`)
最简单的版本控制方式就是备份每次修改后的文件,优点在于操作简单,缺点在于占用存储大,在大文件情况下非常耗时,也不利于多人同时开发
第二和第三种方式都是通过保存文件修改记录来进行版本控制
第二种是集中式修订控制,即设立服务器来保存源代码和所有人修改的记录,上传和回滚版本通过服务器进行
优点在于这样可以保证占用的存储大大减小,同时仅需上传和下载修改记录就能够进行版本上传和更新,减小操作时间,并且能够有效协调地理隔离下的多人开发;管理员能控制开发人员权限
缺点在于仅在中央服务器保存了所有的修改记录,一旦损坏无法修复; 无法在同一版本库中同时进行多方向开发
![](https://git-scm.com/book/en/v2/images/centralized.png)
第三种是分布式修订控制,同一个开发库可以进行多分支开发,客户端也保存了所有分支的修改记录
优点:多分支开发;相比于集中式修订控制,仅需在拉取其他开发人员的变更和自己的提交记录时需要连接服务器,所以一些常用操作更加快捷,比如提交记录,浏览历史和回退操作
缺点:增加了客户端开发库的体积;操作较复杂
![](https://git-scm.com/book/en/v2/images/distributed.png)
使用集中式修订控制方式的版本控制系统:`svn`
使用分布式修订控制方式的版本控制系统:`git`
## 相关阅读
* [Version control](https://en.wikipedia.org/wiki/Version_control)

152
docs/git/base/子模块.md Normal file
View File

@@ -0,0 +1,152 @@
# 子模块
开发个人网站的过程中需要用到主题仓库,所以会在一个仓库的里面嵌套另一个仓库,如果直接将子仓库加入版本管理,很多文件无法进行版本化,所以想到了子模块的操作
## 创建子模块
使用`git submodule`命令添加子模块
$ git submodule add git@github.com:zjZSTU/hexo-theme-next.git blogs/themes/next
Cloning into 'blogs/themes/next'...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 5402 (delta 0), reused 0 (delta 0), pack-reused 5401
Receiving objects: 100% (5402/5402), 5.29 MiB | 1.03 MiB/s, done.
Resolving deltas: 100% (3294/3294), done.
Checking connectivity... done.
除了添加子仓库之外,还生成了一个配置文件`.gitmodules`
$ git status
On branch dev
Your branch is ahead of 'origin/dev' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitmodules
new file: blogs/themes/next
`.gitmodules`里面保存了子模块的地址和远程仓库链接
$ cat .gitmodules
[submodule "blogs/themes/next"]
path = blogs/themes/next
url = git@github.com:zjZSTU/hexo-theme-next.git
## 管理子模块
主仓库只能提示子模块中的文件修改,必须子模块自己进行添加和上传
比如在`next`文件夹内新建文件`hellogit`
# 对于主模块而言
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
(commit or discard the untracked or modified content in submodules)
modified: next (untracked content)
no changes added to commit (use "git add" and/or "git commit -a")
# 对于子模块而言
$ git status
HEAD detached at 0c5ed6f
Untracked files:
(use "git add <file>..." to include in what will be committed)
hellogit
nothing added to commit but untracked files present (use "git add" to track)
## 克隆子模块
克隆主仓库后子模块的文件夹在但是没有文件,还需要进一步克隆子仓库
# 初始化本地配置文件
$ git submodule init
# 拉取远程仓库
$ git submodule update
集成代码如下:
$ git submodule update --init --recursive
## 更新子模块
如果子模块有更新,可以进入子模块路径进行更新
$ git pull
或者在主仓库更新所有子模块
$ git submodule foreach git pull
可能会遇到如下错误:
```
Entering 'blogs/themes/next'
You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
Stopping at 'blogs/themes/next'; script returned non-zero status.
The command "git submodule foreach git pull" failed and exited with 1 during .
```
需要指定哪个分支,要么修改更新语句如下:
```
$ git submodule foreach git pull origin master
```
要么设置远程关联分支
```
$ git branch --set-upstream-to=origin/remote_branch your_branch
# 或
git branch --set-upstream your_branch origin/remote_branch
```
## 忽略子模块
参考:[submodule.<name>.ignore ](https://git-scm.com/docs/gitmodules#Documentation/gitmodules.txt-submoduleltnamegtignore)
当在子模块执行完成修改提交后,在主模块仍会显示子模块待提交
```
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: blogs/themes/next (new commits)
```
需要修改`.gitmodule``ignore`属性,用于定义何种情况下使用`git status`或者`diff`命令会显示子模块的修改状况
`4`个可选的属性值
1. `all`:子模块永远不会被认为是已修改的(但是当它处于暂存阶段时仍旧会在状态和提交命令的输出中)
2. `dirty`:子模块工作树上的所有改变都会被忽略,只考虑子模块的`HEAD`与其在父项目中记录的状态之间的差异
3. `untracked`:仅仅子模块未追踪的文件会被忽略。对于已追踪文件的提交差异和修改会显示
4. `none`:默认选项。不会忽略所有对于子模块的修改,会显示所有的对于提交差异以及对于已追踪或未追踪的文件修改
所以设置属性`ignore``all`就能忽略子模块变化
```
[submodule "blogs/themes/next"]
path = blogs/themes/next
url = https://github.com/zjZSTU/hexo-theme-next.git
ignore = all
```
## 相关阅读
* [7.11 Git 工具 - 子模块](https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97)

View File

@@ -0,0 +1,22 @@
# 工作区域
`git``3`个工作区域概念:
1. 仓库(`repository`)
2. 工作目录(`working directory`)
3. 暂存区域(`staging area`)
`git`仓库是保存项目元数据和所有修改记录的地方(就是`.git`文件夹)
工作目录保存从仓库中按某个版本取出的文件(和`.git`同一级目录下的其他文件)
暂存区域在仓库目录下,保存了下次要提交的修改记录
所以工程文件处于`3`种状态下
1. 已修改(`modified`):表示文件已修改
2. 已暂存(`staged)`:表示已保存修改记录,还未提交到仓库
3. 已提交(`commited`):保存在仓库,生成一个新的版本
![](https://git-scm.com/book/en/v2/images/areas.png)

View File

@@ -0,0 +1,104 @@
# 提交新版本
在本地创建`git`仓库后,就可以对文件进行版本控制.文件分为`2`种状态:已追踪(`tracked`)和未追踪(`untracked`)
* 未追踪(`untracked`)文件就是未添加到版本控制的文件
* 已追踪的文件可分为已修改(`modified`),已暂存(`staged`)和未修改(`unmodified`)文件
基本的`git`工作流程如下:
1. 在工作目录下添加文件,修改文件或删除文件
2. 保存文件状态到暂存区域
3. 提交修改记录到仓库
![](https://git-scm.com/book/en/v2/images/lifecycle.png)
## 查看文件状态
使用命令`git status`查看文件状态,同时会给出相应文件状态下的命令
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: docs/source/index.rst
Untracked files:
(use "git add <file>..." to include in what will be committed)
.vscode/
docs/source/git/
no changes added to commit (use "git add" and/or "git commit -a")
仅需查看文件状态
$ git status -s
A .vscode/settings.json
M docs/source/index.rst
?? docs/source/git/
符号`A`表示已暂存,符号`M`表示已修改,符号`??`表示未追踪
其中已修改文件有如下`3`种状态
$ git status -s
M README
MM Rakefile
M lib/simplegit.rb
* 符号`_M`表示已修改还未加入暂存区域
* 符号`MM`表示已加入暂存区域的已修改文件又被修改了
* 符号`M_`表示添加到暂存区域的已修改文件
## 添加修改文件
使用命令`git add`将未追踪文件或已修改文件添加到暂存区域
git add name1 name2 ...
# 添加所有文件
git add *
### 忽略文件
编辑`.gitignore`文件,将整个工程文件中不想添加版本控制的文件加入进去
所有空行或者以 开头的行都会被 Git 忽略。
可以使用标准的 glob 模式匹配。
匹配模式可以以(/)开头防止递归。
匹配模式可以以(/)结尾指定目录。
要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
可参考[github/gitignore](https://github.com/github/gitignore),上面包含了很多不同开发环境下的.gitignore文件
如果`git add`添加了忽略文件会报错,可以使用`-f`强制添加
git add -f name1 name2 ...
## 提交暂存文件
git commit -m '提交信息'
或者`git commit`然后回车,会打开设置的编辑器进行操作
## 撤销暂存文件
对添加到暂存区域的文件使用命令
git reset HEAD <file>...
## 撤销已修改文件
对已修改文件使用命令
git checkout -- <file>...

View File

@@ -0,0 +1,140 @@
# 标签设置
`git``log`设置的基础上添加了标签设置,其目的是标识重要的提交记录(比如标识发布版本)
*下面以[opencv版本库](https://github.com/opencv/opencv)为例*
## 标签分类
标签分为两种
1. 注解标签(`annotated tag`)
2. 命名标签(`named tag`)
注解标签用于版本管理,命名标签用于私有或者临时标注。注解标签包含创建日期、标签名、`e-mail`、标签信息和可选的`GPG`签名
命名标签仅包含标签名,所以称为轻量级标签(`lightweight tag`
## 创建标签
常用参数如下:
1. `-a, --annotate`:生成一个未签名的注解标签对象
2. `-s, --sign`生成一个GPG签名的标签使用默认`e-mail`地址作为键
3. `-u <keyid>, --local-user=<keyid>`:生成一个`GPG`签名的标签,使用给定的键
4. `-m <msg>, --message=<msg>`:添加标签消息
5. `-F <file>, --file=<file>`:带有标签消息的文件
### 注解标签
使用上述参数`-a`/`-s`/`-u`生成的标签称`.git/refs/tags`标签,注解标签必须添加消息,可以使用参数`-m`或者`-F`,否则会打开一个编辑器
$ git tag -a v1.1 -m "添加一个注解标签"
### 命名标签
不使用任何参数,直接添加标签名的就是命名标签
$ git tag v1.1
### 给过去提交记录创建标签
默认给当前记录创建标签,还可以给过去记录创建标签
查看记录
$ git log --pretty=oneline
9e1b1e5389237c2b9f6c7b9d7715d9836c0a5de1 OpenCV 3.4.2
a0baae8a559d31c22ae08976b41cc99e046ba52b Merge pull request #11875 from dkurt:dnn_fix_reshape
9a66331984c5b0f0a71d6208a8237fc410ae923e Merge pull request #11882 from alalek:videoio_vfw_lower_priority
...
...
打标签时指定提交签名(*头几位就行了*)
$ git tag v0.3 0660c1108f969a -a -m "给过去提交记录打标签"
## 查看标签
查看所有标签
$ git tag
2.2
...
...
4.0.1
4.0.1-openvino
查看指定系列标签,使用参数`-l`
# 列出4.x版本
$ git tag -l 4.*
4.0.0
4.0.0-alpha
4.0.0-beta
4.0.0-openvino
4.0.0-rc
4.0.1
4.0.1-openvino
查看某一标签信息
$ git show 标签名
# 对于注解标签而言
$ git show 4.0.0
tag 4.0.0 # 标签名
Tagger: Alexander Alekhin <alexander.a.alekhin@gmail.com> # 标签者
Date: Sun Nov 18 09:19:48 2018 +0000 # 标签日期
OpenCV 4.0.0 # 标签信息
...
...
# 对于命名标签而言
$ git show v1.1 # 仅显示提交记录
commit a92571f31ddc7ea959b99e129f6c4705a404c36b
Author: zhujian <505169307@qq.com>
Date: Thu Feb 28 20:37:01 2019 +0800
## 共享标签
`github`利用标签进行`github release`的发布
但是默认情况下不会传输标签到远程托管服务器,需要单独显式推送
$ git push origin v1.0
# 或者全部推送
$ git push origin --tags
## 文件解析
创建的标签在`.git/refs/tags`文件夹下
## 删除标签
### 本地删除
使用参数`-d`来删除标签
$ git tag -d v1.1
Deleted tag 'v1.1' (was a92571f)
### 远程删除
同样需要单独显式删除远程标签
# 删除版本v1.1
$ git push origin :refs/tags/v1.1
## 切换标签
切换到指定标签,创建分支同时指定标签名
```
# 利用标签内容创建分支,并切换到分支
$ git checkout -b 分支名 标签名
```
## 相关阅读
* [2.6 Git 基础 - 打标签](https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE)

View File

@@ -0,0 +1,32 @@
# 获取git仓库
## 本地新建
在工程目录中输入如下命令
git init
生成一个`.git`文件夹
## 远程拉取
从远程服务器`(github/gitee`等)获取地址后拉取到本地
git clone <repo> [<dir>]
`<repo>`表示输入地址
git clone https://github.com/zjZSTU/git-guide.git
也可制定路径
git clone https://github.com/zjZSTU/git-guide.git ../git-repo/
或文件夹
git clone https://github.com/zjZSTU/git-guide.git guide
下载并绑定指定分支
git clone --branch [标签名/分支名] [git地址]

11
docs/git/index.md Normal file
View File

@@ -0,0 +1,11 @@
# 引言
分为两部分:
1. `get started`: 基本`Git`操作;
2. `advanced`:进阶操作
## 相关阅读
* [Pro Git](https://gitee.com/progit/)

View File

@@ -50,4 +50,18 @@ nav:
- '[gitmessage]提交模板': message/gitmessage.md
- 'Conventional提交规范': message/conventional-commit.md
- 'Angular提交规范': message/angular-commit.md
- VERSION: version/semver.md
- VERSION: version/semver.md
- 工具:
- Git:
- 引言: git/index.md
- Base:
- 关于版本控制: git/base/关于版本控制.md
- 工作区域: git/base/工作区域.md
- '[git config]配置': git/base/git-config.md
- 获取git仓库: git/base/获取git仓库.md
- 提交新版本: git/base/提交新版本.md
- '[git log]日志查询': git/base/git-log.md
- 标签设置: git/base/标签设置.md
- '.gitkeep': git/base/gitkeep.md
- 子模块: git/base/子模块.md
- '[ssh][http]传输方式切换': git/base/ssh-http-传输方式切换.md