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.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Covers 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

  1. 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.
  2. In the text editor that opens up, replace pick with squash or s next to the commits you want to squash into the commit before it. The first commit should be left as pick.
  3. Save and close the editor.
  4. An editor window will open for you to change the commit message of the new squashed commit. Save and close when you are finished.
  5. Run git push origin branch-name --force to 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

  1. Make the changes you want to include in the commit.
  2. Run git commit --amend. If you don't want to change the commit message, use git commit --amend --no-edit.
  3. If you're not using the --no-edit option, an editor window will open for you to change the commit message. Save and close when you are finished.
  4. Run git push origin branch-name --force to 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Image Converter

Convert between different image formats.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help