Git 合并简化:像专业人士一样合并分支

合并 Git 中的分支有时可能是一项令人望而却步的任务,但 git merge 命令显著简化了这个过程。在本教程中,您将学习使用 git merge 结合 GitHub(一款领先的项目管理工具)的分支合并的基础知识。

让我们开始这段关于 Git 和 GitHub 的旅程,重点介绍强大的 git merge 命令。

掌握 Git 合并的先决条件

在深入研究 git merge 的复杂性之前,请确保您具备以下设置:

  • Git(本教程使用版本 2.25.1)- 从 Git 官方网站 下载。
  • A GitHub account – Sign up at GitHub.
  • Ubuntu 机器(这里使用的是 Ubuntu 20.04,但任何带有 Git 的操作系统都可以)。

为 Git 合并初始化项目

要有效地使用 git merge,您首先需要一个 Git 项目。让我们为练习设置一个演示项目。

1. 首先创建一个名为 ~/git-demo 的新目录,并进入其中:

mkdir git-demo && cd git-demo

2. 在 ~/git-demo 目录中创建一个 index.html 文件,并添加以下内容。这个基本的 HTML 文件显示了关于开始使用 git merge 的消息。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Git Merge Demo</title>
</head>
<body>
    <h4>Let's get started with git merge</h4>
</body>
</html>

3. 在您的项目目录中初始化一个新的 Git 仓库:

git init命令用于将现有的未版本化项目转换为Git存储库,或者使用Git开始一个新项目。

git init
Initializing a Git repository for git merge

4. 确认在项目目录中创建了.git文件夹。Git将在此文件夹中跟踪更改:

ls -la
Verifying the existence of the GIT repository in the directory.

5. 使用git add命令将更改内容加入到项目中。通过将index.html文件加入暂存区,您为Git工作流程中的下一次提交做好了准备。

git add index.html

6. 使用git status确认您的文件已正确加入暂存区。此命令提供了当前工作目录状态的快照。

git status
Confirming the staging of changes for git merge.

7. 接下来,使用git commit提交您的更改。此命令将您的暂存更改保存到本地存储库中,标志着您版本历史中的一个重要步骤。

git commit -m "initial commit"
Executing the initial commit in preparation for git merge.

如果是第一次使用Git,请设置您的Git身份信息:git config --global user.email "[email protected]"git config --global user.name "Your Name"

8. 使用git log查看提交历史记录。此命令有助于您跟踪更改,也是准备进行git merge时的重要步骤。

git log
Checking commit history before performing git merge.

9. 使用git branch确认存储库中存在的分支。在执行git merge之前,这一步至关重要。

注意:在最新的Git版本中,默认分支名为“main”,取代了“master”。

git branch
Listing all branches in preparation for git merge.

为Git合并创建分支

完成初始的Git设置并进行了第一次提交后,现在是创建一个新分支的时候了。这个分支将用于有效演示git merge过程。

1. 更新index.html文件的内容,以演示您分支中的更改。这个版本包括用户输入字段,为演示git merge做准备。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Git Merge Demo</title>
</head>
<body>
    <h4>Enhancing Git Merge Skills</h4>
    <form action="">
      <input type="text" name="username" placeholder="Username" />
      <input type="password" name="password" placeholder="Password" />
    </form>
</body>
</html>

2. 使用git status检查存储库的状态,查看index.html中的更改。它应该指示文件已修改且未暂存,准备好进行git merge过程的下一步。

Inspecting repository changes for git merge.

3. 使用git checkout -b form创建一个名为“form”的新分支。此命令不仅创建了新分支,还将您的工作分支切换到“form”,这对于实现git merge是至关重要的。

git checkout -b form
Creating a new branch for git merge.

4. 运行git branch确认新分支的创建。现在您应该能看到“form”和“master”两个分支,“form”作为活动分支,用星号(*)表示。

git branch
Confirming the active ‘form’ branch for git merge.

5. 将更改暂存并提交到form分支。此操作确保form分支反映了对index.html文件的最新更新,为有效的git merge做好准备。

## 准备更新的文件
git add index.html

## 提交新的更改
git commit -m "Added user input fields to form"
Committing updated index.html to the ‘form’ branch for git merge.

使用 Git Merge 进行本地分支合并

现在您在 GitHub 存储库中有不同的分支,下一步是合并它们。本节重点介绍使用 git merge 命令在本地合并分支,这是 Git 的基本操作之一。

1. 开始合并,切换到 master 分支,该分支将接收来自 form 分支的更新:

git checkout master

2. 一旦在 master 分支上,执行 git merge formform 分支的更改合并到 master。这是 git merge 过程中的关键步骤。

git merge form
Successfully merging branches using git merge.

3. 确认合并,确保您在 master 分支上,并检查 index.html 的内容是否已更新。

