DEV Community

Simon Shine
Simon Shine

Posted on

Why rewriting version control history matters

Life is understood backwards, but must be lived forwards.
--- Søren Kierkegaard

A summary on good commit messages

Because version control systems, like git and GitHub, are at the heart of modern software development workflows, most workflows are heavily impacted by and defined in terms of how to operate and cooperate around version control.

Making use of issue trackers, pull requests, review tools, and commit messages, there are many right ways to distribute the communication that revolves around a change in source code. So...

Why good commit messages?

  • Code is read much more often than it is written. As Raymond Chen says:

Even if you don't intend anybody else to read your code, there's still a very good chance that somebody will have to stare at your code and figure out what it does: That person is probably going to be you, twelve months from now.

  • The same reason extends to commit messages. As Chris Beans says:

... a well-crafted Git commit message is the best way to communicate context about a change to fellow developers (and indeed to their future selves). A diff will tell you what changed, but only the commit message can properly tell you why.

  • When an entire commit message consists of messages like "wip", "small fix", or "added code", Peter Hutterer suggests this is due to a lack of training in reading code:

If you haven't given much thought to what makes a great Git commit message, it may be the case that you haven't spent much time using git log and related tools. There is a vicious cycle here: because the commit history is unstructured and inconsistent, one doesn't spend much time using or taking care of it. And because it doesn't get used or taken care of, it remains unstructured and inconsistent.

The cost of rewriting under collaboration

When discovering the solution to a problem, the explored path is rarely the shortest path. Placing good explanations in git history makes it ideal for being read, but there are several costs when one has to "go back" and place them while at the same time collaborate with others:

  1. Cost of learning new tools: Rewriting git history requires some of the more difficult git commands, but there are simple approaches, too:
    • GitHub lets you squash pull requests; in the web UI, you can squash all commits into a single one and edit the commit message in a textarea. This works best when your pull requests are small.
    • git commit --fixup and git rebase -i --autosquash are easy to learn and use. Read the --fixup & --autosquash guide by Florent Lebreton.
    • git reset --soft lets you re-commit everything anew. Read Git Basics: Rewriting a branch's commit history from scratch by Igor Marques da Silva.
    • You can always create a local copy of a branch before attempting to rewrite history. You can even view the history of your copy (using git log <branch> and git show <hash>) while simultaneously rebuilding the history (using git add -p and git commit -v).
  2. Cost of wording: The time and patience to write a good explanation, and the expectation that it will eventually be read. Writing messages to your future self can work as a legitimate motivator.
  3. Cost of merge conflicts: When rewriting history and force-pushing to a remote branch, this forces collaborators on that branch out of sync by removing conflict-free merge paths, and it risks overwriting contributions.

    • Clearly, push --force is bad, mm'kay?! Fortunately, Steve Smith has something to say:

    The problem here is that when doing a force push Bob doesn’t know why his changes have been rejected, so he assumes that it’s due to the rebase, not due to Alice’s changes. This is why --force on shared branches is an absolute no-no; and with the central-repository workflow any branch can potentially be shared.

    But --force has a lesser-known sibling that partially protects against damaging forced updates; this is --force-with-lease.

    What --force-with-lease does is refuse to update a branch unless it is the state that we expect; i.e. nobody has updated the branch upstream. In practice this works by checking that the upstream ref is what we expect, because refs are hashes, and implicitly encode the chain of parents into their value.

  • Even when your collaborator is only reviewing or evaluating code, and not pushing changes, a merge conflict caused by force-pushing can cause extra time spent syncing. If this bothers a collaborator, perhaps rewriting history can happen only before and after their role is fulfilled.

The explorative path might truthfully involve a lot of "small fix", "trying", and "oops" steps, but these are not interesting in 6 months. We may be interested in more than perfect hindsight, so leaving a trail of comments in an issue tracker, in a pull request thread, or in a code review is a way to deepen the understanding.

