Understanding merge commits in Git

Sara Verdi
Sara Verdi
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Merge commits are a fundamental aspect of Git, helping to integrate changes from different branches into a single branch. This guide will cover what merge commits are, how they behave in different scenarios, and how to use the Graphite CLI to manage them efficiently.

A merge commit occurs when you integrate changes from one Git branch into another. This action results in a new commit on the target branch that ties together the histories of both branches. Unlike a rebase or a squash, a merge commit preserves the entire history of both branches, making it easier to understand the context of changes and decisions made throughout the development process.

When performing a standard merge in Git, the commit history from both branches is preserved. This is important for maintaining a comprehensive history of your project, which can be invaluable for debugging and understanding the evolution of your project. For instance, when you use git merge, Git automatically creates a merge commit unless you explicitly disable this behavior with options like --no-commit or --squash.

Here's a basic example of creating a merge commit:

Terminal
# Checkout to the main branch
git checkout main
# Merge changes from the feature branch into main
git merge feature-branch

In this scenario, if there are no conflicts, Git will create a merge commit that links the histories of main and feature-branch.

Using git merge --no-squash allows you to merge changes while retaining each commit from the source branch as individual commits on the target branch. This is particularly useful when you want to preserve the detailed commit history of a feature branch without squashing into a single commit.

The Graphite CLI enhances the management of branch stacks and merge operations in Git. It integrates seamlessly with Git, allowing for easy visualization and manipulation of your repository's structure and dependencies.

For instance, to visualize how branches are stacked and their relationships, you can use the gt log command provided by Graphite.

When you're ready to merge your changes and manage your stack effectively, the Graphite CLI provides commands like gt merge which respects the integrity of your commit history, ensuring that merge commits are handled according to your project's workflow.

Understanding the history of merge commits can provide insights into the decision-making process within your team's project. Git's powerful tools like git log --graph allow you to visualize complex branches and their merges, helping you trace the history of decisions and changes. This command provides a graphical representation of your repository's history, showing where branches diverged and merged.

In conclusion, merge commits are a powerful feature of Git that help maintain a comprehensive and navigable project history. Using tools like the Graphite CLI, you can enhance your workflow by efficiently managing and visualizing merge operations and their impacts on your project's development.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2