Skip to main content
ubuntuask.com

Back to all posts

How to Squash Multiple Git Commits Into One?

Published on
4 min read
How to Squash Multiple Git Commits Into One? image

Best Git Tools to Buy in October 2025

1 Learning Git: A Hands-On and Visual Guide to the Basics of Git

Learning Git: A Hands-On and Visual Guide to the Basics of Git

BUY & SAVE
$34.92 $45.99
Save 24%
Learning Git: A Hands-On and Visual Guide to the Basics of Git
2 Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development

Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development

BUY & SAVE
$43.23 $65.99
Save 34%
Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development
3 Professional Git

Professional Git

BUY & SAVE
$24.79 $52.00
Save 52%
Professional Git
4 Version Control with Git: Powerful tools and techniques for collaborative software development

Version Control with Git: Powerful tools and techniques for collaborative software development

  • QUALITY ASSURANCE: CAREFULLY INSPECTED FOR GOOD CONDITION AND READABILITY.
  • AFFORDABLE CHOICE: SAVE MONEY WITH DISCOUNTED PRICES ON QUALITY BOOKS.
  • ECO-FRIENDLY OPTION: CONTRIBUTE TO SUSTAINABILITY BY BUYING SECOND-HAND.
BUY & SAVE
$42.44 $44.99
Save 6%
Version Control with Git: Powerful tools and techniques for collaborative software development
5 Head First Git: A Learner's Guide to Understanding Git from the Inside Out

Head First Git: A Learner's Guide to Understanding Git from the Inside Out

BUY & SAVE
$50.99 $79.99
Save 36%
Head First Git: A Learner's Guide to Understanding Git from the Inside Out
6 Git Commands Cheat Sheet Reference Guide – Essential Git Command Quick Guide for Beginners Developers

Git Commands Cheat Sheet Reference Guide – Essential Git Command Quick Guide for Beginners Developers

BUY & SAVE
$14.99
Git Commands Cheat Sheet Reference Guide – Essential Git Command Quick Guide for Beginners Developers
7 Pro Git

Pro Git

BUY & SAVE
$31.02 $59.99
Save 48%
Pro Git
8 Git Prodigy: Mastering Version Control with Git and GitHub

Git Prodigy: Mastering Version Control with Git and GitHub

BUY & SAVE
$19.00
Git Prodigy: Mastering Version Control with Git and GitHub
9 Pragmatic Guide to Git (Pragmatic Programmers)

Pragmatic Guide to Git (Pragmatic Programmers)

  • AFFORDABLE PRICES FOR QUALITY PRE-OWNED READS.
  • ENVIRONMENTALLY FRIENDLY CHOICE: RECYCLE AND REUSE BOOKS!
  • UNIQUE FINDS: DISCOVER RARE TITLES AT GREAT VALUE!
BUY & SAVE
$7.90 $25.00
Save 68%
Pragmatic Guide to Git (Pragmatic Programmers)
10 50pc GIT INDUSTRIAL TOOL MOUNTED GRINDING STONE ASSORTMENT BITS 1/4" DRILL MSA50

50pc GIT INDUSTRIAL TOOL MOUNTED GRINDING STONE ASSORTMENT BITS 1/4" DRILL MSA50

  • DURABLE AND RELIABLE TOOLS FOR INDUSTRIAL USE
  • TRUSTED BRAND: GOLIATH INDUSTRIAL TOOL QUALITY
  • COST-EFFECTIVE SOLUTIONS FROM CHINA’S LEADING MANUFACTURER
BUY & SAVE
$22.95
50pc GIT INDUSTRIAL TOOL MOUNTED GRINDING STONE ASSORTMENT BITS 1/4" DRILL MSA50
+
ONE MORE?

To squash multiple Git commits into one, you can follow the steps below:

  1. Start by ensuring that you are on the branch where you want to squash the commits.
  2. Open up your terminal or command prompt and navigate to the root directory of your Git repository.
  3. Use the following command to initiate an interactive rebase: git rebase -i HEAD~n Replace n with the number of commits you want to squash. For example, if you want to squash the last 3 commits, use git rebase -i HEAD~3.
  4. The command will open up a text editor with a list of commits you specified. Each commit starts with the word "pick." To squash the commits together, change "pick" to "squash" or simply "s" for the commits you want to squash into the previous one.
  5. Save and close the file. Another text editor will appear, combining the commit messages of the squashed commits. You can edit and customize the message if desired.
  6. Save and close this file as well. Git will now perform the interactive rebase, squashing the specified commits.
  7. Finally, to push the changes to the remote repository, you might need to use the --force flag to overwrite the existing commits: git push origin branch-name --force

It is essential to note that squashing commits rewrites the repository's history. Therefore, if you have already pushed the commits to a shared repository, use caution in implementing this technique, as it may disrupt the work of collaborators who depend on that particular history.

What is the purpose of rebasing in Git?

The purpose of rebasing in Git is to integrate changes from one branch to another by applying the commits of one branch onto another branch. It allows you to move or combine a sequence of commits to a new base commit, making the commit history cleaner and more linear.

The primary goals of rebasing are:

  1. Maintaining a linear commit history: By rebasing, you can avoid unnecessary merge commits, as the changes are incorporated directly into the target branch, resulting in a cleaner and more readable commit history.
  2. Facilitating easier code reviews: A linear commit history makes it easier to review changes, understand the development process, and identify potential issues or bugs.
  3. Grouping related changes together: Rebasing helps in organizing and grouping related commits together, giving a logical and coherent structure to the commit history.
  4. Resolving conflicts: Rebasing allows you to address conflicts that may arise when applying the changes from one branch onto another. It helps in resolving conflicts in a more controlled and localized manner, leading to a cleaner overall codebase.

However, it is important to note that rebasing changes the commit history, which can cause problems when working with others on shared branches. It is generally recommended to use rebasing on local branches or when working on personal or feature branches.

How to force push changes in Git?

To force push changes in Git, you can follow these steps:

  1. Make sure you have committed all your local changes to your branch using the command git commit -m "Commit message".
  2. Run git fetch to ensure you have the latest changes from the remote repository.
  3. Use git rebase origin/branch-name to incorporate the latest changes from the remote branch into your branch. (Replace "branch-name" with the name of your branch).
  4. If there are conflicts during the rebase process, resolve them by editing the conflicting files manually and then run git add for each resolved file.
  5. After resolving the conflicts, use git rebase --continue to continue the rebase process.
  6. Once the rebase is completed, run git push -f origin branch-name to force push your local changes to the remote repository. (Replace "branch-name" with the name of your branch).

Note: Be cautious while force pushing as it can overwrite the commit history of the branch, so it is generally recommended to avoid force pushing if you are collaborating with others on the same branch to avoid conflicts and ensure everyone has the latest changes.

What is the Git command to show changes in a file?

The Git command to show changes in a file is "git diff [file]". This command displays the differences between the changes made to a file and the latest commit in the repository.