Git的分支操作
什么是分支
在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用)
Git分支查看&创建&切换
命令名称 | 作用 |
---|---|
git branch 分支名称 | 建立分支 |
git branch -v | 查看分支 |
git chekot 分支名称 | 切换分支 |
git merge 分支名称 | 把指定分支合并到当前的分支 |
查看分支
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git branch -v
- master f13b3f1 my first commit
建立分支
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git branch hod-fix
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git branch -v
hod-fix f13b3f1 my first commit
- master f13b3f1 my first commit
切换分支
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git checkout hod-fix
Switched to branch 'hod-fix'
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (hod-fix)
$
:::tips
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (hod-fix)
$ vi hello.txt #在这个分支上进行文件的修改
$ git status #查看状态
On branch hod-fix
Changes not staged for commit:
(use "git add
(use "git restore
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (hod-fix)
$ git commit -m "hod-fix commit frist" hello.txt #上传本地库
[hod-fix d936e22] hod-fix commit frist
1 file changed, 1 insertion(+), 1 deletion(-)
$ git reflog #查看信息
d936e22 (HEAD -> hod-fix) HEAD@{0}: commit: hod-fix commit frist
f13b3f1 (master) HEAD@{1}: checkout: moving from master to hod-fix
f13b3f1 (master) HEAD@{2}: reset: moving to f13b3f1
50b9dfa HEAD@{3}: reset: moving to 50b9dfa
50b9dfa HEAD@{4}: commit: my second commit
f13b3f1 (master) HEAD@{5}: commit (initial): my first commit
分支合并
git merge 分支名称
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git merge hod-fix 合并到master
Updating f13b3f1..d936e22
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
分支冲突合并
冲突产生的原因:
合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git无法替我们决定使用哪一个。必须人为决定新代码内容。
在master 分支对文件进行修改并上传
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ vi hello.txt
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git add hello.txt
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git commit -m "master txt" hello.txt
[master 73fe697] master txt
1 file changed, 3 insertions(+)
切换分支 hod-fix,对文件修改并上传
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git checkout hod-fix
Switched to branch 'hod-fix'
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (hod-fix)
$ vi hello.txt
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (hod-fix)
$ git add hello.txt
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (hod-fix)
$ git commit -m "hod-fix" hello.txt
[hod-fix 2a8be36] hod-fix
1 file changed, 2 insertions(+)
切换回主分支进行合并,会发现冲突
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (hod-fix)
$ git checkout master
Switched to branch 'master'
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git merge hod-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (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
both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
查看hello文件
$ cat hello.txt
hello Git 111
<<<<<<< HEAD
master txt
hod-fix
hod-fix
对hello文件进行修改,修改掉特殊的相关的字符
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master|MERGING)
$ vi hello.txt
$ cat hello.txt
hello Git 111:
master txt
hod-fix
对文件进行上传
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master|MERGING)
$ git add hello.txt
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master|MERGING)
$ git commit -m"merge test" #不可带文件名称
[master fcc6441] merge test #只会修改master分支的数据
创建分支和切换分支图解
master、hot-fix其实都是指向具体版本记录的指针。当前所在的分支,其实是由HEAD决定的。所以创建分支的本质就是多创建一个指针。
HEAD 如果指向master,那么我们现在就在master 分支上。
HEAD如果执行hotfix,那么我们现在就在hotfix分支上。
所以切换分支的本质就是移动HEAD指针。
标签:hod,Git,fix,git,master,操作,分支
From: https://www.cnblogs.com/yutoujun/p/16915129.html