Git - Merge
When you are working on a new feature, you usually create a separate branch to test your ideas without changing the main project. But while you are working, others may also update the main branch. Once your feature is ready, you want to bring your changes and the latest main branch updates together. This is where Git merge helps.
In this article, we will learn what Git merge is, why it’s used, how it works, and what to do if Git can’t merge automatically (i.e., when a merge conflict happens).
What is Git Merge?
Git merge is a command to combine the changes from two branches into one. It helps in integrating work from multiple branches, ensuring that all changes are integrated and up-to-date in the target branch without losing any progress made on either branch. For example, you can merge your feature branch into the main branch to include all your updates. It brings together all commits from both branches so your work and others' changes stay in one place.
- Preserves History: Keeps the commit history of both branches.
- Automatic and Manual: Automatically merges unless there are conflicts.
- Fast-Forward Merge: Moves the branch pointer forward if no diverging changes exist.
- Merge Commit: Creates a special commit to combine histories.
- No Deletion: Branches remain intact after merging.
- Used for Integration: Commonly integrates feature branches into main branches.
Syntax
git merge <branch-name>
How Does Git Merge Work?
The concept of git merging is basically to merge multiple sequences of commits, stored in multiple branches in a unified history, or to be simple you can say in a single branch.
- Common Base: When merging two branches, Git looks for the common base commit.
- Merge Commit: Once it finds the common base, Git creates a new "merge commit" to combine the changes.
- Conflict Resolution: If there are any conflicts between the branches, Git will prompt you to resolve them.

- In our case, we have two branches one is the default branch called "main" and the other branch named "dev" and this is how our git repo looks before merging.
- Here git finds the common base, creates a new merge commit, and merged them.

- A git merge operation is performed by running the below command.
- When we perform merging, git always merges with the current branch from where we are performing the operation(in our case it is "main"). By this, the branch being merged is not affected.
"git merge <name of the branch to be merged (in our case it is "dev")>".
Types of Merging in Git
Git supports several types of merging
In Git, there are two primary types of merging. There are.
1. Fast-forward merging
- This occurs when the target branch (e.g., main) is directly ahead of the feature branch (e.g., dev).
- Instead of creating a merge commit, Git simply moves the current branch’s tip to the target branch’s tip.
- Fast-forward merging is only possible when branches haven't diverged

2. Three-way merging
- This type occurs when the base branch has changed since the branch was first created.
- Git generates a new merge commit by comparing changes in both branches with the base branch.

Note : Git also supports some other types of merging like recursive and octopus margin. With the help of a single merge commit "octopus merging" can merge multiple branches at once. "Recursive merging" is similar to three-way merging but it can handle some more complex merge operations than the three-way merging.
Essential Commands To Perform Git Merging
To perform a git merge, you need a Git repository with at least two branches. Here's how to proceed:
- Create a new branch
git branch <name of the branch you wanna create>
- Merge two branches: First, check out the target branch, then run:
git merge <name of the current branch>

- Now we have successfully merged our two branches and as you can see we have the same changes or you can say commits in both branches.
Steps To Merge a Branch
To ensure smooth merging, follow these steps:
Step 1: Create a New Branch
Create a new branch from the remote repository you want to merge.
git checkout -b <new-branch-name>
Step 2: Pull the Latest Changes
Before merging, ensure that you pull the latest changes from both branches (e.g., main and the feature branch).
git checkout <target-branch>
git pull origin <target-branch>
git checkout <feature-branch>
git pull origin <feature-branch>
Step 3: Merge the Branch
If any conflicts arise, Git will notify you. Resolve them manually before proceeding.
git checkout <target-branch>
git merge <feature-branch>
Step 4: Test the Merged Code
Make sure the merged code functions correctly by testing it either automatically or manually.
# Run tests or manually test your application
Step 5: Commit the Merged Code
Once satisfied with the merged code, commit the changes:
git commit -m "Merge branch 'dev' into main"
Step 6: Push the Merged Branch
Push the changes to the remote repository to share the new merged branch:
git push origin main
Git Merge vs Rebase
Git Merge | Git Rebase |
---|---|
Git merge will create a new commit by combining the changes in one branch with another branch. | Git rebase will integrate one branch commits to another branch by this the commit history will be rewritten. |
The commit history of all the branches with which you have merged will be stored. | A new commit will be created when you integrate the changes. |
It is useful for incorporating changes from a feature branch or integrating changes from multiple developers. | Rebase is also helpful for resolving conflicts early and maintaining a more streamlined and cohesive commit history. |
Uses of Git Merge
The following are the uses of git merge:
- Incorporate changes: Integrates changes from another branch into the current one.
- Consolidate development work: Merges development done across different branches.
- Bring feature branches into the main branch: For example, merging feature branches into main or master.
Conclusion
Git merge is a useful command that helps combine work from different branches into one. When you are working on a feature in a separate branch and the main branch gets updated, merging helps you bring everything together without losing any changes. You can use fast-forward merge when branches have not changed much or a three-way merge when both branches have new commits. If there are conflicts, Git will ask you to fix them. Once merged, you should test your code and push it to the remote repository. This way, everyone's work stays in sync and the project moves forward smoothly.