mirror of
https://github.com/notfresh/git_faq.git
synced 2026-06-04 16:34:23 +08:00
Update README.md
This commit is contained in:
195
README.md
195
README.md
@@ -138,33 +138,22 @@ http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
|
||||
|
||||
## 快问快答
|
||||
|
||||
### 1 问: 这个教程是写给谁看的?
|
||||
答: 写个对 Git 了解不多的人看的, 或者 Git 的新手.
|
||||
### 问: 这个教程是写给谁看的?
|
||||
答: 写个对 Git 了解不多的人看的, 或者 Git 的新手.
|
||||
|
||||
|
||||
|
||||
### 2 问: 这个教程我能学到什么?
|
||||
答: 可以学到关于 Git 的基础入门知识和工作实用技巧.
|
||||
|
||||
|
||||
|
||||
### 3 问: 为什么要写个教程?
|
||||
答: 有三个原因:
|
||||
|
||||
- 通过输出来检验输入, 这样能够更好的检验自己的知识.
|
||||
- 我学 Git 浪费了很多时间,我希望把自己的经验更好的传播出去, 让每一个读到的人都少走弯路.
|
||||
- 写教程或者技术贴有助于交友和提升自己形象.
|
||||
- 最后补充: 其实也是互相学习, 我又很多不懂的地方, 我目前知识够用, 我未必对 Git的底层原理和某些工具熟悉多少.
|
||||
### 问: 这个教程我能学到什么?
|
||||
答: 可以学到关于 Git 的基础入门知识和工作实用技巧,以及一些其他教程没有说透的知识点。
|
||||
|
||||
|
||||
|
||||
### 4 问: 需要知道 Git 和其他版本控制工具的区别吗?
|
||||
答: 不是必须知道的, 你至少需要了解 Git 就好了, 我认为做比较这种事需要了解或者熟悉比较对象. 如果你熟悉 Git或者 SVN, 或者 CVS, 还有很多别的版本管理工具, 你心里大概有数. 如果你不知道别的, 甚至连
|
||||
Git也不了解, 那么说比较这个话题, 毫无意义.
|
||||
### 问: 需要知道 Git 和其他版本控制工具的区别吗?
|
||||
答: 不是必须知道的, 你至少需要了解 Git 就好了, 我认为做比较这种事需要了解或者熟悉比较对象. 如果你熟悉 Git或者 SVN, 或者 CVS, 还有很多别的版本管理工具, 你心里大概有数.
|
||||
|
||||
|
||||
|
||||
### 7 问: Git 应该是用命令行还是界面工具?
|
||||
### 问: Git 应该是用命令行还是界面工具?
|
||||
答: 我认为各有优势. 命令行工具是程序员的传统工具, 面对黑乎乎的界面, 你需要从头开始记得很多命令, 需要有一个适应的过程.
|
||||
但是命令行因为不需要构建 GUI(用户界面), 开发工作量小, 可以接受的参数多, 使用灵活, 所以功能非常的强大. 缺点刚刚也说了, 就是要记住很多命令.
|
||||
命令行还有一个缺点, 就是阅读体验不好, 尤其是比较两个文件差异的时候, 可读性比较差.
|
||||
@@ -185,7 +174,9 @@ http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
|
||||
|
||||
Git 主要的目的是通过操纵这三棵树来以更加连续的状态记录项目的快照。
|
||||
|
||||
<img src="https://typora-1256991781.cos.ap-beijing.myqcloud.com/uPic/image-20200618092908203.png" alt="image-20200618092908203" style="zoom:50%;margin:0" />
|
||||
<div><img src="https://typora-1256991781.cos.ap-beijing.myqcloud.com/uPic/image-20200618092908203.png" alt="image-20200618092908203" style="width:50%;margin:0" /></div>
|
||||
|
||||
|
||||
|
||||
让我们来可视化这个过程:假设我们进入到一个新目录,其中有一个文件。 我们称其为该文件的 v1 版本,将它
|
||||
标记为蓝色。
|
||||
@@ -193,7 +184,13 @@ Git 主要的目的是通过操纵这三棵树来以更加连续的状态记录
|
||||
1 现在运行 git init,这会创建一个 Git 仓库,其中的 HEAD 引用指向未创建的分支(master 还
|
||||
不存在)。我们创建一个 file1.txt,那么现在仓库的情况是这样的:
|
||||
|
||||
<img src="https://typora-1256991781.cos.ap-beijing.myqcloud.com/uPic/image-20200618093126098.png" alt="image-20200618093126098" style="zoom:50%;margin:0" />
|
||||
|
||||
|
||||
<div>
|
||||
<img src="https://typora-1256991781.cos.ap-beijing.myqcloud.com/uPic/image-20200618093126098.png" alt="image-20200618093126098" style="width:50%;margin:0" />
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
此时,只有工作目录有内容。
|
||||
|
||||
@@ -333,7 +330,26 @@ git commit -m COMMIT_MASSAGE
|
||||
|
||||
其中,COMMIT_MASSAGE 应该尽量使用动宾短语,简练而清晰。
|
||||
|
||||