How to write Good Commit Messages: Quick Reference

  1. Separate subject from body with a blank line
  2. Limit the subject line to 50 characters,
  3. Capitalize the subject line
  4. Do not end the subject line with a period
  5. Use the imperative mood in the subject line
  6. Wrap the body at 72 characters
  7. Use the body to explain what and why vs. how
  1. Why is it necessary? It may fix a bug, it may add a feature, it may improve performance, reliabilty, stability, or just be a change for the sake of correctness.
  2. How does it address the issue? For short obvious patches this part can be omitted, but it should be a high level description of what the approach was.
  3. What effects does the patch have? (In addition to the obvious ones, this may include benchmarks, side effects, etc.)

Some real-world examples

Imagine receiving a pull request with the following commits:

commit edc9ff5febb698a05f68650d6d58828202c24daf
Author: John Doe <>
Date:   Mon Nov 22 16:23:24 2021 +0100

Enter fullscreen mode Exit fullscreen mode

This message is clearly a working title that should be rephrased when the author is comfortable. Sometimes you want to commit and push half-done work, and sometimes you want to wait with providing a good explanation.

When collaborating, establish a standard way of saying "don't do X yet," where X could be look at, review, merge, contribute to, or any other activity that creates potential conflicts between contributors. Typically the word "WIP" means that.

This warrants git history rewriting before merging.

commit 11f8a068bec49abdddd7bdf203b77ecbfe48451f
Author: John Doe <>
Date:   Thu Nov 4 10:19:40 2021 +0100

    Added cronjob
Enter fullscreen mode Exit fullscreen mode

Added what cronjob, and why?

commit da8362e0b43ea3b18013f9245a614cf00ce43b60
Author: John Doe <>
Date:   Thu Nov 4 12:49:43 2021 +0100

    fixup! Added cronjob
Enter fullscreen mode Exit fullscreen mode

The author is signalling that they intend to squash the change. With this intent in mind, this clearly warrants git history rewriting before merging.

commit 57e0b283f69c576db48003dc1ec3b2f492f79540
Author: John Doe <>
Date:   Mon Nov 22 14:30:12 2021 +0100

    small fix
Enter fullscreen mode Exit fullscreen mode

This commit should either be squashed into some other commit, or explain what small fix this is. This either warrants rewriting the commit message, or squashing the commit.

commit 438cc1ce87dec47081b75d4cbd6ce4abca40b8c0
Author: John Doe <>
Date:   Thu Oct 7 19:42:13 2021 +0200

    Added indexed "cachedStatus" field on KnowledgeNodeStore which is automatically filled with the value of KnowledgeNode->data['status']['state']. This might help the speed of common KnowledgeNodeQuery calls. Migration included that needs to run before we can remove the old filtering logic in KnowledgeNodeQuery.
Enter fullscreen mode Exit fullscreen mode

Yes, that is a 308-character subject field. Besides coming up with a short, descriptive title, and arguably word-wrapping, this commit message does detail what and why rather than how. The included migration that is mentioned could be split into a separate commit.


Top comments (4)

sshine profile image
Simon Shine • Edited

Thanks a lot for your feedback. :)

it’s improper to suggest that we regularly rebase the git history

The point I want to make is that outside of "critical regions" in time where multiple persons read/write to the same branch (i.e. always for the main branch, and during collaboration on a branch, and during review/evaluation of a branch that only you work on, but not on all remote branches in general), we should rebase as crazily as we want, if it benefits our individual workflows.

The story I heard repeatedly is "don't rewrite history, it's bad," but in fact, there's two arguments that get muddled up, being "don't create unnecessary conflicts on shared branches," and "don't use these tools, they're too advanced for me." -- both of which are valid points, but neither of which warrant blacklisting rebasing e.g. at the end of a branch'es lifetime, immediately before it gets merged back, and possibly even before that, as long as you don't create conflicts when someone else is working, and as long as you don't force the use of tools on others.

I don’t see a strong reason to push a commit without a well-written commit message, intending to fix it later. That just sounds lazy to me (which I guess most programmers are 🤷)

It may be laziness, or it may be priorities, I'm not the one to judge. When commits like the ones I posted end up on the master branch, that is both lazy and careless. But if you end the day half-done with something, and you want to back up your work, pushing a WIP commit to your remote branch, before you've asked others to sync, and then rebasing it the day after, is not a crime in my book. :)

sshine profile image
Simon Shine

Someone did a better write-up than me that encourages rewriting history. They do end with a PR after reading where I’m open to rebasing after publishing a PR: