Happy new year you all! Have you thought about your new aims of 2016? One of mine is trying to write a post each 2 weeks, so let’s start with it!

In this post I will show the basic workflow and commands that anyone should know when using Git in their projects.

Beggining

Git is a version control software which help us adding “checkpoints” to our code for letting us recover the code if we save a code with mistakes. This functionality will help us in other tasks of a coder’s life like teamworking or testing new software features. Let’s see.

Git is commonly used as a command line tool but there is a lot of softwares with a GUI which can make the use of the version control much easier. We will use the command line tool.

When you are working with git you must initialize a git repository of your software, for doing this you should navigate to the root directory of your project and run:

$ git init

Once the repository is ready you could create a file containing the information about which directories should not be included, this file must be named as .gitignore and you can create it following the official guide.

Now is the time to add files to your repository, git offers a simple command for this: add. Using it you can add single files manually or add all files recursively using add . or add -A. So we are going to add any file in our project ( we’re not including files ignored in .gitignore) using:

When we have all our files added we can save the state of this files in a checkpoint commiting them. For this we must use the commit command and we will add a message to this commit which will allow us difference it from others and know what files where modified and why.

$ git commit

We will be asked for adding the commit message and when you are ready just finish the commit. When commiting we can use the “-m” flag for adding fast message like:

$ git commit -m "Fix infinite loop error on loop.js"

It is highly recommended for any develop to follow good practices when writing commit messages. Use a standard will allow others using the same repository (when participating in a team you will use a common remote repository) identify why the code was commited. Here you have a funny guide explaining why commit messages are so important and some good practices.

Branches

A branch is a way to “freeze” your actual code in a point you want and start working in another feature or problem at the time that you are able to change to your frozen code. The frozen code here act as the base of the new code and you can create as many branches as you want from a same base. Using this you can work in different tasks in your code separately and organize your code easily. Branches can be created easily and can be easily removed too so it’s just a basic and great tool to use and, as any basic tool, you must understand how to work with it.

To manage and naveigate between branches there are two commands to know: branch and checkout.

Git branch

With this command you can create, rename and delete branches. Let’s see how to do it.

To add a new branch you can use:

$ git branch <name>

$ git branch bugfix

To rename a branch you must be in that branch and type

$ git branch <new_name>

To delete a branch just run:

$ git branch -d <branch_name>

This will show an alert if there are changes in your code which are not registered so if you want remove it fully without saving changes you can force it using:

$ git branch -D <branch_name>

Git checkout

With checkout you can navigate between branches and even create them.

For navigating you should run checkout with the name of the branch you want to navigate to:

$ git checkout <branch_name>

$ git checkout bugfix

You can create and navigate and create a new branch at the same time using:

$ git checkout -b <branch_name>

If that branch already exists you will stay in the same branch you were and an alert message will be shwon in the terminal. You can force resetting a branch to your actual branch and moving to it using

$ git checkout -B <branch_name>

Git merge

When you have finish your task in a branch and want to update master (main branch) with it content you must use merge. Merge will mix the code of both branches automatically and if a conflict appears you will be warned in the terminal log and you will need to modify that files solve the conflicts and commit the fixed code.

To merge a branch you must move to the branch you want to merge to (if you want to update master to reflect bugfix you should checkout to master) and run merge:

$ git checkout master

$ git merge bugfix

With this you are ready to use Git locally with its basic use, in a next post I will explain how to use it remotely and some commands useful in team work.