Back to Guides
Last modified: Tue April 11 2017 - 18:55:03 (CEST)

Tutorial: Git
provided by Dr. Hella Rabus

Git is a version control system (VCS) for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for software development, but it can be used to keep track of changes in any files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows. If you want to learn more about git, see the official git documentation or get a copy of ProGit . Information on how a write an appropriate commit message, can be found in this style guide .

How to work with a repository?

To actually copy a repository, i.e., to get a local copy you can work with, you first need to clone the repositoty. Therefore navigate to the folder you want to clone your repository to in your terminal and type

git clone $HOST:$PROJEKTNAME

where $PROJEKTNAME is the name of the repository. The clone command creates a directory called $PROJEKTNAME and copies the projects history into that folder. To check the projects history, enter the $PROJEKTNAME directory and type either one of the following commands

gitk
git log

The gitk command opens a GUI, whereas the git log command prompts you to the log output in LESS. If you want to close LESS and return to your normal terminal, just type the letter q. If you want to get updates from the server, e.g. if somebody hast changed something and you want to get those changes, just type

git pull

If you edited some of the files in the repository, created new files or deleted old ones, you can prepare to commit your changes and push them to the server, so that others may work with them. First you need to add all the files, whose changes you want to give to the server. This is done by either of the following commands

git add $FILENAME
git add -A
git add -u

The -A option adds all files in the repository, which means all the files, that are already tracked, as well as any new file. The -u option updates the status of all tracked files, i.e., adds the changes of all files that are already tracked. The command

git status

prints the status of your current stages to the terminal. Staged files should be displayed in green and unstaged but changed files should be displayed in red. If you added all the files you want, you can commit your changes by writing

git commit

This prompts you to a vim window in your terminal. It is very important that you write a commit message, so that others can understand what you changed and why. To write a commit message in vim, just type i to enter the insert mode and start writing your message. If you are done, just type ESC followed by :wq. This should bring you back to your terminal. As an alternative, if your commit message is very short (less than 50 characters), you can type

git commit -m "short commit message here"

Before you push your changes to the server, it is a good idea to use git pull to merge possible changes done by other users since your last pull into your current work. If any merge conflicts arise, you need to solve them manually. If there are any problems the the merge, please feel free to contact an admin and get some help. If everything is merged or no merge conflict is displayed, just type

git push

to push your changes to the server.

How to work with branches?

The following command lines create a new branch called $BRANCHNAME, switches to that branch and add that branch to the server

git branch $BRANCHNAME
git checkout $BRANCHNAME
git push -u origin $BRANCHNAME

The checkout command can be used to switch through branches. If you want to merge two branches, switch to one of them and type

git merge $BRANCHNAME

where $BRANCHNAME is the name of the other branch. This merges $BRANCHNAME into your current branch.

How to use git to track my own work?

If you want to track other project with git, you can create your own local git repository by changing to the folder you want to be tracked and just type

git init

and you can track all your work. The only difference now is, that do not a server to push your history to. This means, that the git push and git pull commands will not work and you just need to commit your changes.