Using Git 101 — Part 1

Nelson Rodrigues
by the way, Hi
Published in
5 min readJul 12, 2018
“Colorful lines of code on a computer screen” by Sai Kiran Anagani on Unsplash

Git is a powerful tool for developers, especially when working in a team where the code base is shared. It helps you code with more confidence and try new ideas easier, without risking losing your hard work. But it’s a tool that can take a while to learn how to use it with ease.

In this series we’ll learn about Git, why should you use it and the most common scenarios you’ll encounter when working with it. Hopefully, by the end of the series you’ll have a better grasp on how Git works and feel more comfortable with it.

What is Git?

Git is a version control system. In other words, a program that when installed in your computer can be added to your software projects to create a way of tracking the changes in the source code, this is called a Git repository. It allows you to code with greater confidence because all changes are recorded and labeled with a useful description (most of the times).

Git also allows collaboration by the use of remote repositories. Services like the popular Github and Gitlab allow you to create free remote repositories to easily store and share your projects with other developers. You can also create your own private repository (ex.: inside your own company) to ease the collaboration with your coworkers.

Installing Git

The first step to use Git is to install it in your machine (if you already have Git installed, feel free to skip ahead).

There are multiple ways of installing Git in your computer but one of the easiest is just by going to the official site https://git-scm.com and hitting the download button.

The official Git website

After the installation is complete you’ll gain access to the git command in the Terminal and Windows Command Line. Learning how to use Git in the terminal is a great tool to add to your tool belt as a developer because once you learn it, it can be used everywhere no matter the platform you’re working with.

Even so, if using the terminal is not your cup of tea, there are also GUI clients available for Git. You can check some of those here: https://git-scm.com/downloads/guis.

In this guide we’ll use the terminal but you should be able to follow using a GUI client without any problems.

Adding Git to our project

Now with Git installed in our machine, it’s finally time to add it to our project. For this we’ll create a fake To-Do app. Let’s start by creating our project folder (you’re free to name it however you want), I’ll name mine todo-app.

For our app we’ll be just using a text file. Create a new text file inside the project folder and name it todo.txt. It is empty for now but we’ll use this to see how Git works. With our setup completed let’s add Git to our project.

To add Git to our project will use a command in the Terminal, type the following command inside the project folder: git init.

Adding Git to our project

We can see that Git tells us that it created a new empty repository and added a new folder named .git. in our project. This is where Git will store all the recorded changes that we make in our project. We don’t usually interact directly with what’s inside the folder and removing it will essentially remove Git from our project, so be careful when dealing with this folder.

Our first commit

Now that we added Git to our project, let’s see how we actually use Git to keep record of the changes in our project.

Git allows us to, at anytime, check the status of changes in our project by using the command git status.

Using git status to check the changes

Using git status Git tells us that right now we have one untracked file, our todo.txt file. This means that Git it’s not yet tracking the changes of this file and we could change its contents and Git would know nothing about it. To change this we need to tell Git to start tracking this file.

We do this by using the git add <file> command. You can also use git add . to add all untracked and/or changed files, let’s use that.

Using the git add command to stage a new file

When we use git add command, Git “moves” the selected files to a special zone. This is called the staging zone and it’s a temporary zone where Git keeps the files ready to be committed (we’ll get into that in just a moment). Imagine the staging zone as your laundry basket. You put your clothes into the laundry basket when they’re dirty and then, when you’re ready, you throw them into the washing machine.

Now that we added the files into the staging zone, we’re ready to record our changes. For this we use the git commit command. In your terminal type the following: git commit -m "First commit".

Using git commit to create our first commit

What do we did here? A commit is the final step in registering our changes. Every time we commit one or more files we’re creating a snapshot of how those files looked in a specific place in time. As we keep creating these snapshots of our code, we’re essentially creating a timeline of how our project evolved over time.

You’re probably wondering what the -m "First commit" part in the commit command does? The -m parameter is used to specify a message that identifies the commit and the text in double quotes is the message itself. A commit always comes accompanied by a commit message.

Whenever we commit, it’s important to write a descriptive message of the changes made to the project. We’ll talk more about this and other best practices later but first, let’s review the steps that we did:

  1. We used git status to check for new/changed files
  2. Then we used git add to prepare the files to be committed
  3. Finally, we used git commit to record the changes made

These three steps are the core of using Git and you should get familiar with them and how they work.

With this, we reached the end of the first part of this series. Hope you’re enjoying it so far. In the next part we’ll learn how to use Git to see what changed in a file, how to use a remote repository and more. See you there!

--

--