Open menu with table of contents Git Introduction
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Android Development

Git Introduction

Stuttgart Media University

1 Basic About Git

  • Git was initially created during the development of the Linux kernel by Linus Torvalds and others in April 2005.
  • It's a free and open source distributed version control system. That's the official website.
  • A version control system allows to keep historical versions tracked and improves the collaboration in the team. It can be used as single source of truth when it comes to code which also allows to automate building applications with eg continous integration (CI). The place where it's stored is called a repository.
  • The concept is working locally on a separate branch and then push it to make it public to team. If it's tested and working, your changes can be merged to the main or master branch. The main branch should always work!

2 Basic Commands

Please check this Git cheat sheet for a quick overview.

2.1 Initialize a repository

To initialize a new project, initialize a git repository.

git init

The command above is not needed in our case because you will already get it set up with the right directory, so please do:

git clone git@gitlab.mi.hdm-stuttgart.de:mad1/mad1.pages.mi.hdm-stuttgart.de.git

Hint: Replace the location of the remote repository to the one which you have access to. If you use SSH make sure your keys are set up correctly.

2.2 Working locally

After initializing you are automatically on the main/master branch. Now we are going to start working in the repository by creating a new file in there called "INTRODUCTION.md".

You can check the status of the files in a directory to display if there are untracked or modified ones:

git status

Currently, git does not track this file so tell git that the file exists call:

git add INTRODUCTION.md

And then you can do a commit so git keeps track on that:

git commit -m "Added a INTRODUCTION.md"

2.3 Working with a remote repository

While working on a team, we have a remote repository. In our case this is hosted on the HdM Gitlab.

So now that we have our changes locally we want to push them to the remote repository:

git push origin master

It could also be that team members made changes, and you want to check them:

git fetch origin

The above will only fetch information about the changes, to get the changed code call:

git pull

2.4 Branching and merging

Now that you are in a team, each team member works on a different feature of the application in their own space but with the main/master branch as their base:

git branch feature1

You can check out existing branches by:

git branch

If you want to switch a branch you can call:

git checkout feature1

Let's assume you did your work on feature1 branch, you added files and commited them, then you want to merge that branch to main/master because you want to make a production release. So first you want to switch to master branch:

git checkout master

And then merge the feature1 branch:

git merge feature1

This merge is only locally so if you want the changes in remote you have to push again or directly merge to master: git merge origin/master. If you are not using a branch anymore you can delete it.

3 Git Clients

If you have issues using git in command line you can of course use any git clients. There are multiple ones on the market, so here are popular ones:

Questions?