Menu
×
   ❮     
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>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>这是我的新 Git 仓库中的第一个文件。</p>
<p>我们在文件中添加了一行新内容!</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>这是我的新 Git 仓库中的第一个文件。</p>
<<<<<<< HEAD
<p>此行在这里是为了显示合并是如何工作的。</p>
=======
<p>我们在文件中添加了一行新内容!</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>这是我的新 Git 仓库中的第一个文件。</p>
<p>此行在这里是为了显示合并是如何工作的。</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

开始练习


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.