How to Merge Local Branches in Git

John Wachira Feb 02, 2024
  1. the git merge Command
  2. Use Fast Forward Merge to Fix Minor Features and Bugs in Git
  3. Use 3-Way Merge When Working on a Large Feature in Git
How to Merge Local Branches in Git

This article will introduce the git merge command and how you can use it to merge local branches in Git. We will look at how the command works and the merging process.

the git merge Command

Definition: We use the git merge command to integrate different branches into a single branch. This makes it easy for developers to work on distinct features in a project.

How the command works: You can merge two or more branches using the git merge command.

The merge process: Follow these simple steps to start the merging process.

  1. Run the git status command. This will point the HEAD to the recipient branch.

    Switch to the recipient branch using the git checkout <recipient branch> command.

  1. Update your master branch with the latest remote commits using the git fetch and git pull commands.
  2. Merge the branches by running the git merge <your branch name here> command.

Use Fast Forward Merge to Fix Minor Features and Bugs in Git

We use the fast-forward merge to fix minor features and bugs. This type of merger will combine the history of your branches linearly.

Here is an illustration.

fastforward merge

Use 3-Way Merge When Working on a Large Feature in Git

This is an alternative to the fast-forward merge. We use this when several developers are working on a large feature independently.

Git invokes a 3-way merge when the histories of your branches diverge. Here is an illustration.

three-way merge

If a conflict occurs, run the git add <conflicted file> command and commit to finishing up the process.

The git merge --no--ff command creates a merge commit in fast forward, and 3 way merge.

The git merge --squash command combines individual commits to one. This helps keep your project clean.

The git merge --abort command ends the merging process when a conflict occurs and restores your project.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Merge