Git04-分支重要操作

258 阅读2分钟

1、分支进阶

删除分支

#删除分支
git branch -d 分支名
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git branch new_branch2
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git branch -d new_branch2
Deleted branch new_branch2 (was 573cf00).

强制删除分支,因为可能该分支上有一些修改没有加入

#强制删除分支
git branch -D 分支名

合并代码

#创建一个分支new branch,然后新建一个文件test3.txt,并提交到本地版本库
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ git add .
warning: LF will be replaced by CRLF in test3.txt.
The file will have its original line endings in your working directory
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ git status
On branch new_branch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   .gitignore
        new file:   dir01/test3.txt
        new file:   test3.txt
​
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ git commit -m "new branch add test3.txt"
[new_branch feb5149] new branch add test3.txt
 3 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 dir01/test3.txt
 create mode 100644 test3.txt
​
#切换会master分支,并且合并new branch 分支上做的修改
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git merge new_branch
Updating 573cf00..feb5149
Fast-forward
 .gitignore      | 2 +-
 dir01/test3.txt | 0
 test3.txt       | 1 +
 3 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 dir01/test3.txt
 create mode 100644 test3.txt

注意git merge new_branch的意思是讲new_branch上的修改合并到master分支里。

查看最近3次提交的log,看到HEAD指向了master和new_branch分支:

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git log -3
commit feb514987dd466d081cc5aa980dc2bac0464f2e4 (HEAD -> master, new_branch)
Author: Oliver <16140@qq.com>
Date:   Fri Jun 3 11:25:26 2022 +0800
​
    new branch add test3.txt
​
commit 573cf001ec7c70254df1234d4c3f8a2e6855f814
Author: Oliver <16140@qq.com>
Date:   Fri Jun 3 10:30:53 2022 +0800
​
    ignore *.b, add a.b
​
commit c22a20fe740a58641cb161d11d10b8abb12b3dd9
Author: Oliver <16140@qq.com>
Date:   Fri Jun 3 10:15:41 2022 +0800
​
    add .gitignore

commit链就是一条工作记录线:

image.png

演示一下合并冲突

主分支修改conflict.txt文件

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vim conflict.txt
#第一行写上hello master
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add .
warning: LF will be replaced by CRLF in conflict.txt.
The file will have its original line endings in your working directory
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   conflict.txt
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit -m "master fix confict.txt"
[master cfabbb2] master fix confict.txt
 1 file changed, 1 insertion(+)
 create mode 100644 conflict.txt

new branch分支修改conflict.txt文件

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout -
Switched to branch 'new_branch'
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ vim conflict.txt
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ git add .
warning: LF will be replaced by CRLF in conflict.txt.
The file will have its original line endings in your working directory
git
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ git status
On branch new_branch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   conflict.txt
​
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ git commit -m "new branch fix conflict.txt"
[new_branch c649328] new branch fix conflict.txt
 1 file changed, 1 insertion(+)
 create mode 100644 conflict.txt

切换到主分支合并,发现有代码冲突:

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch)
$ git checkout -
Switched to branch 'master'
​
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git merge new_branch
CONFLICT (add/add): Merge conflict in conflict.txt
Auto-merging conflict.txt
Automatic merge failed; fix conflicts and then commit the result.

进入文件可以看出:

<<<<<<< HEAD
hello master
=======
hello new branch
>>>>>>> new_branch

<<<HEAD到=====就是指当前的分支,=====到>>>>指的就是和当前分支冲突的分支,那么看看如何修改即可。

看一下分支的指针是如何变化的

  • 下面就如同执行了git checkout -b dev指令,让HEAD指向了dev:

image.png

再记一遍:HEAD指向的是分支,master和dev指向的是提交。

可以查看一下HEAD:进入.git文件的HEAD

记录的是ref: refs/heads/master,也就是指向了master,切换就指向了ref: refs/heads/new_branch

现在在dev上再次进行提交:

image.png

再把dev的提交合并到master上: image.png