git分支

分支

分支就是一个指针,跟随者最新提交。

查看分支

查看本地分支:

1
2
$ git branch
* master

查看远端仓库分支:

1
2
3
4
5
6
7
8
$ git branch -a
* hexo
master
remotes/origin/HEAD -> origin/hexo
remotes/origin/dependabot/npm_and_yarn/acorn-6.4.1
remotes/origin/dependabot/npm_and_yarn/https-proxy-agent-2.2.4
remotes/origin/hexo
remotes/origin/master

查看每个分支的最后一次提交:

1
2
3
4
 jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b2)
$ git branch -v
* b2 86a66e5 1.txt
master 86a66e5 1.txt

创建分支

基于当前最后一次提交创建:

1
2
3
4
5
$ git branch b1

$ git branch
b1
* master

指定基于某个提交创建分支:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ git log --oneline
2137bd7 (HEAD -> master) 3.txt
7c5ea26 2.txt
86a66e5 (b1) 1.txt
8502169 msg

# 基于2.txt这个提交对象来创建b2分支
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ git branch b2 7c5ea26

# 成功切换,可以看到b2的最后一次提交未2.txt
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ git branch -v
b1 86a66e5 1.txt
b2 7c5ea26 2.txt
* master 2137bd7 3.txt

切换分支

1
2
3
4
5
6
7
8
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ git checkout b1
Switched to branch 'b1'
-
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b1)
$ git branch
* b1
master

还可以创建和切换一步完成

1
2
3
4
5
6
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b1)
$ git checkout -b b2
Switched to a new branch 'b2'

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b2)
$

注意:

切换分支时,尽量在切换之前保持当前分支是干净的,也就是该暂存的暂存,该提交的提交,最好是提交后再切换。

删除分支

删除本地分支:

删除一个分支时,不能处于这个分支,只能切换到另一个分支才能把它删除

1
2
3
4
5
6
7
8
9
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b2)
$ git branch
b1
* b2
master

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b2)
$ git branch -d b1
Deleted branch b1 (was 86a66e5).

强制删除:

当我们创建分支产生了提交信息时,但是还没有往主分支合并,删除时就会失败提示,可以强制删除。

1
2
3
4
5
6
7
8
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ git branch -d b2
error: The branch 'b2' is not fully merged.
If you are sure you want to delete it, run 'git branch -D b2'.

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ git branch -D b2
Deleted branch b2 (was ef5e3ea).

删除远程分支:

1
git push origin --delete remotes/origin/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
jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b4)
$ ls
1.txt 2.txt 3.txt 4.txt

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b4)
$ cat 4.txt
50%
100%

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b4)
$ ls
1.txt 2.txt 3.txt 4.txt

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (b4)
$ git checkout master
Switched to branch 'master'

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ ls
1.txt 2.txt 3.txt

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ git merge b4
Updating 2137bd7..004004e
Fast-forward
4.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 4.txt

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ ls
1.txt 2.txt 3.txt 4.txt

jw199@DESKTOP-2OG3D2D MINGW64 /c/my_code/git_study (master)
$ cat 4.txt
50%
100%

我在b4分支创建了4.txt。b4分支提交后,切换到master分支,此时并没有4.txt文件。用merge命令,把b4分支的内容合并到当前的master分支,合并完之后查看4.txt存在。

/