Version Control with Git: Get Started in Less Than 15 Minutes | by CR Ferreira | Towards Data Science

Do you often hear about Git (or maybe GitHub) but have no idea what that means, what it is good for, or how it works? You’ve come to the right place.
In this tutorial, you will learn how to use the most basic Git commands — the ones that you will need to know most of the time when working with Git. More specifically, here you’ll learn how to create a Git repository for your project and how to use it to document all future changes in that project. And completing this tutorial should take less than fifteen minutes of your time, assuming that you won’t have any problems with the installation process (which I hope most people won’t). So let’s get right into it.
In a nutshell, Git is an open-source Version Control System that allows you to keep track of your project changes over time. For instance, with Git you can easily check what was the state of each file in the project at a given time, and even revert it to that state if you want to. It also makes it much easier to manage projects in which multiple people are working simultaneously, each one on their own computers.
Git is not the only Version Control System available (other examples: SVN, CVS, Mercurial), but it’s the most used one nowadays. If you are a software developer, web designer, data scientist, or work on basically any other kind of project involving text files, learning how to use Git will surely be a very valuable skill for you.
In this tutorial, we’ll be running Git commands from the command-line terminal (also known as command prompt in Windows), so it can be helpful if you already have some experience with using the terminal. But don’t worry if you have never used it before — you will still be able to follow this tutorial.
First, let’s open a command-line terminal on your computer. Here are some quick ways to do it depending on your operating system:
Windows: press [⊞ Win]+[R], then type cmd and press enter.
Linux: press [Ctrl]+[Alt]+[T].
MacOS: press [Command]+[Space], then type terminal and press enter.
We’ll start by checking if you already have Git installed on your computer — and if it isn’t, we’ll install it. Type git --version on the terminal and press enter. If Git is installed, you’ll see the currently installed version, as in the screenshot below. If you already have Git, you may skip directly to the next section, “Setting up your data”.
If you get something like git: command not found, then you will need to install Git. On MacOS, running git --version may already prompt some installation instructions, and you will just need to follow them. If that’s not your case, follow the instructions below, depending on your operating system:
Windows: install Git for Windows.
Linux: run sudo apt-get install git on the terminal.
MacOS: install Git for Mac.
After installation, you need to configure your name and e-mail — they will be used to identify you in your “commits” (I’ll explain what they are later). Do it by running these two commands in the terminal with your personal data:
git config --global user.name "Your Name"git config --global user.email "your@email.com"
That’s it. Now you are ready to start using Git!
While Git is mostly used by programmers, it can actually be used for any project with one or more computer files. In this tutorial, you will use Git to manage a project containing two simple text files: one with a shopping list and another with a to-do list. The commands you’ll use to manage this project are exactly the same as what you would use for a programming project.
Let’s start by creating the project directory and two text files in it. Create a directory named my_lists somewhere in your hard drive and create two text files inside it, with the following content:
shopping_list.txt:
- Toilet paper (10 rolls) - Beer (12 bottles)- Food
to-do_list.txt:
- Meet friends- Go shopping - Learn Git
This will be our project’s initial state.
Using the terminal, navigate to my_lists, the directory you’ve just created. You can do that with the command cd. If you don’t know how to do navigate using the terminal, here are some instructions: On Windows / On Linux or MacOS. After navigating to my_lists, if you run ls (on Linux/MacOS) or dir (on Windows), you should be able to see the two files you created.
Now, run git init to make your project directory officially a Git repository. After that, you can run git status from inside that directory at any moment to check what’s happening within it. If you do it now, you will see that your repository has “no commits yet” and that the two files are marked as “untracked”:
To make those files start being tracked by Git, you need to “add” them to the repository. You will use the command git add for that. Any of these options will do the trick:
Run git add shopping_list.txt and then git add to-do_list.txt to add each file at a time; or
run git add shopping_list.txt to-do_list.txt to add both files with a single command; or
run either git add . or git add -A to add all files in the current directory at once.
After adding the files, run git status again and you should see something like this:
Now the two files are in what is called the “staging area”. This means that those files will be included in the next commit you make.
In Git, a commit is a snapshot of the project state at a given time. Think of it as a version of the project that will be stored forever, such that in the future it will be easy for you to see what has changed since then, and you’ll even be able to revert the project back to that state if you want. Every time you make important changes in your project you should create a new commit, so that your work gets registered in the project’s history.
After adding the two files to the staging area, you can create your first commit with the command git commit -m "Your commit message" . Be sure to always write a meaningful commit message describing what has changed, because that message will be seen by anyone checking the project history.
After committing, run git log. This will show your commit history. At this point, you should only have one commit in the log, like this:
Note: you can also run the command git commit without the -m option — this will open a text editor that will ask you to input a commit message. But be careful, because depending on your system this may open a command-line text editor like vim, which can be hard to exit if you don’t know how to use it. You can also change the default text editor with the command git config --global core.editor <editor> .
After creating a Git repository for your project, the next step is to use it to document future changes. So let’s get back to our example: suppose that you’ve just found out about a major virus outbreak happening in your city and that forced you to modify your shopping and to-do lists, as shown below:
shopping_list.txt:
- Toilet paper (100 rolls)- Beer (120 bottles)- Food- Hand sanitizer
to-do_list.txt:
- Go shopping - Wash hands- Learn Git
As before, you can check the repository’s current state with the command git status. This will show that Git has detected changes in both files:
But you can also have more details on what has changed since your last commit with git diff. This will show all that has changed in all files, line-by-line:
Usually, unchanged lines are printed white, while new lines are green, and deleted lines are red. Note that lines that have been modified actually show up as if the old version had been deleted and a new line had been added.
To create a new commit with those changes, the process is the same as what you did to create the first commit: you’ll first use git add and then git commit.
While you could create a single commit for the changes in the two files, let’s create one commit for each file. First, run git add shopping_list.txt to add this file to the staging area. After that, you can check which changes have been added to the staging area with git diff --staged. This command will show exactly which changes will be committed if you run git commit now.
If everything is as expected, now create a new commit with those staged changes by running git commit, just like you did when creating your first commit. Again, make sure to include a meaningful commit message. After that, as an exercise, you can create a third commit with the changes in the file list.txt.
If you’ve done everything correctly, you now have three commits in your repository. You can check that with git log, and you should see something like this:
That’s it. Now you know how to create a Git repository for your project and how to make new commits for all your future changes. In general, most of the time you work with Git you will be using these few commands that you’ve just learned.
Of course, there’s a lot more to Git than that — the goal of this tutorial was only to get you started with the basic workflow described above. Here is a list of a few other things you can do with Git (including links with further details):
You can see which changes were introduced by a specific commit with git show.
You can undo all your uncommitted changes with git reset (be careful with this one).
You can revert all changes introduced by a specific commit with git revert.
You can temporary “stash” your changes with git stash.
You can revert your project to an older state with git checkout.
You can manage multiple features being implemented at the same time with Git branches.
You can share your project and/or collaborate with other people using a remote Git platform like GitHub or GitLab.
In general, you can find very helpful advice on how to use Git in online forums like StackOverflow. So if you have no idea how to do something with Git, you can usually learn a lot by simply searching about that online.
Git: A fast and easy guide to version control, by Christian Leonardo.
Beginning Git and GitHub: A Comprehensive Guide to Version Control, Project Management, and Teamwork for the New Developer, by Mariot Tsitoara.
For a couple of other useful commands, check 10 Git Commands You Should Know.
If you want to learn how to use GitHub, check Introduction to GitHub for Data Scientists.
medium.com
towardsdatascience.com
levelup.gitconnected.com
Disclosure: This post contains one or more links from the Amazon Services LLC Associates Program. As an affiliate, I get commissions for purchases made through those links, at no extra cost to the customer.