change path
This commit is contained in:
158
notes/version-control-tool/Git.md
Normal file
158
notes/version-control-tool/Git.md
Normal file
@ -0,0 +1,158 @@
|
||||
<!-- GFM-TOC -->
|
||||
* [学习资料](#学习资料)
|
||||
* [集中式与分布式](#集中式与分布式)
|
||||
* [Git 的中心服务器](#git-的中心服务器)
|
||||
* [Git 工作流](#git-工作流)
|
||||
* [分支实现](#分支实现)
|
||||
* [冲突](#冲突)
|
||||
* [Fast forward](#fast-forward)
|
||||
* [分支管理策略](#分支管理策略)
|
||||
* [储藏(Stashing)](#储藏stashing)
|
||||
* [SSH 传输设置](#ssh-传输设置)
|
||||
* [.gitignore 文件](#gitignore-文件)
|
||||
* [Git 命令一览](#git-命令一览)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 学习资料
|
||||
|
||||
- [Git - 简明指南](http://rogerdudler.github.io/git-guide/index.zh.html)
|
||||
- [图解 Git](http://marklodato.github.io/visual-git-guide/index-zh-cn.html)
|
||||
- [廖雪峰 : Git 教程](https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000)
|
||||
- [Learn Git Branching](https://learngitbranching.js.org/)
|
||||
|
||||
# 集中式与分布式
|
||||
|
||||
Git 属于分布式版本控制系统,而 SVN 属于集中式。
|
||||
|
||||
集中式版本控制只有中心服务器拥有一份代码,而分布式版本控制每个人的电脑上就有一份完整的代码。
|
||||
|
||||
集中式版本控制有安全性问题,当中心服务器挂了所有人都没办法工作了。
|
||||
|
||||
集中式版本控制需要连网才能工作,如果网速过慢,那么提交一个文件的会慢的无法让人忍受。而分布式版本控制不需要连网就能工作。
|
||||
|
||||
分布式版本控制新建分支、合并分支操作速度非常快,而集中式版本控制新建一个分支相当于复制一份完整代码。
|
||||
|
||||
# Git 的中心服务器
|
||||
|
||||
Git 的中心服务器用来交换每个用户的修改。没有中心服务器也能工作,但是中心服务器能够 24 小时保持开机状态,这样就能更方便的交换修改。Github 就是一种 Git 中心服务器。
|
||||
|
||||
# Git 工作流
|
||||
|
||||
<div align="center"> <img src="../pics//a1198642-9159-4d88-8aec-c3b04e7a2563.jpg"/> </div><br>
|
||||
|
||||
新建一个仓库之后,当前目录就成为了工作区,工作区下有一个隐藏目录 .git,它属于 Git 的版本库。
|
||||
|
||||
Git 版本库有一个称为 stage 的暂存区,还有自动创建的 master 分支以及指向分支的 HEAD 指针。
|
||||
|
||||
<div align="center"> <img src="../pics//46f66e88-e65a-4ad0-a060-3c63fe22947c.png"/> </div><br>
|
||||
|
||||
- git add files 把文件的修改添加到暂存区
|
||||
- git commit 把暂存区的修改提交到当前分支,提交之后暂存区就被清空了
|
||||
- git reset -- files 使用当前分支上的修改覆盖暂缓区,用来撤销最后一次 git add files
|
||||
- git checkout -- files 使用暂存区的修改覆盖工作目录,用来撤销本地修改
|
||||
|
||||
<div align="center"> <img src="../pics//17976404-95f5-480e-9cb4-250e6aa1d55f.png"/> </div><br>
|
||||
|
||||
可以跳过暂存区域直接从分支中取出修改或者直接提交修改到分支中
|
||||
|
||||
- git commit -a 直接把所有文件的修改添加到暂缓区然后执行提交
|
||||
- git checkout HEAD -- files 取出最后一次修改,可以用来进行回滚操作
|
||||
|
||||
# 分支实现
|
||||
|
||||
Git 把每次提交都连成一条时间线。分支使用指针来实现,例如 master 分支指针指向时间线的最后一个节点,也就是最后一次提交。HEAD 指针指向的是当前分支。
|
||||
|
||||
<div align="center"> <img src="../pics//fb546e12-e1fb-4b72-a1fb-8a7f5000dce6.jpg"/> </div><br>
|
||||
|
||||
新建分支是新建一个指针指向时间线的最后一个节点,并让 HEAD 指针指向新分支表示新分支成为当前分支。
|
||||
|
||||
<div align="center"> <img src="../pics//bc775758-89ab-4805-9f9c-78b8739cf780.jpg"/> </div><br>
|
||||
|
||||
每次提交只会让当前分支向前移动,而其它分支不会移动。
|
||||
|
||||
<div align="center"> <img src="../pics//5292faa6-0141-4638-bf0f-bb95b081dcba.jpg"/> </div><br>
|
||||
|
||||
合并分支也只需要改变指针即可。
|
||||
|
||||
<div align="center"> <img src="../pics//1164a71f-413d-494a-9cc8-679fb6a2613d.jpg"/> </div><br>
|
||||
|
||||
# 冲突
|
||||
|
||||
当两个分支都对同一个文件的同一行进行了修改,在分支合并时就会产生冲突。
|
||||
|
||||
<div align="center"> <img src="../pics//58e57a21-6b6b-40b6-af85-956dd4e0f55a.jpg"/> </div><br>
|
||||
|
||||
Git 会使用 <<<<<<< ,======= ,>>>>>>> 标记出不同分支的内容,只需要把不同分支中冲突部分修改成一样就能解决冲突。
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
Creating a new branch is quick & simple.
|
||||
=======
|
||||
Creating a new branch is quick AND simple.
|
||||
>>>>>>> feature1
|
||||
```
|
||||
|
||||
# Fast forward
|
||||
|
||||
"快进式合并"(fast-farward merge),会直接将 master 分支指向合并的分支,这种模式下进行分支合并会丢失分支信息,也就不能在分支历史上看出分支信息。
|
||||
|
||||
可以在合并时加上 --no-ff 参数来禁用 Fast forward 模式,并且加上 -m 参数让合并时产生一个新的 commit。
|
||||
|
||||
```
|
||||
$ git merge --no-ff -m "merge with no-ff" dev
|
||||
```
|
||||
|
||||
<div align="center"> <img src="../pics//dd78a1fe-1ff3-4bcf-a56f-8c003995beb6.jpg"/> </div><br>
|
||||
|
||||
# 分支管理策略
|
||||
|
||||
master 分支应该是非常稳定的,只用来发布新版本;
|
||||
|
||||
日常开发在开发分支 dev 上进行。
|
||||
|
||||
<div align="center"> <img src="../pics//245fd2fb-209c-4ad5-bc5e-eb5664966a0e.jpg"/> </div><br>
|
||||
|
||||
# 储藏(Stashing)
|
||||
|
||||
在一个分支上操作之后,如果还没有将修改提交到分支上,此时进行切换分支,那么另一个分支上也能看到新的修改。这是因为所有分支都共用一个工作区的缘故。
|
||||
|
||||
可以使用 git stash 将当前分支的修改储藏起来,此时当前工作区的所有修改都会被存到栈上,也就是说当前工作区是干净的,没有任何未提交的修改。此时就可以安全的切换到其它分支上了。
|
||||
|
||||
```
|
||||
$ git stash
|
||||
Saved working directory and index state \ "WIP on master: 049d078 added the index file"
|
||||
HEAD is now at 049d078 added the index file (To restore them type "git stash apply")
|
||||
```
|
||||
|
||||
该功能可以用于 bug 分支的实现。如果当前正在 dev 分支上进行开发,但是此时 master 上有个 bug 需要修复,但是 dev 分支上的开发还未完成,不想立即提交。在新建 bug 分支并切换到 bug 分支之前就需要使用 git stash 将 dev 分支的未提交修改储藏起来。
|
||||
|
||||
# SSH 传输设置
|
||||
|
||||
Git 仓库和 Github 中心仓库之间的传输是通过 SSH 加密。
|
||||
|
||||
如果工作区下没有 .ssh 目录,或者该目录下没有 id_rsa 和 id_rsa.pub 这两个文件,可以通过以下命令来创建 SSH Key:
|
||||
|
||||
```
|
||||
$ ssh-keygen -t rsa -C "youremail@example.com"
|
||||
```
|
||||
|
||||
然后把公钥 id_rsa.pub 的内容复制到 Github "Account settings" 的 SSH Keys 中。
|
||||
|
||||
# .gitignore 文件
|
||||
|
||||
忽略以下文件:
|
||||
|
||||
- 操作系统自动生成的文件,比如缩略图;
|
||||
- 编译生成的中间文件,比如 Java 编译产生的 .class 文件;
|
||||
- 自己的敏感信息,比如存放口令的配置文件。
|
||||
|
||||
不需要全部自己编写,可以到 [https://github.com/github/gitignore](https://github.com/github/gitignore) 中进行查询。
|
||||
|
||||
# Git 命令一览
|
||||
|
||||
<div align="center"> <img src="../pics//7a29acce-f243-4914-9f00-f2988c528412.jpg"/> </div><br>
|
||||
|
||||
比较详细的地址:http://www.cheat-sheets.org/saved-copy/git-cheat-sheet.pdf
|
||||
|
||||
|
269
notes/version-control-tool/git常用命令.md
Normal file
269
notes/version-control-tool/git常用命令.md
Normal file
@ -0,0 +1,269 @@
|
||||
git 常用命令
|
||||
|
||||
`git help`
|
||||
|
||||
## Branch related
|
||||
|
||||
### 1. 创建分支
|
||||
|
||||
* 根据当前分支创建新分支
|
||||
|
||||
`git branch newBranchName`
|
||||
|
||||
* 创建并切换分支
|
||||
|
||||
`git checkout -b branchName`
|
||||
|
||||
* 根据其他远程分支创建新分支
|
||||
|
||||
`git branch newBranchName origin/ohterBranchName `
|
||||
|
||||
* 根据其他远程分支创建新分支并切换分支
|
||||
|
||||
`git checkout -b newBranchName origin/otherBranchName`
|
||||
|
||||
* 重命名分支
|
||||
|
||||
`git branch -m oldBranchName newBranchName`
|
||||
|
||||
### 2. 提交分支
|
||||
|
||||
* 提交分支到远程库
|
||||
|
||||
`git push origin branchName`
|
||||
|
||||
* 是否提交成功:查看远程端所有分支
|
||||
|
||||
`git branch -r`
|
||||
|
||||
* 查看本地分支
|
||||
|
||||
`git branch`
|
||||
|
||||
* 查看本地和远程分支
|
||||
|
||||
`git branch -a`
|
||||
|
||||
### 3. 删除分支
|
||||
|
||||
* 删除本地未提交分支
|
||||
|
||||
`git branch -d branchName`
|
||||
|
||||
* 删除远程分支
|
||||
|
||||
`git branch -r -d origin/branchName`
|
||||
|
||||
`git push origin :branchName`**冒号前有空格**
|
||||
|
||||
* 批量删除本地分支
|
||||
|
||||
`git branch -a | grep -v -E 'master|develop' | xargs git branch -D`
|
||||
|
||||
* 批量删除远程分支
|
||||
|
||||
`git branch -r| grep -v -E 'master|develop' | sed 's/origin\///g' | xargs -I {} git push origin :{}`
|
||||
|
||||
> 如果有些分支无法删除,是因为远程分支的缓存问题,可以使用`git remote prune`
|
||||
|
||||
* 批量删除本地tag
|
||||
|
||||
`git tag | xargs -I {} git tag -d {}`
|
||||
|
||||
* 批量删除远程tag
|
||||
|
||||
`git tag | xargs -I {} git push origin :refs/tags/{}`
|
||||
|
||||
*Ps:用到命令说明*
|
||||
|
||||
*grep -v -E 排除master 和 develop*
|
||||
|
||||
*-v 排除*
|
||||
*-E 使用正则表达式*
|
||||
|
||||
*xargs 将前面的值作为参数传入 `git branch -D` 后面*
|
||||
|
||||
*-I {} 使用占位符 来构造 后面的命令*
|
||||
|
||||
*强制删除把-d 换成-D*
|
||||
|
||||
### 4. 切换分支
|
||||
|
||||
`git checkout branchName`
|
||||
|
||||
*切换分支时需要将修改过的文件全部提交*
|
||||
|
||||
### 5. 合并分支
|
||||
|
||||
----
|
||||
|
||||
* 将开发中的分支(branchName)合并到主分支(branchName)
|
||||
* 首先切换到主分支`git checkout origin/branchName`
|
||||
* 合并`git merge newBranchName`
|
||||
|
||||
----
|
||||
|
||||
* 将开发中的分支(branchName)合并到主分支(branchName),不保留日志
|
||||
* 首先切换到主分支`git checkout origin/branchName`
|
||||
* 合并`git rebase newBranchName`
|
||||
|
||||
----
|
||||
|
||||
* 保存之前的分支历史合并`git merge -no -ff newBranchName`
|
||||
|
||||
### 6. 撤销
|
||||
|
||||
* 撤销最近一次提交
|
||||
|
||||
`git reset HEAD^`
|
||||
|
||||
### 7. 查看各分支最后一次提交
|
||||
|
||||
`git branch -v`
|
||||
|
||||
### 8. 查看列出详细信息,在每一个名字后面列出其远程url
|
||||
|
||||
`git remote -v`
|
||||
|
||||
----
|
||||
|
||||
## file related
|
||||
|
||||
### 1. git stash命令
|
||||
|
||||
`git stash`命令用于将更改储藏在脏工作目录中
|
||||
|
||||
`git stash list`列索储藏的修改
|
||||
|
||||
`git stash show`进行检查
|
||||
|
||||
`git stash apply` 恢复
|
||||
|
||||
`git stash drop stash@{0}`移除存储
|
||||
|
||||
### 2. git clone
|
||||
|
||||
`git clone <版本库的网址>`将存储库克隆到本地仓库
|
||||
|
||||
`git clone <版本库的网址> <本地目录名>`指定克隆的目录
|
||||
|
||||
### 3. Git init/git add
|
||||
|
||||
`git init` 初始化一个工作区
|
||||
|
||||
`git add fieName`添加文件到暂存区
|
||||
|
||||
`git add .`将文件的修改,文件的新建,添加到暂存区
|
||||
|
||||
`git add -u`将文件的修改、文件的删除,添加到暂存区
|
||||
|
||||
`git add -A`将文件的修改,文件的删除,文件的新建,添加到暂存区
|
||||
|
||||
---
|
||||
|
||||
**远程仓库地址操作**
|
||||
|
||||
---
|
||||
|
||||
`git remote add origin git@github.com:athc/ath_auth.git`将本地仓库和远程仓库连接起来
|
||||
|
||||
`git remote set-url origin git@github.com:athc/ath-cloud.git`修改远程仓库地址
|
||||
|
||||
或分两步先删除
|
||||
|
||||
`git remote rm origin`先删除远程仓库地址
|
||||
|
||||
`git remote add origin git@github.com:athc/ath-cloud.git`添加远程仓库地址
|
||||
|
||||
---
|
||||
|
||||
### 4. git commit#git reset#git rm#git mv
|
||||
|
||||
`git commit -m 'message'`提交添加的文件
|
||||
|
||||
`git commit -a -m 'message'` 相当于git add -A +git commit
|
||||
|
||||
git reset将当前HEAD 复位到指定状态,一般用于撤销操作
|
||||
|
||||
`git reset fileName`或`git reset HEAD fileName`回退文件
|
||||
|
||||
`git reset HEAD^`回退版本,一个**^**表示一个版本,可以多个,另外也可以使用`git reset HEAD~n`这种形式
|
||||
|
||||
`git reset commit-id`回退某一个commit-ID的提交
|
||||
|
||||
|
||||
|
||||
Git rm命令用于从工作区和索引中删除文件
|
||||
|
||||
`git rm Documentation/\*.txt`从documentation目录及其任何子目录下的索引中删除所有.txt文件
|
||||
|
||||
|
||||
|
||||
Git mv命令用于移动或重命名文件,目录或符号链接
|
||||
|
||||
`git text.txt mydir`把text.txt 移动到mydir目录,这条命令相当于以下执行
|
||||
|
||||
```
|
||||
$ mv test.txt mydir/
|
||||
$ git rm test.txt
|
||||
$ git add mydir
|
||||
```
|
||||
|
||||
### 5. git status#git log #git show#git diff
|
||||
|
||||
`git status`命令用于显示工作目录和暂存区的状态。使用此命令能看到那些修改被暂存到了, 哪些没有, 哪些文件没有被Git tracked到
|
||||
|
||||
`git status -uno`只列出所有已经被git管理的切被修改但是没提交的文件
|
||||
|
||||
|
||||
|
||||
git log命令用于显示提交日志信息
|
||||
|
||||
`git log -3`查看最近三次提交日志
|
||||
|
||||
`git log`查看提交日志
|
||||
|
||||
`git log --no-merges`显示提交记录但是跳过合并的
|
||||
|
||||
|
||||
|
||||
`git show`命令用于显示各种类型的对象
|
||||
|
||||
|
||||
|
||||
`git diff`命令用于显示提交和工作树等之间的更改。此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容
|
||||
|
||||
|
||||
|
||||
`git checkout -b newBrach origin/master`从远程分支取回到本地分支
|
||||
|
||||
如:
|
||||
|
||||
`git checkout -b dev/1.5.4 origin/dev/1.5.4`从远程dev/1.5.4分支取得到本地分支/dev/1.5.4
|
||||
|
||||
|
||||
|
||||
### 5. Git fetch #git pull # git merge
|
||||
|
||||
`git fetch`命令用于从另一个存储库下载对象和引用
|
||||
|
||||
`git fetch`可以同步远程分支信息
|
||||
|
||||
`git pull`命令用于从另一个存储库或本地分支获取并集成(整合),在默认模式下,`git pull`是`git fetch`后跟`git merge FETCH_HEAD`的缩写。
|
||||
|
||||
`git push origin localBranchName` 将本地分支push到远程分支
|
||||
|
||||
`git pull`命令用于从另一个存储库或本地分支获取并集成(整合)。`git pull`命令的作用是:取回远程主机某个分支的更新,再与本地的指定分支合并,它的完整格式稍稍有点复杂
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
###
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user