Chapter 9. Altering Commits

A commit records the history of your work and keeps your changes sacrosanct. Git provides several tools and commands specifically designed to help you modify and improve the commit history catalogued within your repository.

There are many valid reasons why you might modify or rework a commit or your overall commit sequence. Here are a few examples:

  • You can fix a problem before it becomes a legacy issue.

  • You can decompose a large, sweeping change into a number of small, thematic commits. Conversely, you can combine individual changes into a larger commit.

  • You can incorporate review feedback and suggestions throughout the development lifecycle of your project.

  • You can reorder commits into a sequence that doesn’t break a build requirement.

  • You can order commits into a more logical sequence.

  • You can remove debug code that was committed accidentally.

  • You can remove sensitive data in code that was committed “accidentally.”1

As you’ll see in Chapter 11, which explains how to share a repository, there are many more reasons to change commits prior to publishing your repository.

For this chapter, we will discuss the philosophy of altering commit histories, its repercussions, and various tooling Git provides for us to be able to alter either a series or a specific commit in our repositories’ commit history.

Tip

Remember, what gets committed in Git stays in Git (the commit history)! As such, you will need to be cautious of how changes ...

Get Version Control with Git, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.