Skip to content

Follow These Best Practices for Using Git

As a developer, working in a version control system is a basic necessity as soon as you start any project. The 2021 Technology Survey by Stack Overflow shows Git as the most popular technology tool, reinforcing how important it is to understand, and use it correctly, in your projects.

This article compiles some tips and hopefully good advice when working with Git.

Never push to the main branch.

"The Git feature that really makes it stand apart from nearly every other SCM out there is its branching model." - from git-scm

Pushing directly to the main branch doesn't make much sense in Git as it doesn't promote collaboration.

Instead, make use of merges/rebases from pull requests that most providers have (GitHub, AWS CodeCommit, etc.).

Define patterns & standards within your team

Every team has its patterns and standards for naming conventions, tags, and commit messages. Those should be followed and pointed out in all pull-request reviews.

If you don't have any starting pattern, here are a couple of suggestions:

  • branch name: user_name/ticket_number,-short_description_dashed

    • example: mimurawil/1,-my-first-commit
  • commit title: type(scope): short_description

    • type: feat, fix, revert, etc

    • scope: ticket number (can be prefixed by "#")

    • example: fix(#1): fix broken build

Write useful commit messages

Try to focus more on "why" and "what" instead of "how". You are spending a bit more time to complete your commit, but it will pay back when your future self or teammates revisit that piece of code.

fix(#42): use proper url prefix on social share images

During build time, our pages don't know or don't have access to the
`location` props, making the prefix for our image URL's to be `undefined`.
Fixing this by using the proper `SITE_URL` from our env variables.

ref: ticket #42 link

Rebase your branch frequently

Rebasing your branch frequently ensures you are always working with the latest version from the main branch. Your teammates will review the correct changes, and you will usually encounter no conflicts when merging to the main branch.

# -i for interactive rebase
git rebase -i main

(make sure you have your local main branch updated from remote)

Make use of Git commands

A few Git commands that I found incredibly useful in my journey:

  • git revert: creates a new commit that reverts changes from a specific commit

  • git stash: stores the changes and goes back to a clean working directory

  • git cherry-pick: picks one specific commit and adds it on top of your current branch

  • git bisect: help you find a specific commit in the history that added a bug in your codebase