Git version-control system practically
Why version-control system and what’s GIT
Certainly have you ever worked on a computer on a larger project, such as program source code, diploma work, but even a presentation. Project preparation took a long time and maybe more people cooperated at the same time. Then you have come across a problem where and how to maintain and back up the project. No one wants to start from the beginning and back up regularly the whole project to the folders in the style of Diploma1, Diploma2, Diploma-final, Diploma-final-final is very amateur, confusing and inefficient.
A man named Linus Torvalds, who is also the father of the Linux operating system, had a similar problem. To solve the problem of managing his code, Mr. Torvalds developed the GIT versioning system. Although it was not the first similar system that existed, it has become one of the most popular thanks to several principles on which it was founded. Especially because it is distributed (there is no central repository, everything is quickly accessible locally and no internet connection is needed) and does not work with whole files but only with changes in them (unlike SVN). More about the implementation details of the GIT can be found on the wiki or on the official website. And if someone is not yet convinced that GIT is the right code management tool, here are some references to companies that use it to develop software: Google, Microsoft, Facebook, Android, Twitter ...
Since the GIT is a Linux utility, it is controlled via the command line. However, in order to make the ordinary user more comfortable, several graphic programs have been created through which the versioning system can be used. For example, SourceTree from Atlassian is very popular. Especially because it is clear and simple. In addition, it is available for both Windows and Mac OS (Unix users prefer to use the command line anyway :-)).
To install SourceTry, simply download the file from the official site and install it as usual. During installation, you only need to fill in your name and email to be used as the signature. And for native Linux users, a specific command will always be added to show how to execute the functionality in the command line.
We can use the following commands to create a GIT repository:
- Debian, Ubuntu: apt-get install git-core
- Fedora Core, OpenSUSE: yum install git
- Gentoo: emerge dev-util/git
Creation of a repository
The first step is to create a new repository, which is a place to commit our data. As an example, we will create a simple HTML page that we will edit, expand, and maintain in our repository. To create a repository in SourceTree, click Clone/new in the upper left corner, select the Create new repository tab in the pop-up window, and specify the project name and path to the folder where it will be located.
The command to create a repository is git init.
Now when we have a repository created, we can start creating content in the same folder, and then commit it to the repository. So add the index.html test file to the root folder. SourceTree instantly recognizes all changes to the workspace of our project and displays them in the Working Copy Changes bottom box. In the right window, it shows specific changes for the currently selected file.
To save these changes to our repository, click on the Commit button in the top bar and a pop-up window will appear as shown on the picture below. Here you have to enter the name of the commit, which should describe what we are changing in the project. In our case it is "Index created". Next, select the changes we want to add to the repository. This can be done either by drag&drop the desired files from the bottom window to the top Staged changes, or by selecting the individual files if we change Commit mode to Selected files above the windows. In the right window we see all the changes that will be committed to our repository. Press Commit to confirm.
Commit through command line consists of two steps. First, you need to mark the changes to be added to the repository by using the git add
Now if you make additional changes to the index.html file, which is already part of the repository, SourceTree, or GIT, will only offer these changes to save. On the picture below you can see that the header has been added to the file. The large window that lists all commits as they are, is called a tree and is used to make the repository more transparent and perform some operations. At the same time, we can notice the master keyword for the last commit here. This indicates the name of the branch we are in. At the moment, we have only one branch in the repository, but the next article will expand the number and show how it works because it is one of the key features of versioning systems.
You can use the git status command to view the current repository status and git log to list the commit history.
Revert is another of the basic operations supported by GIT. We will use it when we want to cancel changes made to our project and return to one of the previously committed versions. This can also be done for individual files. As we can see on the picture below, in the work directory of our project, the header was modified in index.html file. If you want to return to the version with the original header, just right click on the modified file and select Discard (for unknown reasons, the revert function in SourceTree is called discard) and the index.html file is restored to the original version in the working folder. This example is not such a striking change, so we can make the head adjustment as fast as possible by hand. However, if there are extensive changes in multiple locations, this feature is priceless. And among other things, it can also restore accidentally deleted files!
From the command line, you can revert the files to their original state using the git checkout -