|
||||
如下,一个 commit 的详细信息
|
||||
|
||||
```
|
||||
commit b8b2b4985abf4c132abbfea3c5ffcf23e97db9ef (HEAD -> master)
|
||||
Author: notfreshmbp <1195056983@qq.com>
|
||||
Date: Fri Jun 26 19:35:51 2020 +0800
|
||||
|
||||
add 5.txt
|
||||
|
||||
This is a test message to show people that this is the detailed message.
|
||||
|
||||
diff --git a/5.txt b/5.txt
|
||||
index e69de29..58c9bdf 100644
|
||||
--- a/5.txt
|
||||
+++ b/5.txt
|
||||
@@ -0,0 +1 @@
|
||||
+111
|
||||
```
|
||||
|
||||
|
||||
|
||||
如果使用 git commit 而不带 -m 参数,那么会进入详细提交信息的编辑页面,默认的编辑器在 MacOS 和 Linux 下是 Vim 编辑器,第一行是提交信息,第二行是空行,(包含)第三行往下是详细说明信息,如果本次提交需要附加的信息特别多,应该在编辑器里详细写明。
|
||||
|
||||
@@ -361,11 +377,25 @@ git commit -m COMMIT_MASSAGE
|
||||
|
||||
### git log 命令
|
||||
|
||||
git 查看一个文件的变化, 也可以查看整个仓库的变化。
|
||||
git log 查看一个文件的提交历史, 也可以查看整个仓库的的提交历史。
|
||||
|
||||
毫不夸张的说, git log 的命令是我们使用最高频的命令之一。然而 git log 本身输出的格式不是我们想要的,如图所示:
|
||||
|
||||

