菜单
×
   ❮     
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 提供支持