Git系列:分支创建、切换、合并与冲突解决全攻略

 


Git 分支

分支可以理解为分叉

查看分支

root@osboxes:/home/user/ticgit# git branch
* master

创建分支

root@osboxes:/home/user/ticgit# git branch testing
root@osboxes:/home/user/ticgit# git branch
* master
testing

切换到指定的分支

git checkout testing
Switched to branch 'testing'
root@osboxes:/home/user/ticgit# git branch
master
* testing

查看分叉历史

root@osboxes:/home/user/my_project# git log --oneline --decorate --graph --all
* 3aa3431 (HEAD -> testing, origin/testing) add new file H.c
* f68d527 (master) update tow
* 9951caa (origin/master) amend deleted D.c
* a52ea49 add D.c file
* c017bca deleted B.c
* 9991001 deleted A.c
* 1a22d0d update one
* 0563ce8 add New B.c
* 9369e4b modified A ,add B
| *   66a998d (origin/main, main) Merge branch 'main' of github.com:neonmike/my_project into main
| |\
|/ /
| * c72ebfd Initial commit
* 590f74e initial project version

一个命令创建并且切换到指定分支

git checkout  -b testing2
Switched to a new branch 'testing2'

注意切换分支前:一定保持好一个干净的状态。 有一些方法可以绕过这个问题(即,贮藏(stashing) 和 修补提交(commit amending)), 我们会在 贮藏与清理 中看到关于这两个命令的介绍。

合并分支

合并分支方式,“快进(fast-forward)合并方式。

情况描述:当用户遇到一个问题的时候,就会创建一个testing分支,然后,进行一些列的问题代码处理,处理完成后,会合并到master分支。

就是:对于Master分支,此时没有提交,用户创建一个testing 用于解决问题,问题解决完成,需要合并到主分支。

创建新分支,创建H.c文件用于解决问题,解决完成后,切换会master 分支,合并。

root@osboxes:/home/user/my_project# git branch testing # 建立新分支
root@osboxes:/home/user/my_project# git checkout testing# 切换到新分支
Switched to branch 'testing'
root@osboxes:/home/user/my_project# git branch # 查看分支
main
master
* testing

root@osboxes:/home/user/my_project# touch H.c #创建新文件,解决问题
root@osboxes:/home/user/my_project# echo "this is file H.c" >>H.c
root@osboxes:/home/user/my_project# git add H.c
root@osboxes:/home/user/my_project# git commit -m "add new file H.c" #提交新代码  
root@osboxes:/home/user/my_project# git checkout master ## #切回master分支
Switched to branch 'master'
root@osboxes:/home/user/my_project# git branch
main
* master
testing
testing2
root@osboxes:/home/user/my_project# git merge testing ## 合并分支
Updating f68d527..3aa3431
Fast-forward
H.c | 1 +
1 file changed, 1 insertion(+)
create mode 100644 H.c

由于master 主分支没有更改新提交,因为这种情况下的合并操作没有需要解决的分歧——这就叫做 “快进(fast-forward)”,来实现合并。

合并方式二:解决冲突的合并方式

你在两个不同的分支中,对同一个文件的同一个部分进行了不同的修改,Git 就没法干净的合并它们

假设:master分支,进行了提交,testing2 进行了提交,此时,testing 进行合并需要解决矛盾等问题。

root@osboxes:/home/user/my_project# touch conflict.c 
root@osboxes:/home/user/my_project# echo "this is master branch conflict.c file" >> conflict.c
root@osboxes:/home/user/my_project# git add conflict.c
root@osboxes:/home/user/my_project# git commit -am "commit master branch conflict.c file"
[master 0f75fe3] commit master branch conflict.c file
1 file changed, 1 insertion(+)
create mode 100644 conflict.c
root@osboxes:/home/user/my_project# git checkout testing
Switched to branch 'testing'
Your branch is up to date with 'origin/testing'.
root@osboxes:/home/user/my_project# ll
total 20
drwxr-xr-x 3 root root 4096 Jul 17 14:15 ./
drwxr-xr-x 6 root root 4096 Jul 17 12:55 ../
-rw-r--r-- 1 root root   17 Jul 16 16:42 D.c
-rw-r--r-- 1 root root    0 Jul 16 15:59 E.c
drwxr-xr-x 8 root root 4096 Jul 17 14:15 .git/
-rw-r--r-- 1 root root   17 Jul 17 14:01 H.c
root@osboxes:/home/user/my_project# touch conflict.c
root@osboxes:/home/user/my_project# echo "this is testing branch conflict.c file" >> conflict.c
root@osboxes:/home/user/my_project# git add conflict.c
root@osboxes:/home/user/my_project# git commit -am "commit testing branch conflict.c file"
[testing 3202b88] commit testing branch conflict.c file
1 file changed, 1 insertion(+)
create mode 100644 conflict.c

