Learn Git good practices!
Tags: webdev, git
Published: 2020-09-17 19:45:16

How well do you know GIT?

Learning git is one of the most important things that programmers have to know. You need to know how what it is, how to use it in your project, what you should do, and what you should not do.

In this post, I prepared eight things that you should know. These are not all of the best practices that you should know, but these are the ones that will make you a better developer instantly.

### The eight GIT best practices that you should know are:

# 1\. Make clean commits

Don't do too much on one commit. If you fixed one bug related to one thing, and then another bug related to another thing pops out, don't include it in your commit.

Fix one bug, commit, fix the second bug, commit, and so on (only if the bugs are not related to each other).

# 2\. Good commit messages

Capitalized, meaningful commits in the present tense is the best commit message practice. It's good both for others, such as visitors, developers, recruiters, clients, and also for you.

You could stand out from other developers only by improving your commit messages game.

Let's say that you added responsiveness to your project, which is a website.

### Bad:

added responsive mobile version

### Good:

Add responsiveness to the website.

# 3\. Commit often

Rather than making the first commit your project's last commit, create small commits every meaningful update you make.

That doesn't mean your every new line of code needs to be committed singly. Here's an example:

### Bad:

body {};

### Good:

body { margin: 0; width: 100vw; font-size: 1rem; };

# 4\. Don't commit keys

If you need to commit secret API keys or anything that people shouldn't be able to see on your GitHub project's repository, you can use environment variables.

Within a React project, install a dotenv package and create a ".env" file in the root directory. Then, you can place there a key and its value, like:

### REACT\_APP\_YOUTUBE\_API=xxxxxxxxxxxxxxxx;

Then in your project, you can use process.env.REACT\_APP\_YOUTUBE\_API to get that from your local file and no one will be able to see that key when you commit the file.

# 5\. Use branches

Branches can allow you to have multiple versions of your code in a single repository.

A simple example in which I used branches on my portfolio website's project:

In the repository, I have three branches: master, md-blog, and no-blog, which have different codes depending on their name. The Master branch is the version with a Contentful blog, md-blog stands for Markdown blog, and no-blog is nothing else than a no blog version of the portfolio.

By having them, when I want to change something minor on all of them, I simply head on one of them, and after doing the commit I merge changes with the rest of the branches.

# 6\. Check security alerts

If you want to make sure your app has no vulnerabilities, you can use the GitHub's Data services to receive alerts when one or more of your project's dependencies will need to be fixed automatically.

In that case, I would suggest you visiting regularly GitHub and checking the notifications. If there will be something like: "Bump x from 1.0.0 to 1.0.1, you need to click on that and merge the automatic changes.

# 7\. Create aliases

Improve your productivity by using Git aliases. The most common ones are:

$ git config --global alias.co checkout

$ git config --global alias.br branch

$ git config --global alias.ci commit

$ git config --global alias.st status

You can always create aliases on your own to make your git flow more efficient. A lot of people share their settings online, so if you will find something interesting, you can use that in your terminal as well!

# 8\. Small cheat sheet

### 1\. Adds a file to your previous commit

$ git commit --amend

## 2\. Rather than staging everything, you can stage only parts of a changed file

$ git add .

$ git add -p

## 3\. Check file changes

$ git diff --cached

# That's it!

Thank you so much, and have a productive day!