Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/*
_book/*
_book/*
node_modules/*
87 changes: 48 additions & 39 deletions SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
# 目录
# Summary

1. [本书介绍](README.md)
1. Git基础
1. [Git追踪文件的生命周期](git-basic/life-cycle.md)
1. [文件状态](git-basic/files-state.md)
1. [修改类型](git-basic/change-types.md)
1. [理解Git三个树](git-basic/three-tree.md)
1. [理解git checkout与git reset区别](git-basic/checkout-reset.md)
1. Git基本命令
1. 文件操作
1. [git mv](git-commands/git-mv.md)
1. [git rm](git-commands/git-rm.md)
1. [git add](git-commands/git-add.md)
1. 查询
1. [git status](git-commands/git-status.md)
1. [git diff & git difftool](git-commands/git-diff.md)
1. [git log](git-commands/git-log.md)
1. 提交、撤销与拉取
1. [git commit](git-commands/git-commit.md)
1. [git checkout](git-commands/git-checkout.md)
1. [git reset](git-commands/git-reset.md)
1. 远程库操作
1. [git clone](git-commands/git-clone.md)
1. [git remote](git-commands/git-remote.md)
1. [git fetch](git-commands/git-fetch.md)
1. [git push](git-commands/git-push.md)
1. 标签
1. [git tag & git show](git-commands/git-tag.md)
1. 分支操作与管理
1. [git branch](git-commands/git-branch.md)
1. [git checkout](git-commands/git-checkout.md)
1. [git merge](git-commands/git-merge.md)
1. [git rebase](git-commands/git-rebase.md)
1. 偏好设置
1. [git config](git-commands/git-config.md)
1. .gitignore
1. [使用.gitignore文件]()
1. [glob模式]()
1. 常用场景实战
* [本书介绍](README.md)
* Git基础
* [Git追踪文件的生命周期](git-basic/life-cycle.md)
* [文件状态](git-basic/files-state.md)
* [修改类型](git-basic/change-types.md)
* [理解Git三个树](git-basic/three-tree.md)
* [理解git checkout与git reset区别](git-basic/checkout-reset.md)
* Git基本命令
* 文件操作
* [git mv](git-commands/git-mv.md)
* [git rm](git-commands/git-rm.md)
* [git add](git-commands/git-add.md)
* 查询
* [git status](git-commands/git-status.md)
* [git diff & git difftool](git-commands/git-diff.md)
* [git log](git-commands/git-log.md)
* 提交、撤销与拉取
* [git commit](git-commands/git-commit.md)
* [git checkout](git-commands/git-checkout.md)
* [git reset](git-commands/git-reset.md)
* 远程库操作
* [git clone](git-commands/git-clone.md)
* [git remote](git-commands/git-remote.md)
* [git fetch](git-commands/git-fetch.md)
* [git push](git-commands/git-push.md)
* 标签
* [git tag & git show](git-commands/git-tag.md)
* 分支操作与管理
* [git branch](git-commands/git-branch.md)
* [git checkout](git-commands/git-checkout.md)
* [git merge](git-commands/git-merge.md)
* [git rebase](git-commands/git-rebase.md)
* 偏好设置
* [git config](git-commands/git-config.md)
* Git高级命令
* 储藏与清理
* [git stash](git-commands/git-stash.md)
* 常用场景实战
* [修改commit提交](git-commands/git-ammend-commit.md)
* [修改远程仓库提交](git-commands/git-force-update-remote-commit.md)
* .gitignore
* 使用.gitignore文件
* glob模式
* GitLab操作
* [什么是GitLab](gitlab/about-gitlab.md)
* [基于 Merge Request 的开发流程](gitlab/gitlab-merge-request.md)
* [如何撤销 Merge Request?](gitlab/gitlab-merge-request-revert.md)

10 changes: 10 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
"fontsettings",
"sharing",
"lunr",
"search",
"highlight",
"livereload"
]
}
8 changes: 8 additions & 0 deletions git-commands/git-ammend-commit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#git commit --amend
>如果你已经完成提交,又因为之前提交时忘记添加一个新创建的文件,想通过添加或修改文件来更改提交的快照,修改提交备注信息,也可以通过类似的操作来完成。

##修改提交
git commit --amend [<-m commit_message>]

* ###参数
* commit_message (需要修改的commit备注信息)
8 changes: 8 additions & 0 deletions git-commands/git-force-update-remote-commit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#git push -f
>如果你已经把本地提交commit已经push到远程分支。但是如果发现本地分支的代码出现错误,需要重写commit历史,例如使用了git commit --amend,一般情况下,远程分支是会拒绝本次提交,这样只能够通过强制更新远程分支(洗版)来进行更新。

##强制提交远程分支
git push -f

##注意事项
此操作非常危险,如果远程分支已经被其他人fetch到本地分支,就严格禁止再更改原有的commit历史!
87 changes: 71 additions & 16 deletions git-commands/git-stash.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,75 @@
#git add -i
>交互式暂存,如果希望一些改动能放到若干提交而不是混杂在一起成为一个提交时,希望这些改动能放到若干提交而不是混杂在一起成为一个提交时,这几个工具会非常有用。并且可以撤销一些暂存提交。
#git stash
>当你在项目的一部分上已经工作一段时间后,所有东西都进入了混乱的状态,而这时你想要切换到另一个分支做一点别的事情。 问题是,你不想仅仅因为过会儿回到这一点而为做了一半的工作创建一次提交。 针对这个问题的答案是 git stash 命令

##推送数据
git add -i
##储藏修改
git stash
当前状态:
````bash
$ git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: index.html

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: lib/simplegit.rb
````

````bash
git stash
Saved working directory and index state WIP on master: 84dfd50 this is change at github master
````
再次查看目录是工作目录是干净的了:
````bash
git status
# On branch master
nothing to commit, working directory clean
````
在这时,你能够轻易地切换分支并在其他地方工作;你的修改被存储在栈上。 要查看储藏的东西,可以使用 git stash list:
````bash
$ git stash list
stash@{0}: WIP on master: 84dfd50 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
````

##取回修改
git stash apply [<stash_name>]

* ###参数
* stash_name (储存名称)

* ###参数
* -i/--interactive Git 将会进入一个交互式终端模式

在本例中,有两个之前做的储藏,所以你接触到了三个不同的储藏工作。 可以通过原来 stash 命令的帮助提示中的命令将你刚刚储藏的工作重新应用:git stash apply。
````bash
git stash apply
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: index.html
# modified: lib/simplegit.rb
#
````
这样之前的修改就会重回工作区。
##注意事项
如果想要应用其中一个更旧的储藏,可以通过名字指定它,像这样:git stash apply stash@{2}。 如果不指定一个储藏,Git 认为指定的是最近的储藏

##删除储藏修改
git stash drop [<stash_name>]

* ###参数
* stash_name (储存名称)

应用选项只会尝试应用暂存的工作 - 在堆栈上还有它。 可以运行 git stash drop 加上将要移除的储藏的名字来移除它:

````bash
staged unstaged path
1: +6/-0 nothing base.js
2: +2/-0 +1/-0 one.js
3: +5/-0 nothing work.js

*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)
````
7 changes: 7 additions & 0 deletions gitlab/about-gitlab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#什么是GitLab

![image](src/logo_gitlab.png)

GitLab 是一个类似于 GitHub 的开源源码托管服务,它除了提供基于 git 的基本代码托管服务外。还具备很多与软件开发协作相关的其他功能。比如 issues、Merge Requests 、CD/CI发布功能等。

利用 GitLab 提供的这些功能,我们可以实践一些简单的项目管理和协作流程。这套流程借鉴于很多成功的开源项目,非常适合在小型开发团队里面使用。
28 changes: 28 additions & 0 deletions gitlab/gitlab-merge-request-revert.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#如何撤销 Merge Request?
>GitLab 组合了 Git的强大功能可以撤销任何的commit提交。当你需要撤销你之前merge request过的代码,以下就介绍在GitLab上是如何操作

##撤销一个Merge Request
1. 当一个 Merge Request 已经合并了,会出现一个名为 Revert 按钮,让你撤回当前的Merge Request。
![image](src/cherry_pick_changes_mr.png)
1. 按下按钮后,一个弹出会出现,GitLab会让你选择两种不同的方法,方法1:直接撤销该分支(不建议),方法2:新建一个新的merge request[^1]用来提交撤销操作(建议)。
1. 如果上一步选择了新的merge request来执行撤销操作,只需要合并该merge request就完成操作。
### 注意
1. 一旦撤销成功,Revert 按钮就不可再点击
1. 如果目标分支是保护分支(protected branch)直接在目标分支做撤销操作通常是不起作用[^2]

##如何重新提交
1. 由于被撤销合并的分支已经在目标分支合并过,所以继续在被撤销的分支上再有任何的修改,再次提交mr也不会被git侦测到有修改[^3]
1. 解决方法需要把本地的目标分支更新到最新版本,然后拉一个新的分支用于重新提交代码的新分支(例如命名为:re-merge-branch),然后使用命令 git revert (<tree-ish>) (tree-ish提交索引哈希)让该分支回滚到合并目标分支的状态,然后在这分支上进行修改。
1. 修改完成后再按照[基于 Merge Request 的开发流程](gitlab-merge-request.md)里所介绍的步骤重新提交一次mr即可[^4]。








[^1]: Git此时会自动生成一个新的revert分支
[^2]: 此处未经过任何验证,建议使用方法2
[^3]: 原因是被合并分支一旦被撤销,无论该分支的提交记录是最新的,也无法被git判断为新的提交
[^4]: 这个操作未能在GitHub里面成功实现,暂时未有解决方法
57 changes: 56 additions & 1 deletion gitlab/gitlab-merge-request.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
#GitLab提交代码
#基于 Merge Request 的开发流程
> 在 GitLab 上创建的项目,所有人都不应该直接往 master 分支推送代码。而是应该在个人的本地分支(一般fetch master分支)进行开发。并最终通过创建 Merge Request(类似 GitHub pull request)将代码合并到 master 分支。

##使用Merge Request几个好处
1. 方便代码审查,及时发现问题
1. 重要分支设置为受保护(master/staging),杜绝了有些问题代码被提交了,但项目经理不知道的情况;
1. 每个任务都有一个对应的分支,互相隔离,所有的功能提交有据可查;

##开发流程一般步骤

1. 在master拉出新的分支
1. 在新的分支上编写代码
1. 推送修改代码
1. 创建MergeRequest
1. 管理员审核代码与Merge代码

##创建merge Request 具体操作流程
1. 点击左侧导航栏 Merge requests进入页面,点击New merge request
1. 选择需要合并的分支和目标分支
![image](src/merge_request_select_branch.png)
1. 填写合并标题(Title),描述(Description),审核人(Assignee)
![image](src/merge_request_page.png)
1. 当确认无误后,点击 Submit merge request 按钮

### TIPS
GitLab支持WIP标记( Work In Progress),只要在合并标题添加“\[WIP]” 或 “WIP:”开头,Git会自动提醒管理员,当前分支未完成,不能进行合并操作。另外,在提交审查之前,请检查是否有删除WIP标记。
![image](src/wip_blocked_accept_button.png)
![image](src/wip_unmark_as_wip.png)

##管理员审核代码与Merge代码具体操作流程
1. 点击左侧导航栏 Merge requests进入页面
1. 选择其中一个 Merge requests
![image](src/group_merge_requests_list_view.png)
1. 页面下方会有三个tab页面分别为:查看讨论留言(Discussion),查看提交记录(Commits)、查看更改内容(changes)
![image](src/merge_request.png)
1. 查看讨论留言(Discussion)可以快速查看代码审核内提出问题留言
1. 查看提交记录(Commits)可以查看代码提交记录,用于审查分支是否有异常提交
![image](src/squash_mr_commits.png)
1. 查看更改内容(changes)用于审查代码
![image](src/merge_request_diff.png)
1. 当发现代码中有问题时候,管理员可以在相应的位置,点击气泡图案,进行Discussion
![image](src/mr_review_start.png)
1. 而相关的开发同事应该就可以在Discussion Tab中找到问题,与管理员进行讨论或者修复bug,然后再次上传一次commit到该远程分支即可,不用再次提交Merge requests。然后返回到该Discussion点击resolve按钮,表示修复问题。
![image](src/resolve_comment_button.png)
![image](src/resolve_discussion_button.png)
1. 当管理员认为可以进行合并时候,点击 merge 按钮即可完成一次Merge requests。
![image](src/squash_mr_widget.png)

### 注意
1. 如果当前分支与目标分支存在冲突(conflict)是不能够与当前分支进行合并。所以在准备提交之前,如果当前分支出现冲突,请根据情况把当前分支与目标分支先(rebase/merge),在本地解决冲突。因为只有代码作者本人才熟悉代码逻辑结构,解决冲突的责任应由本人操作。
1. 如果当前工程含有CI/CD部署,一旦分支合并就会自动执行部署任务。






Binary file added gitlab/src/cherry_pick_changes_mr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/group_merge_requests_list_view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/logo_gitlab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/merge_request.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/merge_request_diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/merge_request_select_branch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/mr_review_start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/resolve_comment_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/resolve_discussion_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/squash_mr_commits.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/squash_mr_widget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/wip_blocked_accept_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gitlab/src/wip_unmark_as_wip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.