Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 17, 2021 10:28 pm GMT

Git: Are you an over-committer? Squash those commits!

Photo by Christin Hume on Unsplash

Photo by Christin Hume on Unsplash

Are you an over-committer? Me too. Basically, anytime I complete some kind of task, I commit it with a message that details what Ive done. I do this for a few reasons:

  • Ive read over and over again: Commit a lot, commit often
  • I have kids and work at home. As Im working, my kids could need me immediately at any second, and I want to make sure my work is saved.
  • Those crazy kids also sometimes get access to the computer and get happy fingers while my projects are open, so I want to make sure Ive got it all backed up.
  • I often find myself working in short stints and having lots of commit messages helps me retrace my steps and offers me a sort of progress report.

I was recently told by someone who was reviewing my code that I commit too frequently. He suggested, If you feel that it is helpful for you [to make incremental commits], I would recommend you check out squashing commits with Git.

After researching what squashing commits even meant, I realized it was the perfect way for me to have my cake and eat it too! It allows you to make as many smaller commits locally as you feel necessary, based on your own preference and workflow, and then squash them down into one clean commit so that your remote repos commit history is nice and tidy!

Below, Ive laid out a step-by-step guide on how to squash commits that can be followed by any Code Newbie!

Steps to squash your commits:

WARNING! Before you start, keep in mind that you should squash your commits BEFORE you ever push your changes to a remote repository. If you rewrite your history once others have made changes to it, youre asking for trouble or conflicts. Potentially lots of them!

  • Create and checkout a new branch where you will write your code: git checkout -b [your_branch_here]
  • As you work on your project, make as many commits as youd like!
  • Find out how many commits you want to squash! You will need the number of commits so that you can tell Git how far to go back during your rebase. The command below will get you the log of commits not including any that are available to master and you can count how many youd like to squash: git log [your_branch_here] not master
  • Run an interactive rebase. In the sample below, replace X with the number of commits you want to squash. This will rebase you X commits back: git rebase -i HEAD~X
  • The text editor will pop up and list your commits. Shift + i to type. Replace pick with squash on the most recent commits. Make sure the first commit is still preceded with the pick command. This basically tells Git to squash all of your most recent commits into your first commit.
pick first_commitsquash second_commitsquash third_commitsquash fourth_commit# Your text editor will give you a bunch of other instructions and options here. Check them out. There is some really helpful information.
  • esc to escape insert mode.
  • :wq to save your changes. (VIM)
  • Another text editor screen will pop up and allow you to write a new commit message that will sum up all of your previous commit messages. Comment out all of the previous commit messages and write your new one.
# This is a combination of X commits.# The first commits message is:# First commit message# This is the 2nd commit message:# Second commit message# This is the 3rd commit message:# Third commit message# This is the 4th commit message:# Fourth commit messageNew commit message here# Git will list some other instructions and the changes youre committing here.
  • :wq to save and quit. (VIM)
  • You can double check that it worked by running git log again. You should see only your new single commit!

There are multiple variations of this process and countless workflows and commit preferences among us. I am really interested in how you commit and how you squash (if you do). Please share your experiences so that we can all become more productive and efficient developers and team players!


Original Link: https://dev.to/the_real_stacie/git-are-you-an-over-committer-squash-those-commits-2klk

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To