The world of software development requires efficient handling of code and changes. This is where Git comes into play. In this guide, you will learn the basics of Git and how it works. It's time to engage with one of the most important tools for developers.

Key insights

  • Git is a distributed version control system.
  • Changes are made locally and can be uploaded to a central repository (e.g., GitHub).
  • Git allows for easy traceability and versioning of files.

Step-by-step guide

1. Basic understanding of version control

Before you start working with Git, it's important to understand why version control is necessary. In the past, version control was done manually, often by creating copies of a software project. This was not only time-consuming but also prone to errors.

Git – Basics of Version Control

With Git, everything becomes much simpler. It is a centralized repository where all changes to files are tracked. When you save a new state of your project, it is logged in a database.

Concepts like commit, branching, and merging play a central role here.

Git - Basics of Version Control

2. Installing Git

On most operating systems, you can easily download and install Git. Visit the official Git website and download the appropriate version for your operating system. The installation process will guide you step by step through the setup.

3. Creating a repository

After installation, open your terminal or command prompt. To start a new project, navigate to the appropriate folder and run the following command:

git init

This creates a new, empty Git repository in the current directory.

4. Getting started with files

Now add some files that you want to version. After you have added or modified data, you can check the status:

git status

This gives you an overview of the changes in your repository.

5. Adding files to the staging area

To save changes, they need to be moved to the staging area. Add files to the staging area using:

git add <filename>

This instructs Git to save these specific changes.

6. Commit - saving changes

The next step is the commit. This saves the current state of your repository. Use the following command:

git commit -m "Message"

Be sure to write a concise message so it's clear what changes you've made.

7. Checking the commit history

To view the history of your commits, use the command:

git log

Here you will see a list of all changes made in this repository.

8. Working with branches

Branches are an important feature of Git for enabling parallel development. To create a new branch and switch to it, use:

git checkout -b <branch-name>

Uploads can then be made in separate branches without affecting the main branch.

9. Merging branches

If you are happy with a branch, you can merge it into the main branch (usually "main" or "master"). Switch back to the main branch:

git checkout main

And then execute the merge command:

git merge <branch-name>

10. Using remote repositories with GitHub

To share or back up your local work, you can create an online repository on GitHub. Sign in to GitHub, create a new repository, and follow the instructions. To connect your local repository to GitHub, use:

git remote add origin <repository-URL>

After that, you can upload your commits:

git push -u origin main

By following these steps, you have successfully connected your project to a remote repository. Now you can save your changes online at any time.

Summary - Git and version control: The basics

Git is an essential tool for anyone looking to seriously engage in software development. With its help, you can efficiently manage changes, work in parallel, and ensure the integrity of your code.

Frequently Asked Questions

What is Git?Git is a distributed version control system that monitors and stores changes to files.

Why is version control important?Version control allows you to track changes to files and facilitates collaboration among multiple developers.

How do I work with branches in Git?Branches allow parallel development. Create a new branch, work on it, and then merge it back into the main branch later.

What are commits?A commit saves the current state of your files in the repository along with an associated message.

What is the difference between a local and a remote repository?A local repository exists on your machine, while a remote repository, such as on GitHub, is available online.