# 确认当前分支
git branch
# 检查已合并的内容
cat index.html
Reviewing index.html content post git merge.

使用 Git Squash 进行流程优化

在 Git 中进行压缩是一种清理提交历史的技术。当您有多个不需要单独保留的提交消息时,压缩允许您将这些提交合并为一个全面的提交。在准备进行 git merge 时,这种做法特别有用。

让我们通过对 form 分支的 index.html 文件进行进一步更改,然后压缩这些更改来探索压缩过程。

1. 切换回 form 分支以进行额外更改:

git checkout form

2. 通过在 index.html 中添加表单标题来修改。此步骤将创建一个稍后在合并时压缩的更改:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h4>Mastering Git Merge with Form Updates</h4>
    <form action="">
      <h4>Account Details</h4>
      <input type="text" name="username" placeholder="Username" />
      <input type="password" name="password" placeholder="Password" />
      <button>Submit</button>
    </form>
</body>
</html>

3. 使用清晰的消息对进行的更新进行分期和提交:

git add index.html
git commit -m "Enhanced form with a header for git merge"
Staging and committing changes for squashing in git merge.

4. 通过在 form 分支中添加一个新的输入字段来增强 index.html。此修改是为了为有效的 git merge 做准备的一部分。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Git Merge Example</title>
</head>
<body>
    <h4>Practicing Git Merge</h4>
    <form action="">
      <h4>Enter account details</h4>
      <input type="text" name="fullname" placeholder="Full Name" />
      <input type="text" name="username" placeholder="Username" />
      <input type="password" name="password" placeholder="Password" />
      <button>Submit</button>
    </form>
</body>
</html>

5. 使用明确的消息对这些更改进行分期和提交,确保你的工作中的每个步骤都清楚地记录在即将进行的 git merge 中。

git add index.html
git commit -m "Enhanced form with an additional input field"
Adding another input field to index.html for git merge.

6. 接下来,在 index.html 中的表单下插入一个段落元素,进一步修改文件以准备进行 git merge

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Git Merge Practice</title>
</head>
<body>
    <h4>Let's get started with git merge</h4>
    <form action="">
      <h4>Enter account details</h4>
      <input type="text" name="fullname" placeholder="Full Name" />
      <input type="text" name="username" placeholder="Username" />
      <input type="password" name="password" placeholder="Password" />
      <button>Submit</button>
    </form>
    <p>Already a user?</p>
</body>
</html>

7. 再次进行分期和提交更改,使用一个捕捉这次更新本质的消息,对于 git merge 过程至关重要。

git add index.html
git commit -m "Added user status paragraph to form"
Further modifying index.html for git merge.

8. 利用交互式模式的 git rebase 查看和编辑最近的提交。这一步在压缩提交以进行干净的 git merge 过程中非常重要。

git rebase -i HEAD~3

9. 在交互模式中,选择 reword 第一个提交,并 squash 后续提交。这个动作 cons 这你的提交历史,使你的 git merge 更加清洁和有序。

Preparing commits for squashing in git merge.

10. 在重新编辑并保存后,更新汇总的提交消息以反映累积的更改。这一步骤完成了压缩过程,为你提供了一个简化的 git merge

Finalizing the commit message for git merge.

确认您的压缩操作成功,这是处理多个提交消息时git merge过程的一个重要部分。

Successful git squash operation.

对于同时压缩和合并,使用git merge --squash。此技术将表单分支高效合并并压缩到分支中,有效地合并了提交。

git merge --squash form

执行git merge --squash命令将form分支的更改合并到master中,同时将所有提交压缩为一个。这种方法是有效的git merge策略的一部分。

Executing git merge with squash option.

在Git Merge中实现快进

对于简化的提交历史,快进是一种理想的方法,特别适用于小更新或错误修复。它允许您在没有额外合并提交的情况下合并分支,保持干净和线性的历史记录,这对于高效的git merge过程至关重要。

快进发生在基础分支,通常是master,没有分歧时,从而实现历史记录和指针更新的无缝集成。

1. 切换到form分支以开始快进过程:

git checkout form

2. 创建和更新JavaScript文件,例如index.js,在form分支中,对更改进行分段并提交:

git add index.js
git commit -m "Added JavaScript functionality"

3. 最后,使用快进方法将form分支合并到master中。此步骤将主分支与来自表单分支的最新更改对齐,创建一个平滑,线性的git merge路径。

git checkout master
git merge form
Performing a fast-forward merge in Git.

结论

这个教程已经引导你通过各种技巧来使用 git merge 合并分支。从合并提交到快进,这些方法提高了你在 Git 中协作管理项目的能力。

现在具备了这些技能,考虑参与到 GitHub 上的协作 项目中,在实际场景中应用你对 git merge 的知识。

Source:
https://adamtheautomator.com/git-merge/