Git 分支合併
合併分支
我們已經準備好緊急修復,所以讓我們合併 master 和 emergency-fix 分支。
首先,我們需要切換到 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(-)
由於 emergency-fix 分支直接來自 master,並且在我們在工作期間 master 上沒有其他更改,Git 將其視為 master 的延續。所以它可以“快進”,只需將 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>你好,世界!</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>你好,世界!</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>你好,世界!</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).
現在你對分支和合並的工作原理有了更好的理解。是時候開始使用遠端儲存庫了!