git分支操作
-
查看分支:git branch -v
$ git branch -v * master 9e2aa44 second commit
*代表当前所在分支
2.创建分支 git branch 分支名
$ git branch hot-fix
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git branch -v
hot-fix 9e2aa44 second commit
* master 9e2aa44 second commit
3.切换分支 git checkout 分支名
$ git checkout hot-fix
Switched to branch 'hot-fix'
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git branch -v
* hot-fix 9e2aa44 second commit
master 9e2aa44 second commit
在切换的新分支hot-fix上编写代码后,要提交到本地库
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git status
On branch hot-fix
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git commit -m "在新的分支上编写代码" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[hot-fix b593135] 在新的分支上编写代码
1 file changed, 1 insertion(+)
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git status
On branch hot-fix
nothing to commit, working tree clean
4.合并分支(正常合并)git merge 分支名 (把指定分支合并到当前分支上) 通俗点讲如果要把hot-fix合并到master上要先切换分支到master上
$ git checkout master
Switched to branch 'master'
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git merge hot-fix
Updating 9e2aa44..b593135
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
5.合并分支(冲突合并)
冲突产生的原因:
合并分支时,两个分支对同一个位置(这里的同一个位置指的是同一个文件)(两同)作出修改,git无法决定使用哪一个,必须人为决定
(1)先修改hot-fix分支的hello.txt的内容,在hello.txt文件最后一行作出改动,然后提交到本地库
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ vim hello.txt
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (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@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git add hello.txt
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git commit -m "hot-fix修改" hello.txt
[hot-fix ce5e34d] hot-fix修改
1 file changed, 1 insertion(+), 1 deletion(-)
g
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git status
On branch hot-fix
nothing to commit, working tree clean
(2)切回master分支,再先修改master分支的hello.txt的内容,在hello.txt文件最后一行作出改动,然后提交到本地库
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git checkout master
Switched to branch 'master'
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ vim hello.txt
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git status
On branch master
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@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git add hello.txt
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git commit -m "master修改" hello.txt
[master a77710b] master修改
1 file changed, 1 insertion(+), 1 deletion(-)
(3)合并分支 出现冲突
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (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.
(4)人为解决冲突
手动打开冲突的文件
备注:
<<<<<<< HEAD
xxxxxxxxxxxxxxx
=======
中的内容指的是当前分支的内容
而绿色框中指的被合并分支的内容
(5)解决完冲突后要再次git add 和 git commit -m “备注” (注意 git commit -m “备注” 后面不能再跟文件名)
如果 git commit -m “备注” 后面跟了文件名会出现错误:
合并只会修改master分支的内容,并不会修改被合并(hot-fix)分支的内容,切回hot-fix分支查看hello.txt文件没有发生改变