The Wayback Machine - https://web.archive.org/web/20240926223448/https://www.geeksforgeeks.org/how-to-squash-commits-in-git/
Open In App

How to Squash Commits in Git?

Last Updated : 15 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Maintaining a clean and organized Git history is very important for collaboration and project management. One way to streamline your commit history is by squashing commits, which combines multiple commits into a single, more coherent commit. In this article, we will see how to squash commits in Git.

Approach to Squash commits in Git

Step 1: Make an html file(index.html) to make changes and add different commits, after that initialize git in the current directory.

git init

Simply add all files using dot(.) and add your first commit.

git add .
git commit -m "first commit"
Screenshot-2024-05-12-142824
git add . files

Now create a branch to reflect changes

git branch -m "main"
git branch // to see current branch
Screenshot-2024-05-12-143118
git branch

Step 2: Now, make simple changes in index.html and add to git and commit three times.

git add .
git commit -m "commit message"
Screenshot-2024-05-12-144713
git changes

we can see all commit using following command

git log --oneline
Screenshot-2024-05-12-151026

Step 3: Start an Interactive Rebase

Once you have identified the commits, use the `git rebase -i HEAD~n` command, where n is the number of commits you want to squash. For example, if you want to squash the last 3 commits, you would use git rebase -i HEAD~3.

git rebase -i HEAD~3

it will open a Interactive Rebase editor.

Screenshot-2024-05-12-151322

Git will open an interactive rebase editor, listing the commits starting from the HEAD. Each commit will have a prefix like pick, indicating that Git will pick these commits during the rebase. In the interactive rebase editor, change the word pick to squash (or just s) for the commits you want to squash. Keep pick for the first commit you want to keep as-is, as it will be the base commit.

Screenshot-2024-05-12-151606
squash

Now save, “Esc->Ctrl+:->type wq” to save changes

Step 4: Now run below to command to make changes continue

git rebase --continue

it will open another editor, remove or make comment to second commit or third commit

Screenshot-2024-05-12-152226
vim editor

Now save file, it will return a message.

Screenshot-2024-05-12-152547
git rebase

we can see the result using `git log –oneline`

Screenshot-2024-05-12-152732
git log –oneline

Step 5: Push the Changes

Once you have squashed the commits, you may need to force-push the changes to the remote repository if the commits have already been pushed. Use git push origin <branch-name> –force to force-push the changes.



Similar Reads

