Git 分支应用 - 修复Bug & 增加新功能

## 切换分支1

$ git checkout -b test  # 创建并切换
$ git switch master          # git 2.23 之后
$ git switch -c dev 

checkout

-b
Create a new branch named and start it at ; see git-branch(1) for details.
-B
Creates the branch and start it at ; if it already exists, then reset it to . This is equivalent to running “git branch” with “-f”; see git-branch(1) for details.

switch

-c , --create
Create a new branch named starting at before switching to the branch. This is a convenient shortcut for:

  • $ git branch
  • $ git switch

暂存内容 git stash

在 dev 分支未开发完成,不必要commit 时,如果需要切换分支,需要将当前内容暂时存起来。

stash 文档内容如下:

SYNOPSIS
       git stash list []
       git stash show [] []
       git stash drop [-q|--quiet] []
       git stash ( pop | apply ) [--index] [-q|--quiet] []
       git stash branch  []
       git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
                    [-u|--include-untracked] [-a|--all] [-m|--message ]
                    [--pathspec-from-file= [--pathspec-file-nul]]
                    [--] [...]]
       git stash clear
       git stash create []
       git stash store [-m|--message ] [-q|--quiet] 
$ git stash [push]
 Save your local modifications to a new stash entry and roll them back to HEAD (in the working tree and in the index). The 
           part is optional and gives the description along with the stashed state.
 For quickly making a snapshot, you can omit "push". In this mode, non-option arguments are not allowed to prevent a misspelled
           subcommand from making an unwanted stash entry. The two exceptions to this are stash -p which acts as alias for stash push -p and
           pathspec elements, which are allowed after a double hyphen -- for disambiguation.
           
$ git stash list 
list []
           List the stash entries that you currently have.
           
$ git stash pop
pop [--index] [-q|--quiet] []
           Remove a single stashed state from the stash list and apply it on top of the current working tree state, i.e., do the inverse
           operation of git stash push. The working directory must match the index.

           Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by
           hand and call git stash drop manually afterwards.
           
$ git stash apply
 apply [--index] [-q|--quiet] []
           Like pop, but do not remove the state from the stash list. Unlike pop,  may be any commit that looks like a commit created by
           stash push or stash create.

## 合并分支2

git merge --ff / --no-ff / --ff-only

--ff, --no-ff, --ff-only
    Specifies how a merge is handled when the merged-in history is already a descendant of the current history. --ff is the default unless merging an annotated (and possibly signed) tag that is not stored in its natural place in the refs/tags/ hierarchy, in which case --no-ff is assumed.

    With --ff, when possible resolve the merge as a fast-forward (only update the branch pointer to match the merged branch; do not create a merge commit). When not possible (when the merged-in history is not a descendant of the current history), create a merge commit.

    With --no-ff, create a merge commit in all cases, even when the merge could instead be resolved as a fast-forward.
    
    With --ff-only, resolve the merge as a fast-forward when possible. When not possible, refuse to merge and exit with a non-zero status.

修复Bug过程

当前在 dev 开发, 需要在 master 上 修复Bug
1.保存当前修改内容
2.切换master分子
3.创建修复Bug分支
4.修复Bug
5.切换主分支
6.合并
7.切换dev 分支
8.恢复暂存的内容

1. git stash 
2. git checkout master 
3. git checkout -b issue-101  
4. # 修复Bug...
5. git switch master
6. git merge --no-ff -m "merged bug fix 101" issue-101
7. git switch dev
8. git stash list
9. git stash pop
## Bug 修复完成

添加新功能过程

为了防止把主分支内容搞乱,每添加一个新功能,最好新建一个feature分支
1.创建新功能分支
2.开发内容
3.1 合并到 开发分支
3.2 删除新开发内容

# 当前 master 分支
1. git switch -c feature-xx
2. # 开发...
3. git merge --no-ff -m "merged feature" dev
4. git branch -d[-D] feature-xx # 在未合并的情况下,使用 D 强制删除分支
  1. 工具系列 | git checkout 可替换命令 git switch 和 git restore ↩︎
  2. git merge和git merge --no-ff的区别

你可能感兴趣的:(Git 分支应用 - 修复Bug & 增加新功能)