Gitbranchmanagement

Gitbranch is Gitin最强 big functions之一, 它允许你 in 不影响主线Development circumstances under , parallelDevelopment不同 functions or 修复bug. MasterGitbranchmanagement is 成 for Git high 手 关键.

1. branch concepts and 作用

branch is Git仓libraryin 一个独立 Development线, 它允许你 in 不影响otherbranch circumstances under forDevelopment工作. 每个branch都 has 自己 submittinghistory记录.

1.1 branch 作用

2. 查看branch

using git branch commands可以查看当 before 仓library 所 has branch.

# 查看本地branch (当 before branch before has *标记) git branch # 查看所 has branch (including本地 and 远程) git branch -a # 查看远程branch git branch -r # 查看branch 最 after 一次submitting git branch -v # 查看已merge to 当 before branch branch git branch --merged # 查看未merge to 当 before branch branch git branch --no-merged

3. creationbranch

using git branch commands可以creation new branch.

# creation new branch git branch feature-branch # 基于指定submittingcreationbranch git branch feature-branch submitting-hash # 基于远程branchcreation本地branch git branch feature-branch origin/remote-branch

4. 切换branch

using git checkout commands可以切换 to 指定 branch.

# 切换 to 指定branch git checkout feature-branch # creation并切换 to new branch (常用) git checkout -b feature-branch # creation并切换 to 基于远程branch 本地branch git checkout -b feature-branch origin/remote-branch

Notes

in 切换branch之 before , 确保当 before branch 所 has 更改都已submitting or stage, 否则可能会导致更改loss or conflict.

5. mergebranch

using git merge commands可以将一个branch 更改merge to 当 before branch.

# 切换 to 目标branch git checkout master # mergefeature-branchbranch to 当 before branch git merge feature-branch

5.1 fast 进merge (Fast-forward Merge)

当目标branch is 当 before branch 直接祖先时, Git会执行 fast 进merge, 直接将当 before branch指针指向目标branch 最 new submitting.

5.2 三方merge (Three-way Merge)

当目标branch and 当 before branch has 不同 submittinghistory时, Git会执行三方merge, creation一个 new mergesubmitting.

# 禁止 fast 进merge, 始终creationmergesubmitting git merge --no-ff feature-branch

6. deletebranch

using git branch -d commands可以delete已merge branch, using git branch -D commands可以强制delete未merge branch.

# delete已merge branch git branch -d feature-branch # 强制delete未merge branch git branch -D feature-branch # delete远程branch git push origin --delete feature-branch # 另一种delete远程branch 方式 git push origin :feature-branch

7. renamebranch

using git branch -m commands可以renamebranch.

# rename当 before branch git branch -m new-branch-name # rename指定branch git branch -m old-branch-name new-branch-name

8. branchmanagement策略

良 good branchmanagement策略可以improvingDevelopmentefficiency and codequality. 以 under is 几种common branchmanagement策略:

8.1 Git Flow

Git Flow is a经典 branchmanagement策略, 它定义了以 under 几种branchclass型:

8.2 GitHub Flow

GitHub Flow is a simple branchmanagement策略, 适合continuous delivery project:

8.3 GitLab Flow

GitLab Flow is GitHub Flow scale, 结合了environmentbranch and releasebranch:

9. 远程branchmanagement

远程branch is 指store in 远程仓libraryin branch, using git fetch, git push etc.commands可以management远程branch.

# from 远程仓library获取最 new branchinformation git fetch origin # push本地branch to 远程仓library git push -u origin feature-branch # pull远程branch update git pull origin feature-branch # 跟踪远程branch (将本地branch and 远程branch关联) git branch --set-upstream-to=origin/remote-branch local-branch # 取消跟踪远程branch git branch --unset-upstream local-branch

10. branch working principles

in Gitin, branch本质 on is 指向submitting 指针. 当你creation一个branch时, Git只 is creation了一个指向当 before submitting new 指针. 当你 in branch on forsubmitting时, branch指针会自动向 before move.

Git branch is 轻量级 , creation and 切换branch 成本很 low , 这也 is Gitbranchsuch as此强 big and flexible 原因之一.

实践case: usingbranchDevelopment new functions

  1. 确保当 before in master branch: git checkout master
  2. pull最 new code: git pull
  3. creation并切换 to new functionsbranch: git checkout -b feature-login
  4. in new branch on Developmentloginfunctions, for many 次submitting
  5. Developmentcompletion after , 切换回 master branch: git checkout master
  6. pull最 new code: git pull
  7. mergefunctionsbranch to master branch: git merge feature-login
  8. deletefunctionsbranch: git branch -d feature-login
  9. pushmerge after code to 远程仓library: git push

互动练习

请completion以 under 练习, 巩固Gitbranchmanagementoperation:

  1. 查看当 before 仓library 所 has branch
  2. creation一个名 for feature-exercise new branch
  3. 切换 to new creation branch
  4. in new branch on creation一个 new file并submitting
  5. 切换回 master branch
  6. 查看 master branch on is 否 has new creation file
  7. feature-exercise branchmerge to master branch
  8. 再次查看 master branch on is 否 has new creation file
  9. delete feature-exercise branch
  10. creation一个 new branch, for一些modify, 然 after using --no-ff parametermerge to master branch

completion练习 after , 你应该able to熟练MasterGitbranch basicmanagementoperation!