選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

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).

現在你對分支和合並的工作原理有了更好的理解。是時候開始使用遠端儲存庫了!

透過練習來測試自己

練習

hello-you 分支與當前分支合併

git  hello-you

開始練習


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援