概述:master 分支创建conflict.c 文件。进行了一次提交,此时,testing 分支创建了conflict.c 文件,进行了一次修改并且进行提交。下面我们进行合并

root@osboxes:/home/user/my_project# git checkout master ## 切换会master分支
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
root@osboxes:/home/user/my_project# git merge testing ## 合并命令
Auto-merging conflict.c
CONFLICT (add/add): Merge conflict in conflict.c
Automatic merge failed; fix conflicts and then commit the result.

合并结果说明:CONFLICT (add/add): Merge conflict in conflict.c Automatic merge failed; fix conflicts and then commit the result.

解决冲突,查看冲突状态

git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)

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 added:     conflict.c

no changes added to commit (use "git add" and/or "git commit -a")

任何因包含合并冲突而有待解决的文件,都会以未合并状态标识出来。 Git 会在有冲突的文件中加入标准的冲突解决标记,这样你可以打开这些包含冲突的文件然后手动解决冲突。

vim conflict.c ## 查看文件
<<<<<<< HEAD
this is master branch conflict.c file
=======
this is testing branch conflict.c file
>>>>>>> testing

冲突文件解释:上半部分是master分支内容,下半部分是testing 分支内容,我们可以选择保留或者自行修改。

上述的冲突解决方案仅保留了其中一个分支的修改,并且 <<<<<<< , ======= , 和 >>>>>>> 这些行被完全删除了。 在你解决了所有文件里的冲突之后,对每个文件使用 git add 命令来将其标记为冲突已解决

root@osboxes:/home/user/my_project# git add conflict.c # 标记解决
root@osboxes:/home/user/my_project# git status ##git status 来确认所有的合并冲突都已被解决
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)

All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)

Changes to be committed:

      modified:   conflict.c

输入 git commit 来完成合并提交。

git commit ## 此时输入合并信息,默认即可,亦可以自己提示一些合并信息

使用git log --oneline --decorate --graph --all 查看分支情况

删除分支部分

删除分支,首先我们要保证分支被合并,这时候表示分支问题已经被处理。

直接删除分支,我们会发现,分支会发现出现警告和错误,

git branch -d testing
warning: not deleting branch 'testing' that is not yet merged to
        'refs/remotes/origin/testing', even though it is merged to HEAD.
error: The branch 'testing' is not fully merged.
If you are sure you want to delete it, run 'git branch -D testing'.

警告和错误信息表示你试图删除的 testing 分支在本地已经合并到当前分支(HEAD),但是它还没有合并到远程分支 refs/remotes/origin/testing。Git 通过这种方式防止你误删未完全合并的分支,以避免潜在的数据丢失。

切换分支,推送同步,删除分支。

git checkout testing
Switched to branch 'testing'
Your branch is ahead of 'origin/testing' by 1 commit.
(use "git push" to publish your local commits)
root@osboxes:/home/user/my_project# git push
Total 0 (delta 0), reused 0 (delta 0)
To github.com:neonmike/my_project.git
  3aa3431..3202b88 testing -> testing
root@osboxes:/home/user/my_project# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
root@osboxes:/home/user/my_project# git branch -d testing
Deleted branch testing (was 3202b88).

注意:尽量不要删除分支,删除操作是一种危险操作。

Git 会尽量阻止不合理的删除行为。比如:对于没有被合并的分支,是不会允许删除的,你可以使用-D选项强制删除,放弃更改。

查看融合到此分支或者没有融合到此分支的分支

主要用于过滤分支,进行删除。

上面描述的选项 --merged--no-merged 会在没有给定提交或分支名作为参数时, 分别列出已合并或未合并到 当前 分支的分支。过滤这个列表中已经合并或尚未合并到当前分支的分支。

root@osboxes:/home/user/my_project# git branch
main
* master
testing2
root@osboxes:/home/user/my_project# git branch --merged
* master
testing2
root@osboxes:/home/user/my_project# git branch --no-merged
main

发表评论

后一页 前一页