✅ Quick Tip: Squashing Commits

Ah, Github. Mostly, I feel good about it…

Computer Reaction GIF by GitHub

I know the basics and use them frequently. Recently, I learned how to squash commits. And, for whatever reason, I cannot recall the commands quickly. Here’s my guide to running the commands for squashing commits.

There are many great articles expalining squashing in great detail. I recommend this answer in Stack Overflow or FreeCodeCamp’s Git Squash Commits.

NOTE: This post assumes you are familiar with basics like working in the terminal, using vim, and using git commands. I’m also using a Mac.

  1. In Github, look at your current branch. The number of total commits is the number you will need for Step 2. So, if you have 4 commits, you will need to rebase the same number of commits in the terminal.

  2. Open your terminal and input the rebase command:

  3. The next screen displaying in your terminal will show the commits related to the number you used for rebasing. Double-check to ensure these are the commits you want in your PR.

  4. If the commits are correct, type the following:

  5. All of the commits will have the word pick associated with them. Leave the first line. Replace pick with squash for the rest of the commits. No worries. You will not lose any work.

  6. Use the esc key and then type

  7. The next screen provides you the opportunity to input whatever commit message you need. Type the commit message above the line that says “# Please enter the commit message for your changes.”

  8. Use the same vim commands to edit and save your changes

  9. At this point, you will probably need to do a git push —-force. Now you can look at your branch in GitHub and verify that your changes only contain one commit along with your commit message.

And, we did it! Git can feel daunting. But, most everything on Github is retrievable. And, if at any point you are afraid you messed something up, feel free to reach out to your development team. Someone will be happy to help.

Happy squashing!!!

Till next time…

Woman Coding GIF by Pluralsight

Reply

or to participate.