Open In App

How to Merge Commits in Git?

Last Updated : 21 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Merging commits in Git is a crucial part of version control, especially when working on complex projects with multiple contributors. Combining commits can help streamline the commit history, making it cleaner and easier to understand. In this article, we’ll explore different methods to merge commits in Git effectively.

Why Merge Commits?

Before diving into the process, let's understand why merging commits is beneficial:

  • Simplifies History: Merging related commits reduces clutter, making the commit history easier to navigate.
  • Improves Readability: A concise commit history is easier for team members to read and understand.
  • Enhances Project Management: Streamlined commits help in better tracking and managing changes.

There are several ways to merge commits in Git, depending on your needs. Here, we will cover:

Merge Branches

The most common scenario is merging branches. This method integrates changes from one branch into another.

git merge <branch-name>

Example: If you want to merge the feature branch into the main branch:

git checkout main
git merge feature

Squash Commits

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

git rebase -i <base-commit>

Example: If you want to squash the last three commits. The below command will open an interactive rebase editor. Change the word pick to squash (or s) for the commits you want to merge.

git rebase -i HEAD~3

Merge Specific Commits

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

git cherry-pick <commit-hash>

Example: To merge a specific commit from the feature branch into the main branch:

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

Using git merge --squash

This method allows you to merge all changes from a branch but combine them into a single commit.

git merge --squash <branch-name>

Example: To squash all changes from the feature branch into a single commit on the main branch:

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

Combining Commits Using git reset and git commit --amend

This method involves resetting to a previous state and creating a new commit.

git reset --soft <base-commit>
git commit --amend

Example: To combine the last three commits into one:

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

Conclusion

Merging commits in Git is a vital skill for maintaining a clean and manageable project history. Whether you’re squashing commits, using interactive rebase, or performing a fast-forward merge, understanding these techniques will help you keep your repository organized and efficient. By following the steps outlined in this guide, you can confidently manage your commit history and improve your workflow.


Next Article
Article Tags :

Similar Reads