git分支
1. 什么是分支
在版本控制过程中,需要同时推进多个任务,为每个任务,就可以创建每个任务单独的分支。使用分支就意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。(分支底层也是指针的引用)
2. 分支的好处
- 同时并行推进多个功能开发,提高开发效率
- 各个分支在开发过程中,如果某一个分支开发失败,不会对其它分支有任何影响。失败的分支删除重新开始即可
3.分支的操作
命令 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
3.1 创建分支
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git branch hot_fix
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git branch -v
hot_fix ceb62cd third commit
* master ceb62cd third commit
3.2 切换分支
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git checkout hot_fix
Switched to branch 'hot_fix'
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (hot_fix)
$ git branch -v
* hot_fix ceb62cd third commit
master ceb62cd third commit
3.3 修改分支
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (hot_fix)
$ vim hello.txt
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (hot_fix)
$ git status
On branch hot_fix
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (hot_fix)
$ git add hello.txt
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (hot_fix)
$ git commit -m "hot_fix first commit" hello.txt
[hot_fix 6c799a6] hot_fix first commit
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (hot_fix)
$ cat hello.txt
hello git!
ppppppppp
ppppppppp
yypy aaaaaaaq
qa
12234 qweasd
this is hot_fix branch
一般修改之后也要进行保存暂存区,提交本地库等操作,同时在本地库的hello.txt也完成了对应的修改。查看git状态如下:
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (hot_fix)
$ git reflog
6c799a6 (HEAD -> hot_fix) HEAD@{0}: commit: hot_fix first commit
ceb62cd (master) HEAD@{1}: checkout: moving from master to hot_fix
ceb62cd (master) HEAD@{2}: reset: moving to ceb62cd
7a4a133 HEAD@{3}: reset: moving to 7a4a133
ceb62cd (master) HEAD@{4}: commit: third commit
b529e94 HEAD@{5}: commit: second commit
7a4a133 HEAD@{6}: commit: first commit
1f49b3c HEAD@{7}: commit (initial): first commit
可以看到此时多了一行hot_fix first commit的状态,如果需要进行版本切换穿梭,还是执行 “git reset --hard 版本号” 操作即可
3.4 合并分支(正常合并)
使用命令 “git merge 分支名” 即可将指定的分支合并到当前分支上
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git merge hot_fix
Updating ceb62cd..6c799a6
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ cat hello.txt
hello git!
ppppppppp
ppppppppp
yypy aaaaaaaq
qa
12234 qweasd
this is hot_fix branch
3.5 合并分支(冲突合并)
在合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git无法替我们决定使用哪一个时,就会产生代码冲突,此时需要人为确定。
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git merge hot_fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master|MERGING)
$
此时可以发现报错,说产生冲突,同时状态也切换为了 (master|MERGING) ,接下来利用vim查看文件,可以看到git已经将代码的区别标明了
hello git!
ppppppppp
ppppppppp
yypy aaaaaaaq
qa
12234 qweasd
this is hot_fix branch
<<<<<<< HEAD
I want to merge in master
=======
I want to merge in hot_fix
>>>>>>> hot_fix
此时需要手工的将需要的保留,不需要的删除。比如这里直接将第8、10、12行删除,然后保存退出。就说明两个分支的文件的更改都保留
接下来需要保存至暂存区、提交至本地库。注意这里提交至本地库一定不要带上文件名。
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master|MERGING)
$ vim hello.txt
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master|MERGING)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master|MERGING)
$ git add hello.txt
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master|MERGING)
$ git commit -m "merge branch"
[master 00745af] merge branch
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ cat hello.txt
hello git!
ppppppppp
ppppppppp
yypy aaaaaaaq
qa
12234 qweasd
this is hot_fix branch
I want to merge in master
I want to merge in hot_fix
同时查看hot_fix分支的hello.txt可以发现并未更改。说明合并分支时只会更改合并到的当前分支,而不会更改要合并的分支。
master,hot-fix其实都是指向具体版本记录的指针:当前所在的分支,其实是由HEAD决定的。所以创建分支的本质就是多创建一个指针。HEAD如果指向master,那么我们现在就在master分支上。HEAD如果执行houlix,那么我们现在就在houlix分支上。