30 October 2019
Reading time: 9 minutes
Victor Domingos
Tutorials

When doing software or web development, you should have your code (and even other kinds of files) managed by a version control system, so that you are able to backup meaningful changes, checkout a file from a previous version, or collaborate with your team more efficiently. Over the years there have been some tools that serve for this purpose, but these days Git seems to be the most popular version tracking system.
Basically, Git provides a way to manage multiple versions of the files in your project. With remote repositories, you’ll have the additional benefits of a permanent off-site backup and powerful collaboration tools.
Please keep in mind that, while there are a few famous online services that allow you to create remote git repositories (for instance, GitHub, GitLab, Sourceforge, among others), they are not the same thing as Git, which is one of the many technologies they use. Git can also be used independently, but you may arrive to the conclusion that having a private or public remote repository can become very handy in a number of situations. And, in fact, those websites offer some additional features that are useful, like publicly accessible issue trackers for open source projects, easy to use web interfaces for code review and, perhaps even more important, great open source developer communities that help each other in a lot of cool projects.
Before you begin: install git
git
is a command-line application which can be installed in most operating systems and which allows us to interact with git repositories. There are Integrated Development Environments (IDE) and other specialized applications that offer graphical user interfaces (GUI), but at this time it is enough to use the command-line shell, which by the way is often a more practical tool.
You can download the installers for the git
command from the website git-scm.com, where you can also find useful documentation on how to use it. If you work with Visual Studio on Windows, you can also install Git from the Visual Studio Installer application.
Creating a local copy (clone) of a repository
When we need to create a local copy of an existing remote repository, in order to be able to explore its code and documentation in our own computer, to compile and use an application, or to start collaborating with that project, we use the command git clone
:
1 2 3 4 5 6 7 |
|
This command will create a new folder inside the current working directory, and inside it there will be a copy of all the files from that repository. This copy is what we call a clone. So, it may be a good idea to start by using the cd
command first, to change the working directory to the folder where we want to keep our git
repositories.
Creating a new local repository
If what you want is to create a new (empty) repository in your computer, even if you intend to make it available later on GitHub or any other server, first you need to create the project’s folder (in case it does not exist yet). Then, use the command cd
to get into that folder. Finally, the following command will initialize a new local repository on that project’s folder:
1 2 3 4 |
|
Creating a new local repository from an existing folder
If you already have in your computer a folder that you want to transform into a repository, that’s also possible. First, you need to initialize the repository with git init
and then add all of its content git add .
(this command adds all the content of the current folder into the staging area). The git commit
command, which we will show in a section below, will save that initial repository state.
Setting up a list of files and folders to ignore
During software development it is very common to have files and folders that don’t need to be added to the version control system. For instance, temporary files that are created when you compile an application, some generated binary executable files, and many others. The easiest way to automatically exclude all those files is to add a file named .gitignore
to the project’s root directory.
The website www.gitignore.io makes it very easy to generate that file based on your operating system, programming language and/or the IDE you are using. In this GitHub repository, there are also dozens of ready to use .gitignore
files, including one for Visual Studio.
Getting the most recent changes from the remote repository
In order to make sure you’re working with the most recent version of the project, I mean, to be sure that the files in your local repository is synchronized with the remote repository, start by going into the repository folder and then use the git fetch
command.
1 2 3 4 5 6 7 8 9 |
|
The command git status
allows you to check if there are any pending changes:
1 2 3 4 5 6 |
|
In case there are more recent changes in the remote repository, the command git pull
allows us to get our local repository up to date with those changes.
1 2 3 4 5 6 7 8 9 10 11 |
|
Sending local changes to the remote repository
After making changes to our local repository, like for instance file creation, deletion or renaming, or file content changes, we can save the current state performing an operation called commit
. But first we need to prepare that commit
, adding the folders and files that we want to include:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Once again, the git status
command allows us to check for any pending operations, or as in this case, any changes being staged to commit:
1 2 3 4 5 6 7 8 |
|
By entering git commit
we then save the current repository state (with regard to the files and folders we previously added with git add
):
1 2 3 4 |
|
At this moment, a quick check to the output of git status
clarifies that even though there aren’t any files or folders to register into a new commit
, our repository is ahead of the remote repository:
1 2 3 4 5 6 |
|
In order to send these changes to the remote repository (which, in this example, is hosted on GitHub), we just need to use the git push
command:
1 2 3 4 5 6 7 8 9 10 |
|
Final thoughts
So, there you have it, a short guide to get started with Git, which is both powerful and pretty complex tool. There are many aspects of Git that couldn’t fit in an introductory guide like this, and many other features that I confess I am still learning each day. However, by using these simple commands you can already make a basic use of Git in your own projects, and I hope it will help you by serving as a base to start using and learning it.