# Git Tutorial for Beginners

Collaborative **development of software projects** not only happens within companies. Within the **open-source sector**, several hundred to thousands of volunteers (depending on project size) are involved in maintaining, optimizing, developing or modifying a program. These kinds of projects would be virtually unworkable without a suitable **system for recording and managing** the numerous changes submitted by different developers.

One of the most popular solutions to version management is the license-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 management program**.

## What is Git?

Git is a distributed version control system developed by Linux creator Linus Thorvalds in 2005 and released under the free GNU-GPLv2 license. What’s special about the tool is that, although a **central repository** exists for each project, all involved users download a **local working copy** of this directory onto their own working devices. Each of these copies represents a complete backup of the repository, rendering a continuous network connection unnecessary and enabling offline working. Moreover, the copies can be used to restore each project in the event the main repository fails or becomes damaged. Implemented **changes** can be **exchanged with all other project participants** at any time and – where relevant – included in the repository.

---

### Tip

One of the best-known alternatives to Git is the tool **Subversion** which is also an open-source program. It’s better known as **SVN** and, unlike Git, utilizes a **central management system**. Read this article ["Git vs. SVN – Comparing Version Management Tools“](https://www.ionos.com/digitalguide/websites/web-development/svn-vs-git-comparing-version-control-systems/ "SVN vs Git – comparing version control systems") to learn what commonalities these tools share and how they differ.

---

## How to install Git on your device

Anyone who wishes to learn software management with Git should first familiarize themselves 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 installing the respective version, you can control the tool platform-independently 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 management system via **Git-Bash** - the Unix-style shell contained in the installation. Alternatively, the software can be controlled via command line or the Windows terminal. But the parameter structure of the commands works a little differently there (for example, with double quotes instead of single quotation marks).

---

**Binary installation files, guides for packet manager installations (Unix systems) and turn-key portable editions** for the individual systems are available on the [official website of the Git project](https://git-scm.com/downloads "git-scm.com: Git Project Site"). Simply download the required packet or use the suitable packet in your packet management and then follow the installation wizard instructions. The installation step is unnecessary in case of a portable edition, of course.

---

### Tip

In the [download section of git-scm.com](https://git-scm.com/downloads/guis/ "Alternative Git-GUIs in git-scm.com Download Center"), the Git community provides a range of **alternative graphical interfaces** 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 management 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 application. 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 instructions**, allowing you to establish and manage your own repository with ease.

---

### Note

Experts at the German Association for Cybersecurity (Deutsche Gesellschaft für Cybersicherheit) noticed that a misconfiguration of Git or more accurately the repository’s version administration system meant that **.git directories were publicly accessible via browser**. This is possible whenever a Git repository is located in the web root of a server. It should be avoided at all costs. Users should thus **never save their Git directories inside the web root**. Alternatively, they should configure their web server in a way that makes the .git directory inaccessible to others. More information on Git vulnerabilities and security issues can be found on the [Perforce website](https://www.perforce.com/blog/vcs/how-secure-git "Perforce on “How secure is Git?”"). An older blog entry by Internetwache.org also provides [solutions how to fix the Git security issue](https://en.internetwache.org/dont-publicly-expose-git-or-how-we-downloaded-your-websites-sourcecode-an-analysis-of-alexas-1m-28-07-2015/ "Internetwache.org on security issues with the Git repository on web servers").

---

### Creating or cloning a Git repository

The Git repository is the central directory of a managed project and therefore the main hub for all participants. It’s where the complete version management is controlled. Your first step in Git will be to **create a central repository** – **or clone one** (in the form of a working copy), if you’re joining a collaborative development project already managed using Git.

If you want to configure version management or install the tools to learn how to work with Git, you’ll need to create a new repository. To do so, switch to the **desired local repository** on your device via “cd” (***c****hange* ***d****irectory*):

```none
cd individual directory path
```

Now execute the following command to **generate a *.git* repository**:

```none
git init
```

[![Image: Git-Bash: Changing directories and generating a repository](https://www.ionos.com/digitalguide/fileadmin/_processed_/8/1/csm_git-tutorial-1_8ddb81ffec.webp "Git-Bash: Changing directories and generating a repository")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-1.jpg) Git-Bash: Changing directories and generating a repository     If the Git repository for your project already exists, you’ll just need the **web or network address for this repository** in order to generate a **working copy** on your computer using the “**git clone**” command:

```none
git clone https://one-test.website/git-repository
```

---

### Note

Git supports various transmission protocols. As an alternative to the [HTTPS](https://www.ionos.com/digitalguide/hosting/technical-matters/what-is-https/ "What is HTTPS?") shown in the example, you can also use [SSH](https://www.ionos.com/digitalguide/server/tools/ssh-secure-shell/ "SSH: Secure Shell") to access a repository – provided you have the corresponding authorization.

---

### Checking repository status and adding new files for version control

The efficient organization of the working repository 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 **information about the adjustments made by other users**. You can check the current status of your working copy by running the following command:

```none
git status
```

In the case of a newly created repository or an absolute match between the central repository 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*”).

[![Image: Git-Bash: Checking the status of the Git repository](https://www.ionos.com/digitalguide/fileadmin/_processed_/d/b/csm_git-tutorial-2_7400b70e7d.webp "Git-Bash: Checking the status of the Git repository")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-2.jpg) Git-Bash: Checking the status of the Git repository       To **add a new file for version management** or **submit a revised file for the next commit**, apply the “**git add**” command to the file (it must be contained in the working repository). In our Git guide, we add a text document called “Test” as an example:

```none
git add Test.txt
```

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

[![Image: Git-Bash: Checking the status after changing the working copy](https://www.ionos.com/digitalguide/fileadmin/_processed_/e/d/csm_git-tutorial-3_e2f3c6923a.webp "Git-Bash: Checking the status after changing the working copy")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-3.jpg) Git-Bash: Checking the status after changing the working copy       ###  Confirming changes via commit and including them in the HEAD

All changes that you submit for version management (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 environment (also known as “branch”). The command for this step is shown below:

```none
git commit
```

---

### Note

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

---

After running the command, **Git automatically starts the editor** you entered as the standard selection during installation or the editor designated as default by the version management tool. You can now enter an **individual 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:

[![Image: Git Tutorial: Git-Bash output after the “git commit” command](https://www.ionos.com/digitalguide/fileadmin/_processed_/0/8/csm_git-tutorial-4_fd7dfd4d17.webp "Git Tutorial: Git-Bash output after the “git commit” command")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-4.jpg) Git-Bash output after the “git commit” command       As you can see in the screenshot, 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 repository is also the central repository), 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 information about the changes made.

### Revising or reversing generated commits

If you adopt changes in the form of a commit, you can subsequently revise or even remove the content at any time. A typical case in which adjustments 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 **retroactively** using “git add” and repeat the entry into the central repository. To do this, add the option **--amend** to the standard command:

```none
git commit --amend
```

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

```none
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:

```none
git reset --hard HEAD~1
```

### Viewing commit history

Learning how to manage projects with Git is worthwhile because of its basic versioning features. One of the big advantages of the open-source system is that you can view which **changes were last made to the repository**. The Git command for this is:

```none
git log
```

The “[git log](https://www.ionos.com/digitalguide/websites/web-development/git-log/ "Git Log")” command lists the generated commits in reverse chronological 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 **individual message** can also be seen, providing you and other users with key information to quickly classify each of the changes. In our Git guide, we previously generated a single commit with the message “Test”. This is displayed by the command like this:

[![Image: Git Tutorial: Commit history with one entry](https://www.ionos.com/digitalguide/fileadmin/_processed_/8/8/csm_git-tutorial-5_748b3e97fd.webp "Git Tutorial: Commit history with one entry")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-5.jpg) Git Tutorial: Commit history with one entry       The log command can also be modified by various parameters. Some useful options are shown in the table below:

<table>
  <thead>
    <tr>
      <th>Option for the “git log” command</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>-p</td>
      <td>shows the changes contained in a commit</td>
    </tr>
    <tr>
      <td>-2</td>
      <td>only lists the last two commits</td>
    </tr>
    <tr>
      <td>--stat</td>
      <td>adds a small statistic to each entry, showing which files were changed and how many lines were added or removed</td>
    </tr>
    <tr>
      <td>--pretty</td>
      <td>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</td>
    </tr>
    <tr>
      <td>--abbrev-commit</td>
      <td>only shows the first characters of a SHA-1 checksum</td>
    </tr>
    <tr>
      <td>--relative-date</td>
      <td>shows the data of a change in a relative format (e.g. “two weeks ago”)</td>
    </tr>
  </tbody>
</table>

### Including commits into the central repository

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

```none
git push origin master
```

After inputting this command, **Git automatically adopts all generated commits**, which previously existed only in the working copy, into the **central repository**. This repository 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 management systems, Git also offers a tagging feature that allows **selected points in the history of a repository 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 independent 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).
- ***“Lightweight”* 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 information.

You can create **annotated tags** in Git by using the “**git tag -a**” command on the respective commit. If you also add the “**-m**” parameter 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”:

```none
git tag -a Test -m "example tag"
```

---

### Note

If you add the “-m” parameter when creating the tag, Git will automatically open the editor so that you can enter the desired tagging message there.

---

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

```none
git tag Test
```

As soon as tags exist for your repository, you can also display them with the “**git tag**” command and the optional “**-l**” or “**--list**” parameters:

```none
git tag
git tag -l
git tag --list
```

[![Image: Git Tutorial: List of existing tags for the repository](https://www.ionos.com/digitalguide/fileadmin/_processed_/3/4/csm_git-tutorial-6_b5d46d48aa.webp "Git Tutorial: List of existing tags for the repository")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-6.jpg) The “git tag” command displays the previously created tag for the commit “Test”.     To **delete a tag from the local working repository**, apply the command chain “**git tag -d**” to it. We can delete our tag for “Test” as follows:

```none
git tag -d Test
```

Tags have to be manually adapted into the central repository. 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 parameter “**--tags**” which carries all generated tags over to the repository.

```none
git push origin --tags
```

### Creating, managing and deleting branches

The branches used in this Git tutorial are essentially the individual **working versions of the central repository**, which itself is classified as a branch – called “master”. With these branches, Git offers the perfect foundation for developing 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 instruction “**git branch**” which you then supplement with the desired name of the branch. To create an example branch called “**test\_branch**”, you’d enter the command below:

```none
git branch test_branch
```

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

```none
git checkout test_branch
```

[![Image: Git Tutorial: Switching branches via Git-Bash](https://www.ionos.com/digitalguide/fileadmin/_processed_/f/0/csm_git-tutorial-7_6aebff405d.webp "Git Tutorial: Switching branches via Git-Bash")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-7.jpg) After successfully changing branch, Git displays the message: “Switched to branch”.     If you want to merge branches, you can use the “**git merge**” command. First, enter the “**checkout**” command to switch to the repository 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 repository** as follows:

```none
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:

```none
git branch -d test_branch
```

The only **requirement for the deletion process** is that you need to be in a different branch. We, therefore, switched to the central repository before executing the command – as you can see in this screenshot:

[![Image: Switching and removing branches via Git-Bash](https://www.ionos.com/digitalguide/fileadmin/_processed_/3/6/csm_git-tutorial-8_a15d6804b1.webp "Switching and removing branches via Git-Bash")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/git-tutorial-8.jpg) Git confirms the deletion of the branch with the message: “Deleted branch ...”       ---

### Tip

In our Digital Guide, you will also find information on "[Git Branch: How to rename a local and remote branch](https://www.ionos.com/digitalguide/websites/web-development/renaming-a-git-branch/ "Renaming a Git Branch")".

---


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/git-tutorial/](https://www.ionos.com/digitalguide/websites/web-development/git-tutorial/) for AI/LLM consumption.