|
||||
```
|
||||
commit b8b2b4985abf4c132abbfea3c5ffcf23e97db9ef (HEAD -> master)
|
||||
Author: notfreshmbp <1195056983@qq.com>
|
||||
Date: Fri Jun 26 19:35:51 2020 +0800
|
||||
|
||||
add 5.txt
|
||||
|
||||
This is a test message to show people that this is the detailed message.
|
||||
|
||||
commit 4e7f6e354179c19e5adfff6515a2982f732c02e2
|
||||
Author: notfreshmbp <1195056983@qq.com>
|
||||
Date: Thu Jun 18 10:42:32 2020 +0800
|
||||
|
||||
update 1.txt
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -377,7 +407,23 @@ git config --global alias.l "log --color --graph --all --pretty=format:'%Cred%h%
|
||||
|
||||
这条命令为 git log 配置了一个别名,附带了很多的参数,我们可以使用 git l 查看效果:
|
||||
|
||||

|
||||
```
|
||||
➜ git-test git:(master) git l
|
||||
|
||||
* b8b2b49 - (HEAD -> master) add 5.txt (2020-06-26 19:35:51) <notfreshmbp>
|
||||
* 4e7f6e3 - update 1.txt (2020-06-18 10:42:32) <notfreshmbp>
|
||||
* 6bee770 - (newb) branch split (2020-06-18 10:33:23) <notfreshmbp>
|
||||
* 0222aca - create 5.txt (2020-06-18 09:59:57) <notfreshmbp>
|
||||
* 95e0daf - (b4) create 3.txt (2020-06-18 08:54:08) <notfreshmbp>
|
||||
* ec694fd - (b3, b2) create 2.txt (2020-06-18 08:43:01) <notfreshmbp>
|
||||
* c1a68d9 - create 1.txt (2020-06-18 08:42:29) <notfreshmbp>
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
具体的参数可以去官方该网站或者 ProGit 上查找对应配置信息,方便个性化设置。
|
||||
|
||||
@@ -435,8 +481,6 @@ $ git checkout 1a
|
||||
|
||||
<img src="https://typora-1256991781.cos.ap-beijing.myqcloud.com/uPic/image-20200618095257616.png" alt="image-20200618095257616" style="zoom:50%;margin:0" />
|
||||
|
||||
|
||||
|
||||
#### 新增的文件和修改的文件的区别对待
|
||||
|
||||
新增的文件,没有被保存到历史区,无论怎么切换分支,都是可以的,但是如果修改了一个文件A,在切换分支前,应该提交或者暂存(stash命令)修改的内容。
|
||||
@@ -445,16 +489,6 @@ $ git checkout 1a
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### git reflog 命令
|
||||
|
||||
TODO
|
||||
@@ -519,6 +553,10 @@ reset 要做的的第三件事情就是让工作目录看起来像索引。 如
|
||||
|
||||
### git merge
|
||||
|
||||
举个例子,分支 master 含有 c1, c2, c3,共3个提交, 分支 dev 含有 c1, c2,c3, c4 共4个提交,即 dev完全包含 master,如果使用 git merge dev master, 意思就是让 master 合并 dev 分支了里的内容。merge 完成后,master也有了 c1,c2, c3,c4 共4个分支,而dev没有任何变化。这种合并叫做 fast forward, 可以理解为“快速追上”的策略。
|
||||
|
||||
再举个例子,分支 master 含有 c1, c2, c3,共3个提交, 分支 dev 含有 c1, c2, c4 共4个提交,即 dev 分支有包含 master 分支没有的内容,如果使用 git merge dev master, 意思就是让 master 合并 dev 分支了里的内容。merge 完成后,master也有了 c1,c2, c3, c5, 共4个分支,c5里包含着 c4 提交的内容,而dev没有任何变化。这种合并为合并了分叉内容,会自动创建一个提交,例如c5分支。
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -527,11 +565,11 @@ reset 要做的的第三件事情就是让工作目录看起来像索引。 如
|
||||
|
||||
### git pull
|
||||
|
||||
TODO
|
||||
git pull = git fetch + git merge
|
||||
|
||||
git pull --rebase = git fetch + git rebase origin
|
||||
|
||||
### git push
|
||||
|
||||
TODO
|
||||
|
||||
|
||||
|
||||
@@ -541,9 +579,9 @@ TODO
|
||||
|
||||
|
||||
|
||||
### **问: rebase 是个什么东西?**
|
||||
### git rebase
|
||||
|
||||
答: rebase 变基, 打个比方, 分支 master 包含 C1,C2,C3, 分支 branchX 包含 C2, C4, C5.
|
||||
rebase 变基, 打个比方, 分支 master 包含 C1,C2,C3, 分支 branchX 包含 C2, C4, C5.
|
||||
采用普通的merge策略, 会新建一个提交C6, 让C3, C5都指向C6. C6的内容包含master和branchX都有的内容.
|
||||
采用rebase, 命令格式为 git rebase BASE_BRANCH TO_REPLAY_BRANCH
|
||||
在这里使用 `git rebase master branchX`就是把 C4-C5在C3后续上, 然后branchX指针指向C5, master位置不变, 也就是说分支的指向位置都没变, 只不过提交历史的形状变了.
|
||||
@@ -554,7 +592,7 @@ TODO
|
||||
|
||||
|
||||
|
||||
问: 什么情况下应该用 rebase?
|
||||
#### 什么情况下应该用 rebase?
|
||||
|
||||
第一种场景:
|
||||
如果我想手动的自己处理分支, 比如远程仓库已经更新, 我执行如下操作:
|
||||
@@ -570,11 +608,8 @@ TODO
|
||||
|
||||
|
||||
### git fetch 命令
|
||||
```
|
||||
$ git fetch --all
|
||||
$ git fetch --tags
|
||||
$ git reset --hard origin/master
|
||||
```
|
||||
|
||||
git fetch 命令 表示把远程仓库拉下来,但是不合并到当前分支,仅仅用作后序操作或者查看内容之用。
|
||||
|
||||
|
||||
|
||||
@@ -596,6 +631,64 @@ $ git diff 2a 2b
|
||||
|
||||
|
||||
|
||||
### git stash
|
||||
|
||||
如图所示,假设如下场景,我在b4分支的编辑的时候,接到一个临时任务,那么我需要把当前的工作临时保存起来,但不提交,那么我使用 git stash命令即可。然后我切换到其他分支去工作,那我我现在的提交历史如下:
|
||||
|
||||
> 想要看到这种效果,配置如下:
|
||||
> git config --global alias.l "log --color --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s%Creset %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date='format:%Y- %m-%d %H:%M:%S'"
|
||||
>
|
||||
> 然后使用 git l 即可查看。
|
||||
|
||||
我们可以看到 新增了两个“提交”,d341: index on b4 和 1c03 - (refs/stash) WIP
|
||||
|
||||
```
|
||||
* b8b2b49 - (HEAD -> master) add 5.txt (2020-06-26 19:35:51) <notfreshmbp>
|
||||
* 4e7f6e3 - update 1.txt (2020-06-18 10:42:32) <notfreshmbp>
|
||||
* 6bee770 - (newb) branch split (2020-06-18 10:33:23) <notfreshmbp>
|
||||
* 0222aca - create 5.txt (2020-06-18 09:59:57) <notfreshmbp>
|
||||
| * 1c03cfd - (refs/stash) WIP on b4: 95e0daf... (2020-06-18 10:44:33) <notfreshmbp>
|
||||
| |\
|
||||
|/ /
|
||||
| * d34156e - index on b4: 95e0daf create 3.txt (2020-06-18 10:44:33) <notfreshmbp>
|
||||
|/
|
||||
* 95e0daf - (b4) create 3.txt (2020-06-18 08:54:08) <notfreshmbp>
|
||||
* ec694fd - (b3, b2) create 2.txt (2020-06-18 08:43:01) <notfreshmbp>
|
||||
* c1a68d9 - create 1.txt (2020-06-18 08:42:29) <notfreshmbp>
|
||||
```
|
||||
|
||||
当我其他事情工作完毕,我切换到 b4 分支,我再使用 **git stash pop**,可以把临时工作再恢复到工作区。
|
||||
|
||||
```
|
||||
➜ git-test git:(b4) git stash pop
|
||||
On branch b4
|
||||
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: 1.txt
|
||||
|
||||
no changes added to commit (use "git add" and/or "git commit -a")
|
||||
Dropped refs/stash@{0} (1c03cfd8d607a723be6d8afd270fba4d734e3b29)
|
||||
|
||||
➜ git-test git:(b4) ✗ git l
|
||||
|
||||
* b8b2b49 - (master) add 5.txt (2020-06-26 19:35:51) <notfreshmbp>
|
||||
* 4e7f6e3 - update 1.txt (2020-06-18 10:42:32) <notfreshmbp>
|
||||
* 6bee770 - (newb) branch split (2020-06-18 10:33:23) <notfreshmbp>
|
||||
* 0222aca - create 5.txt (2020-06-18 09:59:57) <notfreshmbp>
|
||||
* 95e0daf - (HEAD -> b4) create 3.txt (2020-06-18 08:54:08) <notfreshmbp>
|
||||
* ec694fd - (b3, b2) create 2.txt (2020-06-18 08:43:01) <notfreshmbp>
|
||||
* c1a68d9 - create 1.txt (2020-06-18 08:42:29) <notfreshmbp>
|
||||
|
||||
```
|
||||
|
||||
现在提交历史又恢复了,我们可以继续编辑了。
|
||||
|
||||
|
||||
|
||||
注意,如果我们
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -630,15 +723,15 @@ Small note: If editing the README, please conform to the [standard-readme](https
|
||||
|
||||
## 参考文献
|
||||
|
||||
[1]git - 简明指南, no deep shit
|
||||
[1]git - 简明指南
|
||||
|
||||
https://rogerdudler.github.io/git-guide/index.zh.html
|
||||
|
||||
[2]工作流一目了然,看小姐姐用动图展示10大Git命令
|
||||
[2]用动图展示10大Git命令
|
||||
|
||||
https://mp.weixin.qq.com/s/PUUL913fig6cFfqy4OKcGA
|
||||
|
||||
[3]Git使用教程:最详细、最傻瓜、最浅显、真正手把手教!(建议收藏)
|
||||
[3]Git使用教程:最详细、最傻瓜、最浅显、真正手把手教
|
||||
|
||||
https://mp.weixin.qq.com/s/g0jgzZZ0RG_-dNoBwEs16Q
|
||||
|
||||
@@ -646,7 +739,7 @@ https://mp.weixin.qq.com/s/g0jgzZZ0RG_-dNoBwEs16Q
|
||||
|
||||
https://mp.weixin.qq.com/s/S33W_L9-taAC-aEuHvZYPQ
|
||||
|
||||
[5]Git 居然还有这么高级用法,你一定需要
|
||||
[5]Git 高级用法
|
||||
|
||||
https://mp.weixin.qq.com/s/LTHLKrle5mOczHKsK2k30A
|
||||
|
||||
|
||||
Reference in New Issue
Block a user