How to Squash Git Commits Manually

How to Squash Git Commits Manually

Welcome to Git Weekly! Each week, we explore advanced git features to help you fine-tune your workflow. Today’s topic is manual squashing commits—a powerful way to clean up your commit history.

Squashing commits is the act of combining a few commits into one.?There are two places the term "squash" appears, in the context of git:

The squash merge is a special type of merging, that automatically takes all of the commits in one branch, squashes them together, and adds the result commit into the target branch (usually master).

Squash merge is partially addressed in the following issue of Git Weekly:

In this issue, we're going to talk about the ways one squashes a few commits together manually.

Manual Squash

Manual squash is possible through "interactive rebasing". Let's assume we want to squash 5 last commits together. We'll then do an interactive rebase on the last 5 commits:

git rebase -i HEAD~5        

This command will start the interactive rebase. An editor (e.g.?nano) with content similar to what follows will launch:

pick f72e7cb Feat: Resurrect flatMap
pick 1a18b1c Feat: Add list transformations
pick 3420af4 3.0.0
pick f55a9ee Docs: Add marble diagrams to README
pick 0433f9d Update README.md        

Keep the first line as is and replace every?pick?in the following lines with an?s?(for squash):

pick f72e7cb Feat: Resurrect flatMap
s 1a18b1c Feat: Add list transformations
s 3420af4 3.0.0
s f55a9ee Docs: Add marble diagrams to README
s 0433f9d Update README.md        

Save and exit. Then the editor launches once more with the commit messages of all 5 commits. Feel free to delete it all and rewrite the whole message. Save and exit. And that would conclude the interactive rebase.

To verify that the squash was complete, do a git log:

git log        

After the squash is done, if you want to push to your remote branch and any of the 5 commits were already pushed, you have to force it:

git push --force-with-lease        

Notes

  • Always push with --force-with-lease. Never use --force.
  • In the interactive rebase commit picker, you can write?squash?instead of?s. But I guess a simple?s?would be easier to write down.
  • The alternative would be to write fixup or f, which then discards the commit's message in the next step.
  • To learn more about auto-squash, please refer to my "16 Git Tips and Tricks". I'll write more about it in detail.
  • Subscribe?to my Medium publishes to get notified when a new Git Weekly issue is published.
  • Follow?me on Twitter?for weekly articles and daily tweets on git.

要查看或添加评论,请登录

Mohammad-Ali A'R?BI的更多文章

  • 7 Noob Git Tips

    7 Noob Git Tips

    We will start 2025 one month into the year with seven basic git tips. This issue is based on a Twitter series that I…

  • 7 Basic Git Commands

    7 Basic Git Commands

    In this Git Weekly issue, we'll cover what basic commands are used when committing directly to the main branch. As a…

  • Git Submodule Update

    Git Submodule Update

    Halloween is here and we're back with Git Weekly #8, another piece on the spine-chilling git submodules. ?? Submodules…

  • Git Submodules

    Git Submodules

    Welcome to Git Weekly #7. This week, we will discuss having git repos within git repos.

  • Git Get One File From Another Repo

    Git Get One File From Another Repo

    Welcome to Issue #6 of Git Weekly! This week we're going to talk file and moving them around. Let's assume you have a…

  • Git Merge vs Rebase and Where to Use Them

    Git Merge vs Rebase and Where to Use Them

    There are two workflows for merging a feature branch into the master branch, namely 'rebase and fast-forward' and…

  • Git Merge vs Rebase: The Three Types of Merge

    Git Merge vs Rebase: The Three Types of Merge

    Merging is adding the work done in one branch to another. There are three ways one could merge one branch into another:…

    2 条评论
  • Change a Commit's Author on Git

    Change a Commit's Author on Git

    I usually commit to my work and to my private git projects, and I use two different email addresses for them. But it…

    4 条评论
  • Determine the Git Branch You're On

    Determine the Git Branch You're On

    Branching is one of the most used features of git. People usually change branches all the time and need to determine…

社区洞察

其他会员也浏览了