git 分支合并
合并分支
我们已经准备好紧急修复,所以让我们合并主分支和紧急修复分支。
首先,我们需要切换到master分支:
示例
git checkout master
Switched to branch 'master'
现在我们将当前分支(master)与 Emergency-fix 合并:
示例
git merge emergency-fix
Updating 09f4acd..dfa79db
Fast-forward
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
由于紧急修复分支直接来自 master,并且在我们工作时没有对 master 进行其他更改,因此 Git 将此视为 master 的延续。因此它可以 "Fast-forward",只需将 master 和 Emergency-fix 指向同一个提交即可。
由于master和emergency-fix现在本质上是相同的,我们可以删除emergency-fix,因为不再需要它:
示例
git branch -d emergency-fix
Deleted branch emergency-fix (was dfa79db).
合并冲突
现在我们可以转向 hello-world-images 并继续工作。添加另一个图片文件(img_hello_git.jpg)并更改index.html,以便它显示:
示例
git checkout hello-world-images
Switched to branch 'hello-world-images'
示例
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>
<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<p>A new line in our file!</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>
</body>
</html>
现在,我们已经完成了这里的工作,可以暂存并提交该分支:
示例
git add --all
git commit -m "added new image"
[hello-world-images 1f1584e] added new image
2 files changed, 1 insertion(+)
create mode 100644 img_hello_git.jpg
我们看到两个分支中的index.html 都已更改。现在我们准备将 hello-world-images 合并到 master 中。但是我们最近在 master 中所做的改变会发生什么呢?
示例
git checkout master
git merge hello-world-images
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
合并失败,因为 index.html 的版本之间存在冲突。让我们检查一下状态:
示例
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: img_hello_git.jpg
new file: img_hello_world.jpg
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
这确认了 index.html 中存在冲突,但图片文件已准备好并暂存以供提交。
所以我们需要解决这个冲突。在我们的编辑器中打开文件:
示例
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>
<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<<<<<<< HEAD
<p>This line is here to show how merging works.</p>
=======
<p>A new line in our file!</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>
>>>>>>> hello-world-images
</body>
</html>
我们可以看到版本之间的差异并按我们想要的方式编辑它:
示例
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>
<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<p>This line is here to show how merging works.</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>
</body>
</html>
现在我们可以暂存index.html并检查状态:
示例
git add index.html
git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
new file: img_hello_git.jpg
new file: img_hello_world.jpg
modified: index.html
冲突已经修复,我们可以使用commit来结束合并:
示例
git commit -m "merged with hello-world-images after fixing conflicts"
[master e0b6038] merged with hello-world-images after fixing conflicts
并删除 hello-world-images 分支:
示例
git branch -d hello-world-images
Deleted branch hello-world-images (was 1f1584e).
现在您对分支和合并的工作原理有了更好的了解。是时候开始使用远程存储库了!