How to Merge Commits in Git

Introduction

Merging commits in Git is an essential practice in version control, particularly when working on large projects with multiple contributors. Combining commits helps streamline commit history, making it more readable and easier to manage. A clean Git history improves collaboration, debugging, and understanding project changes over time.

In this guide, we will explore different methods to merge commits effectively in Git, discuss their advantages and disadvantages, and provide practical examples for each approach.


Why Merge Commits?

Before diving into the technical details, let’s examine why merging commits is beneficial:

Advantages of Merging Commits

  • Simplifies History – Reduces clutter by consolidating related commits, making it easier to navigate.
  • Improves Readability – Helps team members understand the project changes at a glance.
  • Enhances Project Management – Streamlined commits make it easier to track and manage updates.
  • Reduces Redundancy – Eliminates unnecessary small commits that could otherwise clutter the repository.
  • Prevents Merge Conflicts – A well-maintained history can minimize potential conflicts.

Disadvantages of Merging Commits

  • Loss of Granular History – Individual commit details may be lost when merging.
  • Difficult Debugging – If a bug is introduced, it may be harder to pinpoint the exact commit responsible.
  • Potential Merge Conflicts – Improperly merging commits may lead to conflicts that require manual resolution.

How to Merge Commits in Git – Methods

Below are different methods you can use to merge commits in Git, depending on the use case:

1. Merging Branches

The most common scenario is merging changes from one branch into another. This is often done when a feature branch is ready to be integrated into the main branch.

Steps:

git checkout main
git merge feature

This command merges the feature branch into main, keeping the commit history intact.

Example:

You have been working on a new feature in the feature branch. Once completed, you merge it into the main branch to make it part of the stable codebase.

Advantages:

  • Preserves full commit history.
  • Simple and easy to use.

Disadvantages:

  • If multiple small commits exist, they will be merged, potentially cluttering history.

2. Squashing Commits

Squashing commits means combining multiple commits into a single commit. This is useful when you have numerous small commits that can be grouped into one logical change.

Steps:

git rebase -i HEAD~3

The interactive rebase editor will open. Change the word pick to squash (or s) for the commits you want to merge.

Example:

You made three consecutive commits that fix minor issues in a feature. Instead of pushing three separate commits, you squash them into a single commit.

Advantages:

  • Produces a cleaner history.
  • Groups related changes together for clarity.

Disadvantages:

  • Rewriting history can be problematic if commits have already been pushed to a shared repository.

3. Merging Specific Commits Using git cherry-pick

Sometimes, you may need to merge specific commits from one branch to another without merging the entire branch. This can be done using git cherry-pick.

Steps:

git checkout main
git cherry-pick <commit-hash>

Example:

If you have five commits in the feature branch but only want to merge the third commit into main, you can cherry-pick that commit.

Advantages:

  • Selectively merges only the necessary changes.
  • Keeps history clean.

Disadvantages:

  • May require resolving conflicts if dependencies exist between commits.

4. Using git merge --squash

This method allows merging all changes from a branch but combining them into a single commit.

Steps:

git checkout main
git merge --squash feature
git commit -m "Merged feature branch as a single commit"

Example:

A developer completes a feature branch with multiple commits. Instead of merging each commit individually, they merge all changes as a single commit.

Advantages:

  • Retains all changes while keeping the history clean.
  • Avoids excessive small commits in the main branch.

Disadvantages:

  • Loses detailed commit history from the feature branch.

5. Combining Commits Using git reset and git commit --amend

This method involves resetting the branch to an earlier commit and creating a new commit with the combined changes.

Steps:

git reset --soft HEAD~3
git commit --amend

Example:

You have made three small commits but want them to appear as a single commit. This method lets you reset those commits and combine them.

Advantages:

  • Provides full control over commit history.
  • Allows restructuring commits before finalizing them.

Disadvantages:

  • If the commits have been pushed, this method requires a forced push (git push --force), which can cause issues for other team members.

Choosing the Right Merge Method

Method Best For Preserves History? Requires Force Push?
Merge Branches Standard merging of branches ✅ Yes ❌ No
Squash Commits Reducing multiple small commits into one ❌ No ⚠️ Sometimes
Cherry-pick Selecting specific commits for merging ✅ Yes ❌ No
Merge –squash Merging a branch into one commit ❌ No ❌ No
Reset & Amend Rewriting commit history before pushing ❌ No ✅ Yes

Conclusion

Merging commits in Git is an essential technique for maintaining a clean and manageable repository. Depending on your needs, you can choose from various methods such as merging branches, squashing commits, cherry-picking specific changes, or using git merge --squash.

Each method has its advantages and trade-offs. For general merging, use git merge. If you need to tidy up history, use git rebase or git merge --squash. For complete flexibility, git reset --soft gives you fine control.

By following these best practices, you can ensure a well-structured, readable, and efficient commit history that benefits the entire development team.

For further details, refer to the official Git documentation.

Explore more about how to revert a commit in git after push​

Related articles

What Are Kubernetes Containers?

What Are Kubernetes Containers? Kubernetes is revolutionizing the way developers build, deploy, and manage applications. With its powerful capabilities,...

Monitor kubernetes cluster with grafana

Monitor kubernetes cluster with grafana In today’s dynamic DevOps environments, monitoring and logging are essential for ensuring application reliability...

How to Manage Linux System Routing Rules With Iptables

How to Manage Linux System Routing Rules With Iptables Iptables is a user-space utility program for managing firewall rules...

What are Data Security Features in Azure

What are Data Security Features in Azure Introduction Data security is one of the most critical aspects of cloud computing....