Combine Multiple Git Commits into One



If you're a Git developer, you've probably dealt with a noisy commit history. Adding multiple Git commits into one (a process called squashing) can help to organize your commit history. In this tutorial, I will show you how to combine Git commits into a single, nice, clean commit.

Why Combine Git Commits?

Combining or squashing multiple Git commits has several benefits:
  1. Cleaner History: It helps to keep a tidy project's history with merging small, incremental changes into a single commit.
  2. Easier Collaboration: It simplifies code reviews by bringing all changes which go together, under the same commit.
  3. Better Readability: It makes it easier to track feature or bug-fix changes.

Methods to Combine Git Commits

There are two main ways to squash commits in Git:

  1. Interactive Rebase (a good idea to combine multiple recent commits)
  2. Soft Reset and Recommit (ideal when combining all commits into one)

Let's understand each of these methods in a little more detail.

Method 1: Using Git Interactive Rebase

Git rebase is a powerful git feature that lets you reorder and squash commits and some other things. In this tutorial, I'll show you how to use interactive rebase to combine your recent commits.

Interactive Rebase - Step-by-Step Guide

1. Identify the Commit Range to Squash: Find out how many commits you want to combine. Suppose you did three commits and you wanted to squash them in one. If you will need to rebase to three commits before your latest commit, then you would do that.
2. Start the Interactive Rebase: Just run your command using N for the number of commits you want to combine.

git rebase -i HEAD~N

For example, if you want to squash the last three commits, type:

git rebase -i HEAD~3

3. Edit the Rebase File: Then, you run the rebase command, and get an editor opening where a list of your recent commits is available. The word pick is prefixed to each commit.
4. Choose Which Commits to Squash: On all the commits you want to combine with the top most, change the word pick to squash (or just s). First, keep pick on the first commit line and change the rest to squash. For example,

pick  Initial commit message
squash Second commit message
squash Third commit message

5. Save and Close the Editor: After editing, save and close the editor. Your commits will be combined now in Git.
6. Edit the Combined Commit Message: The combined commit message will open in your editor and Git will ask you to edit. If you want you can keep the individual commit messages or you can write a new one. When you're finished, save and close the editor.
7. Complete the Rebase: Git will now compress selected commits into one. To verify, use the following command:

git log

You should see a single commit in place of the previous commits.

Method 2: With Git Soft Reset and Recommit.

This is quicker if you wish to squish all your changes into one commit.

Soft Reset - Step-by-Step Guide

1. Identify the First Commit to Include: From all commits decide which you want to be the starting point. Suppose you want to squash everything after the very first one.
2. Soft Reset to the Target Commit: Git reset command with --soft and the hash of your needed commit as your base.

git reset --soft <commit_hash>

Replace with the hash of the specific commit. This will unstage the changes and reset the HEAD to a specified commit.
3. Create a New Commit: All changes staged and commit them all at once.

git commit -m "Your combined commit message"

4. Verify the Commit: To make sure your changes belong to only one commit, run git log.

Important Notes on Squashing Commits

  • Local vs. Remote Branches: The local branches are safe for squashing. To do so, you may be forced to force push (git push -f) and maybe disrupts collaborators' work. If you are working on a shared branch always coordinate with your team.
  • Undoing a Squash: With git reflog, you can reset a branch at an earlier state if you accidentally squash commits.

Squashing Commits Before Merging a Pull Request

Most teams like squashed commits before merging pull requests for cleaner project history. On GitHub or other code hosting sites, you can choose to "Squash and Merge"; it squashes your pull request commits into one and merges them to the main branch.

Conclusion

Combining a bunch of commits into one is a very practical thing that creates a clean and readable commit history. Mastering git rebase and git reset allows you to squash commits, making your contributions more friendly to follow and collaborative.

Updated on: 2024-11-04T13:15:48+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements