Git for beginners

Why am I writing this article?

I want to give you a short overview of Git – a version control system. I’ll explain this in more detail later on.logo@2x

Since you’re reading this article, I assume you’re one of those people who’re well-versed in web programming, web design or perhaps web graphics. No matter which part of the web develpment you’re most comfortable with, Git can be of great assistance.

If you’ve ever talked to a ‘somewhat’ experienced programmer or you happened to be hanging out with a bunch of geeks, you’ve probably heard terms like ‘Git’, ‘Version Control’, ‘Branching’ and so on. These are all words which go hand in hand with numerous pages of different tutorials that can improve your knowledge, but I’ll avoid going into details and instead opt for a more general overview of the Git system, and some basic guidelines that will surely help you see clearly the initial stages of the said system and its usage. This will in turn make all those comprehensive online tutorials easier to understand.

What is Version Control?

This title probably doesn’t tell you much about what’s hidden behind it, right? You probably think it has something to do with version control of a specific web application. But it’s how it’s done that matters.

When you’re trying to learn something, and really understand it, the best thing to do is to explain a certain problem using a real life-scenario. I’ll do the same.

distributed version control systemPicture this for a second. You’re a web programmer and you just finished your web app version 1.0 which of course helped you gain more experience. You maintain contact with your client who after a while wishes for certain modifications to be implemented in the mentioned app, such as a new functionality, faster running app or they might no longer want certain functionality. This means code changes have to be made. When this is finished your client receives the latest app version 1.1. ready to be tested. It just so happens that after the second testing your client wants a particular functionality from the first version back, as well as additional features. What then? You have to somehow access the code from the first app version and make necessary modifications. In that case, Git is your ‘go-to guy’. I’ll explain how after another example.

The second example involves a back-end developer and a front-end developer who are both working on the same specification, and of course, at the same time (the whole process would be too slow if one of them was waiting for the other one to finish his/her part). This means that their code has to merge at a specific point in time. In that case, Git and its commands (which I’ll list later) are used.

These examples represent just some of the many situations of Git use but for the beginners stage they’re good enough.

Installing Git

There are numerous tools for Git use (depending on your operating system) such as:

  •  – Command Line (Windows) or Terminal (Linux & OS X distributions) and
  • – GUI (there are many different ones which are chosen and configured according to user’s requirements)

I prefer the Terminal which will, just like Command Line, enable you access to all commands, which is something GUI lacks. That’s why I suggest you try using this ‘black screen’ everybody shies away from initially.

As far as the installation goes, it’s the same thing, i.e. it all depends on your choice of OS. I’ll explain a simple installation for Linux Ubuntu OS.

The Terminal command is:

apt-get install git

And that’s it. You’ve installed Git.

Bitbucket repository

I’ll keep it short. If you happen to be working on an app you should consider having your own repository on, say, Bitbucket or GitHub (or other hosting services). These are systems for data package hosting (i.e. your source code directories). You need to have a Bitbucket account in order to make your own repository. There you can create projects you’re working on, and by using Git, the documents containing your source code will be pushed, as developers like to say, to your repository.

Also, you can initialize a Git project via the Terminal but I’ll explain this in more detail some other time.

If a project already exists in some repository you have access to, what you need to do is clone that project on your computer. By doing that, you clone an entire content of that project in your OS which allows you to modify anything you want within the code (at the local level) without impacting the code from your repository in any way.

The following command is used:

git clone git@bitbucket.org:username/project-name.git

After using this command, you’ll be able to find the entire project you’re working on in the directory of your choice in your OS.

Main commands

So here’s where you stand right now. Git is installed and your project is cloned from Bitbucket on your computer. The next step is making certain modifications to your code. After that, you’ll be wanting to see your document in your Bitbucket repository as well. You can use these commands in the following order:

In case modifications are made to a single document

git add document-name

In case modifications are made to multiple documents, and you want to avoid using the same command for all of them, use the following one

git add .

Before using this command your documents have to be in the ‘modified’ state. After this command the document or documents that was/were modified (and are in the‘staged’ state) are now ready to ‘commit’.
The next command is

git commit

The modified documents are now in the ‘committed’ state. This means these documents are now located in the local database (.git directory). The only thing left for you to do is to ‘push’ these documents to your online repository. This next command enables you to do just that:

git push

This command will place your modified documents into your repository. Once you’ve entered your repository, you can see the modifications made. Managing repositories is achieved through a system your repository is a part of, in this case, Bitbucket. I won’t elaborate more on this topic since anyone who can follow this simple tutorial can learn how to manage a repository on their own.

Another important command in the beginning stages regarding ‘branching’ is:

git checkout

which will help you switch from one branch to the next.
I’ll keep the promise I made at the beginning of this article, and explain what branching is.

Let’s say you’re working on a specification which will modify your existing code. Imagine you’re in the main branch right now where the main code is located. In case you make certain modifications, this will directly impact your app. When other developers in your team undertake a merge (as explained previously), conflicts may arise in the code due to possible modifications of the same place in the code. That’s why it’s important to make a copy of the entire code in another branch. Following that, and after implemented modifications and conducted testing, i.e. after you made sure that your code works, a merge of your code with the code from the main branch can be performed.

I hope I shed some light on what Git is and how it works.

If you enjoyed reading this article, ‘like’ us on Facebook.

Till next time…

 

Slobodan Barišić, ME in Communication and information technology

Images source: git-scm.com