14-git branch 分支

知识前提:git 重要的三个工作区域:

  1. 工作区(Working Directory):写代码的目录。就是项目代码存放的目录。
  2. 暂存区(index/stage):工作区与版本库之间的缓冲地带。用 git add 把文件添加进去,实际上就是把文件修改添加到暂存区。
  3. 版本库(仓库区):git commit提交更改,实际上是把暂存区的所有内容全部提交到当前分支,查看记录 git log。

git branch 语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$ git branch
usage: git branch [<options>] [-r | -a] [--merged] [--no-merged]
or: git branch [<options>] [-l] [-f] <branch-name> [<start-point>]
or: git branch [<options>] [-r] (-d | -D) <branch-name>...
or: git branch [<options>] (-m | -M) [<old-branch>] <new-branch>
or: git branch [<options>] (-c | -C) [<old-branch>] <new-branch>
or: git branch [<options>] [-r | -a] [--points-at]
or: git branch [<options>] [-r | -a] [--format]

Generic options
-v, --verbose show hash and subject, give twice for upstream branch
-q, --quiet suppress informational messages
-t, --track set up tracking mode (see git-pull(1))
-u, --set-upstream-to <upstream>
change the upstream info
--unset-upstream unset the upstream info
--color[=<when>] use colored output
-r, --remotes act on remote-tracking branches
--contains <commit> print only branches that contain the commit
--no-contains <commit>
print only branches that don't contain the commit
--abbrev[=<n>] use <n> digits to display object names

Specific git-branch actions:
-a, --all list both remote-tracking and local branches
-d, --delete delete fully merged branch
-D delete branch (even if not merged)
-m, --move move/rename a branch and its reflog
-M move/rename a branch, even if target exists
-c, --copy copy a branch and its reflog
-C copy a branch, even if target exists
-l, --list list branch names
--show-current show current branch name
--create-reflog create the branch's reflog
--edit-description edit the description for the branch
-f, --force force creation, move/rename, deletion
--merged <commit> print only branches that are merged
--no-merged <commit> print only branches that are not merged
--column[=<style>] list branches in columns
--sort <key> field name to sort on
--points-at <object> print only branches of the object
-i, --ignore-case sorting and filtering are case insensitive
--format <format> format to use for the output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 列出所有分支
git branch -a
git branch -av # 比-a详细


# 创建分支 dev
git branch dev


# 切换分支 dev,如果不存在分支会直接创建
git checkout dev
# 本地创建分支与远程分支相连接 并切换到本地分支
git checkout -b rights(本地分支名) origin/ rights(远程分支名)


# 拉取分支dev的代码
git remote add origin 你的git远程地址 # 复制的远程地址
git pull origin dev


# 合并分支bug 到当前分支
git merge bug


# 删除bug分支
git branch -d bug

14-git branch 分支
https://flepeng.github.io/044-Git-21-命令-14-git-branch-分支/
作者
Lepeng
发布于
2021年3月8日
许可协议