Best practices to use Git like a pro
Tags: git, beginners, programming, productivity
Published: 2023-02-19 19:44:44

Welcome to the wonderful world of Git 🚀, where we can store versions of our code without the hassle of endless folders named "version1", "version2", "version3", and so on.

Git is an incredibly useful tool for developers, but it can be intimidating at first. In this article, we'll talk about the best practices to adopt if you want to use it like a pro!

## 1. Don't store unnecessary files

Imagine you have a folder full of useless files, like "Untitled-1.txt", "Trying out stuff.txt", "README-copy.md", etc. When you use Git to commit your changes, it will include them in your project history. This means that these useless files will be stored forever in your project history.

The solution? Add a `.gitignore` file to the root of your project and list all the files and folders you don't want to include in the Git history. For the lazy, there are even `.gitignore` templates available online for different types of projects.

> For example : [https://github.com/github/gitignore](https://github.com/github/gitignore), [https://github.com/toptal/gitignore/tree/master/templates](https://github.com/toptal/gitignore/tree/master/templates)

## 2. Regular and atomic commits

When working on a feature or a bugfix, don't forget to make regular commits. This saves your work and makes it easier to understand by other team members (and/or yourself).

Atomic commits are commits that contain only one logical change to your code. If you make multiple changes at the same time, it can be difficult to understand which commit caused which change (I've experienced this unfortunately 🥲). Especially when using atomic commits, it's easier to see the history of code changes.

## 3. Use branches

Branches are copies of your code that you can work on without affecting the main branch. This way, you can test features without affecting the main code and merge changes only when you are sure that everything works fine.

When you create a branch, it's a good idea to name it after the functionality you're developing.

❌ Avoid generic branch names like `branch-1` or `branch-2`.

✅ Prefer more specific names like `feature-payment` or `bug-page-connection`. It's easier to find your way through the branch history.

> "Good to know: *Never push directly on the main branch*"


## 4. Use meaningful commit messages

When you make a commit, the associated message should be clear and precise to explain the change you made.

❌ Avoid unnecessary commit messages like "modify" or "bug fix." They do not provide enough information to understand the changes made.

✅ Prefer more specific messages like "Fixed login page bug" or "Added contact form validation". Use action verbs in your commit messages to describe what the change does: add to, remove from, fix, etc.

## 5. Make pull requests to merge your branches

A pull request is a branch merge request that allows other developers to see the changes made, test them, and give feedback. It is also a good practice to request a code review before merging a branch.

## 6. Make regular backups

Imagine working hard for hours or even days on a project and losing everything due to a hard drive failure or a branch merge error. This can be devastating to your motivation and productivity. So it's crucial to take the time to make regular backups of your code, whether it's using online storage services like GitHub or GitLab, backing up to external drives, or using cloud backup services.


## Conclusion

Now you know the best practices to use Git like a pro! If you follow these tips, you'll avoid common Git pitfalls and be able to develop cleaner, more maintainable projects.

I hope this article was helpful and gave you some tips on how to use Git well and avoid common problems. If you have any questions, comments, or ideas for improvement, feel free to share them in the comments. And if you liked this article, don't forget to share it with your developer friends to help them use Git better.