DEV Community

Cover image for Avoid Common Pitfalls: Octocat's Tips for Better Git Commits๐Ÿ™ ๐Ÿฑ๐Ÿˆ
JohnKagunda
JohnKagunda

Posted on

Avoid Common Pitfalls: Octocat's Tips for Better Git Commits๐Ÿ™ ๐Ÿฑ๐Ÿˆ

Well, if you are reading this article we can agree that our git commits are not what we are most proud of. Mostly, it's because of the idea that we are just putting up our code in GitHub or a remote repo.

Quick Fact: Do you know you can make git commits without the use of GitHub, GitLab, or any remote repository?

Try it:

mkdir my_project
cd my_project
git init
touch README.md
git add README.md
git commit -m "Added a README file"
Enter fullscreen mode Exit fullscreen mode

Now you have a git repository on your local machine, you can write code and commit some more with no worries. If you want to keep track just use git log to see the commit history.

Then why do we need git commits?

  • Well, commits just help you keep track of different versions of your code. It is like a time machine for your code. You can always go back to a previous version of your code if you want to.

Well, now with this ideology we can now all agree commits are important.

  • Commit messages are like the ID of different versions of you. You wouldn't want to go to the past and when you try to check the different versions you, all you have as the ID is Small changes or WIP. That would be a nightmare.

So what makes a good commit message?

  1. Short and sweet: A commit message should be short and sweet.
  2. Meaningful: A commit message should be meaningful.
  3. Consistent: A commit message should be consistent.
  4. Use the imperative mood: A commit message should use the imperative mood.

Let's see comparisons between good commit messages and bad commit messages:

Aspect Bad Commit Message Good Commit Message
Clarity "Fix" "Fix null pointer exception in user login"
Specificity "Bug fix" "Fix bug causing crash when username is empty"
Context "Updated" "Update dependencies to latest versions"
Feature Addition "Added new feature" "Add search functionality to the product catalog"
Initial Commit "Initial commit" "Initial commit: setup project structure and dependencies"
  • Well, now when it comes to maintaining your codebase with good commit history, you can always go back to a previous version of your code and see what you did in that commit.
  • You can also see the commit message and understand what you did in that commit.

Conclusion

So, in conclusion, commit messages are important.

  • They help you keep track of different versions of your code.
  • They help you understand what you did in that commit.
  • They help you maintain your codebase.

So, next time you make a commit, make sure you commit to its message first.

Top comments (0)