Git - Squash Commits
In Git, squashing refers to combining two or more commits into a single commit. This is typically done to improve the organization and clarity of the commit history, especially when working on a collaborative project with other developers. In this article, we will review the feature git squashing, which is a great feature for arranging multiple com
2 min read
Use of Git Squash Commits
When working with Git, especially in feature branches or collaborative projects, you may end up with multiple commits that you’d prefer to combine into a single commit before merging. Squashing commits in Git helps maintain a clean and organized commit history. In this article, we’ll learn about squashing commits in Git. Table of Content What Is Co
4 min read
Git - Squash
Maintaining a clean and concise commit history is important for readability and collaboration. One powerful technique to achieve this is Git squash. Squashing allows you to combine multiple commits into a single commit, making your project history more simpler and easier to follow. In this article, we'll see more about what Git squash is, its benef
6 min read
Recovering Lost Commits in Git
Git is a very powerful tool, and with great power comes great responsibilities. If not used and handles properly, It might cause you to lose your commits. There might be situations when you may find all of your work missing at once. If you have regularly committed your work, there is a way to recover these lost commits. This tutorial will aim at us
2 min read
How to Merge Commits in Git?
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 divi
3 min read
How to Undo the Most Recent Local Commits in Git?
When working with Git, it's common to make mistakes or realize that recent commits need to be revised. Whether you've made an error in your code, committed sensitive information, or simply want to restructure your commits, knowing how to undo the most recent local commits is an essential skill. This guide will walk you through the process of undoin
3 min read
How to Get List of All Commits in Git?
Git a powerful version control system, keeps track of all changes made to a project by recording each change in a commit. Sometimes, you need to see the complete list of commits to understand the project's history, track changes, or debug issues. Listing all commits in a Git repository can be useful for: Understanding Project History: Reviewing all
3 min read
How to List Unpushed Git Commits (local but not on origin)?
Tracking changes efficiently and ensuring that your local commits are pushed to the remote repository is important. However, there are times when you may have local commits that haven't yet been pushed to the origin. Identifying these commits can be important for synchronizing your work with your team. This article will guide you through the proces
3 min read
How do I Delete Unpushed Git Commits?
Sometimes you might make mistakes in your Git commits and want to start fresh. Here's how to remove unpushed commits in Git, focusing on two clear methods. Table of Content Using Soft ResetUsing Hard ResetDiscard Unpushed Commits CompletelyPreserve Changes from Unpushed CommitsUsing Soft ResetUse this if you want to remove the commit itself but kee
3 min read
How to View Git Log of One User's Commits?
In collaborative software development, it's often necessary to view the commit history of individual contributors. Git provides powerful tools to filter and examine commits based on various criteria, including the author. This guide will walk you through the steps to view the Git log of one user's commits. PrerequisitesWhat is a Git RepositoryGit c
2 min read
How to Cache https Credentials For Pushing Commits in Git?
When working with Git repositories over HTTPS, you might find yourself frequently prompted for your username and password when pushing commits. This can be bulky and interrupt your workflow. Thankfully, Git provides a way to cache your credentials so you don’t have to enter them repeatedly. In this article, we’ll explore how to cache HTTPS credenti
3 min read
How to cherry-pick Multiple Commits in Git?
Cherry-picking in Git allows you to apply the changes from a specific commit (or commits) to another branch. This can be particularly useful when you need to incorporate bug fixes or features from one branch to another without merging the entire branch. While cherry-picking a single commit is simple, doing so for multiple commits requires some addi
4 min read
Calculate Number of Lines Changed Between Two Commits in Git
Git allows developers to track changes and collaborate effectively. One common task is to determine how many lines of code have changed between two commits. This information can be useful for code reviews, project metrics, and understanding the scope of changes. In this article, we will walk through the steps to calculate the number of lines change
3 min read
How to Change Author and Committer Info for Multiple Git Commits?
When we are working with Git, the changed history does not preserve the commits but the metadata that is attached to each commit. The metadata includes the author and committer name and email. Sometimes, due to misconfiguration of Git causes incorrect details of the committer and author for not one commit but for all commits. In this article, we'll
3 min read
How To List All Commits That Changed A Specific File In Git?
Git, a widely used version control system, allows developers to track changes in their codebase efficiently. One common need when working with Git is to list all commits that have modified a specific file. This can be particularly useful for tracking changes, understanding the history of a file, or identifying when a bug was introduced. Table of Co
1 min read
How To Undo Pushed Commits Using Git?
Undoing pushed commits in Git is a common task that developers encounter when they need to rectify mistakes or change their commit history. Depending on the specific scenario and requirements, there are different approaches to undoing pushed commits. Here, we will discuss various methods to achieve this, ensuring that you can handle any situation e
2 min read
How to Revert Multiple Git Commits?
Reverting multiple Git commits is a common task when you need to undo a series of changes made to your codebase. This process can be important for fixing mistakes, removing faulty code, or simply rolling back to a previous stable state. Git provides several methods to revert multiple commits, each with its own use cases and implications. This artic
3 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
How To List Only The Names Of Files That Changed Between Two Commits?
Git is a powerful version control system that allows developers to track changes in their codebase. One common task is identifying which files have changed between two specific commits. This can be useful for code reviews, debugging, and understanding the impact of changes. In this article, we'll explore how to list only the names of files that cha
3 min read
How To Permanently Remove Few Commits From Remote Branch?
In some situations, you may need to permanently remove certain commits from a remote branch in a Git repository. This could be due to mistakenly committed sensitive information, broken code, or simply cleaning up the commit history. Removing commits from a remote branch is a sensitive operation because it rewrites the commit history and can affect
3 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct purposes and are used in different contexts. In t
5 min read
Article Tags :