Col­lab­or­at­ive de­vel­op­ment of software projects not only happens within companies. Within the open-source sector, several hundred to thousands of vo­lun­teers (depending on project size) are involved in main­tain­ing, op­tim­ising, de­vel­op­ing or modifying a program. These kinds of projects would be virtually un­work­able without a suitable system for recording and managing the numerous changes submitted by different de­velopers.

One of the most popular solutions to version man­age­ment is the licence-free tool Git. It’s easy to learn and free of charge. In this tutorial, we explain the most important Git basics you need to get started with the version man­age­ment program.

Cheap domain names – buy yours now
  • Free website pro­tec­tion with SSL Wildcard included
  • Free private re­gis­tra­tion for greater privacy
  • Free Domain Connect for easy DNS setup

What is Git?

Git is a dis­trib­uted version control system developed by Linux creator Linus Thorvalds in 2005 and released under the free GNU-GPLv2 licence. What’s special about the tool is that, although a central re­pos­it­ory exists for each project, all involved users download a local working copy of this directory onto their own working devices. Each of these copies rep­res­ents a complete backup of the re­pos­it­ory, rendering a con­tinu­ous network con­nec­tion un­ne­ces­sary and enabling offline working. Moreover, the copies can be used to restore each project in the event the main re­pos­it­ory fails or becomes damaged. Im­ple­men­ted changes can be exchanged with all other project par­ti­cipants at any time and – where relevant – included in the re­pos­it­ory.

Tip

One of the best-known al­tern­at­ives to Git is the tool Sub­ver­sion which is also an open-source program. It’s better known as SVN and, unlike Git, utilises a central man­age­ment system. Read this article "Git vs. SVN – Comparing Version Man­age­ment Tools“ to learn what com­mon­al­it­ies these tools share and how they differ.

How to install Git on your device

Anyone who wishes to learn software man­age­ment with Git should first fa­mil­i­ar­ise them­selves with the software and its user interface. Git is available for Windows, Unix/Linux and macOS, however, the various versions differ slightly in terms of their use. After in­stalling the re­spect­ive version, you can control the tool platform-in­de­pend­ently using the command line or a graphical user interface.

Note

In order to use the commands shown in this Git tutorial, Windows users should run the version man­age­ment system via Git-Bash - the Unix-style shell contained in the in­stall­a­tion. Al­tern­at­ively, the software can be con­trolled via command line or the Windows terminal. But the parametre structure of the commands works a little dif­fer­ently there (for example, with double quotes instead of single quotation marks).

Binary in­stall­a­tion files, guides for packet manager in­stall­a­tions (Unix systems) and turn-key portable editions for the in­di­vidu­al systems are available on the official website of the Git project. Simply download the required packet or use the suitable packet in your packet man­age­ment and then follow the in­stall­a­tion wizard in­struc­tions. The in­stall­a­tion step is un­ne­ces­sary in case of a portable edition, of course.

Tip

In the download section of git-scm.com, the Git community provides a range of al­tern­at­ive graphical in­ter­faces for the version manager. Among other things, you’ll also find Git clients for Android and iOS here, which enable you to use the open-source tool on your mobile device.

Git Tutorial: steps for learning how to use Git

Once Git is installed on your system, you can use the version man­age­ment system to manage your projects. As with any software, you’ll first need to learn the basic functions and commands to get the most out of the ap­plic­a­tion. As part of our complete Git tutorial for beginners, we’ll explain the most important steps for setting up Git and the necessary command line in­struc­tions, allowing you to establish and manage your own re­pos­it­ory with ease.

Note

Experts at the German As­so­ci­ation for Cy­ber­se­cur­ity (Deutsche Gesell­schaft für Cy­ber­sich­er­heit) noticed that a mis­con­fig­ur­a­tion of Git or more ac­cur­ately the re­pos­it­ory’s version ad­min­is­tra­tion system meant that .git dir­ect­or­ies were publicly ac­cess­ible via browser. This is possible whenever a Git re­pos­it­ory is located in the web root of a server. It should be avoided at all costs. Users should thus never save their Git dir­ect­or­ies inside the web root. Al­tern­at­ively, they should configure their web server in a way that makes the .git directory in­ac­cess­ible to others. More in­form­a­tion on Git vul­ner­ab­il­it­ies and security issues can be found on the Perforce website. An older blog entry by In­ter­net­wache.org also provides solutions how to fix the Git security issue.

Creating or cloning a Git re­pos­it­ory

The Git re­pos­it­ory is the central directory of a managed project and therefore the main hub for all par­ti­cipants. It’s where the complete version man­age­ment is con­trolled. Your first step in Git will be to create a central re­pos­it­oryor clone one (in the form of a working copy), if you’re joining a col­lab­or­at­ive de­vel­op­ment project already managed using Git.

If you want to configure version man­age­ment or install the tools to learn how to work with Git, you’ll need to create a new re­pos­it­ory. To do so, switch to the desired local re­pos­it­ory on your device via “cd” (change directory):

cd individual directory path
Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

Now execute the following command to generate a .git re­pos­it­ory:

git init

If the Git re­pos­it­ory for your project already exists, you’ll just need the web or network address for this re­pos­it­ory in order to generate a working copy on your computer using the “git clone” command:

git clone https://one-test.website/git-repository
Note

Git supports various trans­mis­sion protocols. As an al­tern­at­ive to the HTTPS shown in the example, you can also use SSH to access a re­pos­it­ory – provided you have the cor­res­pond­ing au­thor­isa­tion.

Checking re­pos­it­ory status and adding new files for version control

The efficient or­gan­isa­tion of the working re­pos­it­ory is among the most important pillars of Git. It allows you to propose your own changes and additions to a project that can be adopted as “commits”, and obtain in­form­a­tion about the ad­just­ments made by other users. You can check the current status of your working copy by running the following command:

git status

In the case of a newly created re­pos­it­ory or an absolute match between the central re­pos­it­ory and the working copy, you’ll receive a message that there are no new changes to the project (“No commits yet”). In addition, Git will say that you haven’t currently submitted any changes for the next commit (“nothing to commit”).

To add a new file for version man­age­ment or submit a revised file for the next commit, apply the “git add” command to the file (it must be contained in the working re­pos­it­ory). In our Git guide, we add a text document called “Test” as an example:

git add Test.txt

If you check the re­pos­it­ory status again, the example document will be shown as a potential candidate for the next official update to the project (“Changes to be committed”):

Con­firm­ing changes via commit and including them in the HEAD

All changes that you submit for version man­age­ment (as described in the previous section) always need to be confirmed via Commit in order to be included in the HEAD. The HEAD is a type of index that refers to the last effective commit in your current Git working en­vir­on­ment (also known as “branch”). The command for this step is shown below:

git commit
Note

Always check before entering the command whether all changes intended for the commit are marked ac­cord­ingly (using “git add”). Otherwise, they’ll be ignored – even if they’re found in the re­pos­it­ory of the working copy.

After running the command, Git auto­mat­ic­ally starts the editor you entered as the standard selection during in­stall­a­tion or the editor des­ig­nated as default by the version man­age­ment tool. You can now enter an in­di­vidu­al comment for the planned commit; the rows included and separated by a semicolon are not shown later. Once you close the editor, Git will create the commit:

As you can see in the screen­shot, after running “git commit” you’ll receive a summary regarding the commit. In the preceding square brackets, you’ll find the name of the branch (“master” in this case, since our working re­pos­it­ory is also the central re­pos­it­ory), in which the changes have been adopted, as well as the SHA-1 checksum of the commit (“c0fdc90” here). This is followed by the freely chosen comment (“test” in our example) and specific in­form­a­tion about the changes made.

Revising or reversing generated commits

If you adopt changes in the form of a commit, you can sub­sequently revise or even remove the content at any time. A typical case in which ad­just­ments are necessary is, for example, if you have generated the commit too soon and forgotten important files or changes. In this instance, you’ll need to provide the new or adjusted files ret­ro­act­ively using “git add” and repeat the entry into the central re­pos­it­ory. To do this, add the option --amend to the standard command:

git commit --amend

However, if you want to withdraw the most recent commit, you can use the following Git command:

git reset --soft HEAD~1

This command reverses the commit last included in the HEAD. The files it contains are then reset back to the status: “Planned changes for the next commit”. To delete the files entirely, enter the command below instead:

git reset --hard HEAD~1

Viewing commit history

Learning how to manage projects with Git is worth­while because of its basic ver­sion­ing features. One of the big ad­vant­ages of the open-source system is that you can view which changes were last made to the re­pos­it­ory. The Git command for this is:

git log

The “git log” command lists the generated commits in reverse chro­no­lo­gic­al order, whereby the SHA-1 checksum, the author (name and email address) as well as the date of the commit are shown as standard. What’s more, the in­di­vidu­al message can also be seen, providing you and other users with key in­form­a­tion to quickly classify each of the changes. In our Git guide, we pre­vi­ously generated a single commit with the message “Test”. This is displayed by the command like this:

The log command can also be modified by various para­metres. Some useful options are shown in the table below:

Option for the “git log” command De­scrip­tion
-p shows the changes contained in a commit
-2 only lists the last two commits
--stat adds a small statistic to each entry, showing which files were changed and how many lines were added or removed
--pretty changes the format of the output, with various formats available; --pretty=online is one possible format, for example, which lists all commits in a single line
--abbrev-commit only shows the first char­ac­ters of a SHA-1 checksum
--relative-date shows the data of a change in a relative format (e.g. “two weeks ago”)

Including commits into the central re­pos­it­ory

So far, we’ve shown you how to save changes as a commit in the HEAD of the local re­pos­it­ory. For the commit to be included in the central re­pos­it­ory, however, the following command needs to be entered:

git push origin master

After inputting this command, Git auto­mat­ic­ally adopts all generated commits, which pre­vi­ously existed only in the working copy, into the central re­pos­it­ory. This re­pos­it­ory is also referred to as the “master”. If you replace this name in the listed code with the name of another branch, the files will be sent there instead.

Tagging: creating, deleting and listing tags in Git

Like many other version man­age­ment systems, Git also offers a tagging feature that allows selected points in the history of a re­pos­it­ory to be marked as important. These tags are typically used to label the releases of a software program (like version 1.0, 2.0, etc.) so that they remain easy to access even for larger projects. Git supports two types of tags:

  • “Annotated” tags are saved as in­de­pend­ent objects in the database, including their own checksum, tagging message, date, name, and email address of the tag author as well as an optional GNU Privacy Guard signature (GPG signature).
  • “Light­weight” tags act like branches, only serving as a reference to a commit. This type is suitable when you only need temporary tags or don’t want to save the extended in­form­a­tion.

You can create annotated tags in Git by using the “git tag -a” command on the re­spect­ive commit. If you also add the “-m” parametre as a suffix, you can compose the desired tagging message directly in the command line (between straight quotation marks). In this Git guide, we generated the “Test” commit which we can also link with a tag including the message “example tag”:

git tag -a Test -m "example tag"
Note

If you add the “-m” parametre when creating the tag, Git will auto­mat­ic­ally open the editor so that you can enter the desired tagging message there.

The approach for light­weight tags is similar. However, you only need to use the basic command “git tag” on the relevant commit – without any further para­metres. This command would appear as follows for our Git tutorial example:

git tag Test

As soon as tags exist for your re­pos­it­ory, you can also display them with the “git tag” command and the optional “-l” or “--list” para­metres:

git tag
git tag -l
git tag --list

To delete a tag from the local working re­pos­it­ory, apply the command chain “git tag -d” to it. We can delete our tag for “Test” as follows:

git tag -d Test

Tags have to be manually adapted into the central re­pos­it­ory. To do so, both the tag name and the command “git push origin” are required. Instead of the tag name, you can also add the parametre “--tags” which carries all generated tags over to the re­pos­it­ory.

git push origin --tags

Creating, managing and deleting branches

The branches used in this Git tutorial are es­sen­tially the in­di­vidu­al working versions of the central re­pos­it­ory, which itself is clas­si­fied as a branch – called “master”. With these branches, Git offers the perfect found­a­tion for de­vel­op­ing features and functions in isolation and combining them at a later date. This process is also known as “merging”.

It’s quite easy to create a new branch: You only need the in­struc­tion “git branch” which you then sup­ple­ment with the desired name of the branch. To create an example branch called “test_branch”, you’d enter the command below:

git branch test_branch

After that, you can switch to this branch at any time using “git checkout”:

git checkout test_branch

If you want to merge branches, you can use the “git merge” command. First, enter the “checkout” command to switch to the re­pos­it­ory that is to integrate the other branch and then execute the merge command including the name of the branch to be merged. For example, our working version “test_branch” can be merged into the central re­pos­it­ory as follows:

git checkout master
git merge test_branch

Once you’ve merged the branches and therefore no longer need a certain branch, you can simply delete it. To this end, add the command “git brand -d” to the branch no longer needed. Our Git tutorial example “test_branch” can be removed by running the command below:

git branch -d test_branch

The only re­quire­ment for the deletion process is that you need to be in a different branch. We, therefore, switched to the central re­pos­it­ory before executing the command – as you can see in this screen­shot:

Tip

In our Digital Guide, you will also find in­form­a­tion on "Git Branch: How to rename a local and remote branch".

Free Cloud Server Trial
En­ter­prise-grade virtual private servers
  • KVM based dev servers for de­velopers
  • Scalable to en­ter­prise cloud level
  • Pay-as-you-go, per-minute billing
Go to Main Menu