git-merge
1.merge说明
git merge <branch>
把指定branch合并到当前分支
.git
目录下会多出ORIG_HEAD
文件
2.合并操作
2-1.当前分支没有提交
合并 dev分支到master分支时,如果master分支的状态没有被更改过,那么这个合并是非常简单的。 dev分支的历史记录包含master分支所有的历史记录,所以通过把master分支的位置移动到dev的最新分支上,Git 就会合并。这样的合并被称为快进合并(fast-forward)
2-1-1.查看分支情况
2-1-1-1.dev分支日志
dev比master多个
dev_a.txt
和dev_b.txt
提交
git log --oneline --graph
* 0df0755 (HEAD -> dev) add dev_b.txt
* 1b60089 add dev_a.txt
* f8efdee (master) add b.txt
* 40ee907 add a.txt
2-1-1-2.master分支日志
git log --oneline --graph
* f8efdee (HEAD -> master) add b.txt
* 40ee907 add a.txt
2-1-2.合并dev分支
需要先切换到master分支
git merge dev
Updating f8efdee..0df0755
Fast-forward
dev_a.txt | 1 +
dev_b.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 dev_a.txt
create mode 100644 dev_b.txt
2-1-3.查看master分支日志
现在master和dev都指向了最新的
git log --oneline --graph
* 0df0755 (HEAD -> master, dev) add dev_b.txt
* 1b60089 add dev_a.txt
* f8efdee add b.txt
* 40ee907 add a.txt
2-2.当前分支有新的提交
master分支的历史记录有可能在dev分支分叉出去后有新的更新。这种情况下,要把master分支的修改内容和dev分支的修改内容汇合起来。因此,合并两个修改会生成一个提交。这时,master分支的HEAD会移动到该提交上。
2-2-1.查看分支情况
git log --oneline --graph --all
* eabe74b (HEAD -> master) add c.txt
* 8fc52fd master update a.txt
* 270a873 add b.txt
| * e005e0d (dev) add dev.txt
|/
* ea2ca92 add a.txt
2-2-2.合并dev分支
需要先切换到master分支
git merge dev
2-2-3.编辑合并信息
Merge branch 'dev'
* dev:
add dev.txt
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Merge made by the 'recursive' strategy.
dev.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 dev.txt
2-2-4.查看commit信息
git log -1
Merge: 2b18a2a e005e0d
Author: user_name <xxxx@xx.com>
Date: Wed Jul 5 17:40:08 2023 +0800
Merge branch 'dev'
* dev:
add dev.txt
2-2-5.查看日志
git log --oneline --graph --all
* 8384f46 (HEAD -> master) Merge branch 'dev'
|\
| * e005e0d (dev) add dev.txt
* | 2b18a2a add c.txt
* | 8fc52fd master update a.txt
* | 270a873 add b.txt
|/
* ea2ca92 add a.txt
3.合并冲突
3-1.查看分支情况
git log --oneline --graph --all
* 392675b (HEAD -> master) add c.txt
* 8fc52fd master update a.txt
* 270a873 add b.txt
| * 526ed91 (dev) add dev1.txt
| * d3f1159 dev update a.txt
| * e005e0d add dev.txt
|/
* ea2ca92 add a.txt
3-2.合并dev分支(冲突)
需要先切换到master分支
git merge dev
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
3-3.查看状态
git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: dev.txt
new file: dev1.txt
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: a.txt
3-4.修改冲突文件
3-4-1.冲突位置
a
<<<<<<< HEAD
master
=======
dev
>>>>>>> dev
3-4-2.修改完成(都保留)
a
master
dev
3-4-3.把冲突文件添加到暂存区
git add a.txt
3-4-4.再次查看状态
git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: a.txt
new file: dev.txt
new file: dev1.txt
3-5.继续合并
git commit
[master d0fbf37] Merge branch 'dev'
3-6.查看日志
git log --oneline --graph --all
* d0fbf37 (HEAD -> master) Merge branch 'dev'
|\
| * 526ed91 (dev) add dev1.txt
| * d3f1159 dev update a.txt
| * e005e0d add dev.txt
* | 392675b add c.txt
* | 8fc52fd master update a.txt
* | 270a873 add b.txt
|/
* ea2ca92 add a.txt
4.撤销合并
2-1-1.合并
基础上进行撤销merge
4-1.查看reflog日志
git:(master) git reflog
0df0755 (HEAD -> master, dev) HEAD@{0}: merge dev: Fast-forward # 这是刚才合并记录
f8efdee HEAD@{1}: checkout: moving from dev to master # 这个是merge之前的记录
0df0755 (HEAD -> master, dev) HEAD@{2}: commit: add dev_b.txt
1b60089 HEAD@{3}: commit: add dev_a.txt
f8efdee HEAD@{4}: checkout: moving from master to dev
f8efdee HEAD@{5}: commit: add b.txt
40ee907 HEAD@{6}: commit (initial): add a.txt
4-2.撤销
等同于
git reset --hard HEAD~2
git reset --merge f8efdee
4-3.查看log日志
master已经变为了2条记录了
git log --oneline --graph
* f8efdee (HEAD -> master) add b.txt
* 40ee907 add a.txt
4-4.查看reflog日志
git reflog
f8efdee (HEAD -> master) HEAD@{0}: reset: moving to f8efdee
0df0755 (dev) HEAD@{1}: merge dev: Fast-forward
f8efdee (HEAD -> master) HEAD@{2}: checkout: moving from dev to master
0df0755 (dev) HEAD@{3}: commit: add dev_b.txt
1b60089 HEAD@{4}: commit: add dev_a.txt
f8efdee (HEAD -> master) HEAD@{5}: checkout: moving from master to dev
f8efdee (HEAD -> master) HEAD@{6}: commit: add b.txt
40ee907 HEAD@{7}: commit (initial): add a.txt