✅ Quick Tip: Squashing Commits
Ah, Github. Mostly, I feel good about it…
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.
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.
Open your terminal and input the rebase command:
git rebase -i HEAD~4
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.
If the commits are correct, type the following:
i //this is the insert command for vim. This allows you to edit.
All of the commits will have the word
pick
associated with them. Leave the first line. Replacepick
withsquash
for the rest of the commits. No worries. You will not lose any work.Use the
esc
key and then type:wq // this saves your changes
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.”
Use the same vim commands to edit and save your changes
i // you are now in edit mode Add a commit message that is readable both for your future self and for other members on your team esc key // this takes you out of edit mode :wq // this saves your work and exits you out of vim
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…
Written with Sun frequencies (1 hour) - Healing guitar music - Oneness and Safety Within playing in the background