Git & GitHub / Git Branching and Merging
Squashing and Amending Commits
This tutorial will teach you how to squash and amend commits in Git. These skills are useful for keeping your commit history clear and concise.
Section overview
5 resourcesCovers creating branches, merging changes, and resolving conflicts in Git.
1. Introduction
1.1 Goal of the Tutorial
This tutorial aims to instruct you on how to squash and amend commits in Git. Squashing is a method to condense multiple commits into a single commit, and amending allows you to modify the most recent commit.
1.2 Learning Outcomes
By the end of this tutorial, you will be able to:
- Understand what squashing and amending commits in Git means
- Squash multiple commits into one
- Amend a commit
1.3 Prerequisites
Basic knowledge of Git is required. You should know how to make commits before proceeding with this tutorial.
2. Step-by-Step Guide
2.1 Squashing Commits
Squashing is a Git function that allows you to turn multiple commits into one. This is especially useful when you have a lot of commits of a minor nature that clutter up your commit history.
2.1.1 How to Squash Commits
- Run
git rebase -i HEAD~n, replacing n with the number of commits you want to squash. This command will open an interactive rebase session. - In the text editor that opens up, replace
pickwithsquashorsnext to the commits you want to squash into the commit before it. The first commit should be left aspick. - Save and close the editor.
- An editor window will open for you to change the commit message of the new squashed commit. Save and close when you are finished.
- Run
git push origin branch-name --forceto push the squashed commit to the remote repository.
2.2 Amending Commits
Amending is a Git function that allows you to modify the most recent commit. This is useful if you made a mistake in your last commit.
2.2.1 How to Amend Commits
- Make the changes you want to include in the commit.
- Run
git commit --amend. If you don't want to change the commit message, usegit commit --amend --no-edit. - If you're not using the
--no-editoption, an editor window will open for you to change the commit message. Save and close when you are finished. - Run
git push origin branch-name --forceto push the amended commit to the remote repository.
3. Code Examples
3.1 Squashing Commits
Suppose you have a commit history of 5 commits and you want to squash the last 4 commits into the first one.
$ git rebase -i HEAD~5
This will open your text editor with something like this:
pick 01d1124 Adding feature X
pick 6340aaa Fixing bug
pick ebfd367 Updating feature
pick 30e0ccb Refactoring code
pick b073cf Updating readme
Change pick to squash for commits you want to squash:
pick 01d1124 Adding feature X
squash 6340aaa Fixing bug
squash ebfd367 Updating feature
squash 30e0ccb Refactoring code
squash b073cf Updating readme
Save and close your editor. A new editor window will open for you to change the commit message of the new squashed commit. After saving and closing, push the changes to the remote repository.
$ git push origin main --force
3.2 Amending Commits
Suppose you've just made a commit and you realize you forgot to add a file, file.txt.
$ git add file.txt
$ git commit --amend --no-edit
$ git push origin main --force
4. Summary
You have learned how to squash multiple commits into one and how to amend the most recent commit. These are useful skills for maintaining a clean and readable commit history.
5. Practice Exercises
5.1 Exercise 1
Squash your last 3 commits into one.
5.2 Exercise 2
Amend your last commit to include a new file, newfile.txt.
5.3 Exercise 3
Squash your last 5 commits and change the commit message to "5 commits squashed".
5.4 Tips for Further Practice
Try to make a habit of squashing and amending your commits where necessary. It's a good practice to keep your commit history clean and concise.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article