Merge a Branch

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a popular version control system that allows developers to collaborate on projects efficiently. One of the key features of Git is the ability to merge branches. Merging a branch allows you to combine changes from one branch into another, making it easier to manage changes and collaborate with others.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/merge("`Merge Histories`") subgraph Lab Skills git/merge -.-> lab-12741{{"`Merge a Branch`"}} end

Merge a Branch

Your task is to merge a branch into the current branch using Git. You will need to switch to the target branch and then merge the source branch into it. This can be useful when you want to combine changes from a feature-branch-A branch into the master branch of your project.

For this lab, let's use the repository from https://github.com/labex-labs/git-playground. Follow these steps to merge the feature-branch-A into the master branch:

  1. Clone the repository, navigate to the directory and configure the identity:
git clone https://github.com/labex-labs/git-playground
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. Create a feature-branch-A branch. Switch to it:
git checkout -b feature-branch-A
  1. Add "hello,world" to the file2.txt file, add it to the staging area and commit it with the message "fix file2.txt":
echo "hello,world" >> file2.txt
git add .
git commit -m "fix file2.txt"
  1. Switch to the master branch:
git checkout master
  1. Merge the feature-branch-A into the master branch:
git merge feature-branch-A
  1. Resolve any conflicts that may arise during the merge process.

This is the result of running git log:

commit e2b80358ae6e4c3b8439cf111a4672a188739290 (HEAD -> master, feature-branch-A)
Author: xiaoshengyunan <@users.noreply.github.com>
Date:   Fri Jul 21 18:51:00 2023 +0800

    fix file2.txt

Summary

Merging a branch in Git allows you to combine changes from one branch into another. This can be useful when you want to combine changes from a feature-branch-A branch into the master branch of your project. To merge a branch, you need to switch to the target branch and then use the git merge command to merge the source branch into it.

Other Git Tutorials you may like