Skip to main content

git-cheat-sheet

Get Help / See The Documentation

git command --help

Get help (show the manpage) for the given command, e.g. git add --help.

Global Configuration

git config --global user.name 'Your Name' git config --global user.email 'your.name@smail.th-koeln.de'

Do this once so that your snapshots (in git speak: commits) have correct autor data on them.

Creating A Repository

git init [directory]

Creates a new repository in the given directory. If you don't specify directory ,the current directory is used. If directory does not exist, it will be created.

git clone repository

Creates a local copy of the given repository. Most of the time, repository will be a ssh connection specification to the CoCo gitlab server. They look like this: git@git.coco.study:students/foo/bar/exercises.git

A new directory is created for the local copy, named like the basename of the repository (without .git extension). In the example above, the directory would be named exercises.

Recording History: Making Commits

git status

Give an overview of modified files and files git does not know yet. It also lists the files that are currently selected for making a snapshot. (In git speak: it lists the staged files, i.e. those added to the index.)

git diff [file] [...]

See the differences of the given files to the last commit. If you don't give any files, all changes will be shown.

git diff --staged

See the differences that are currently selected for making a snapshot (staged for a commit).

git add [file] [...]

Selects changed files to make a commit of. (In git speak: stage the files by adding them to the index.) If the file was previously unknown to git, it will be known now.

Beware: if you select a file for committing using git add and then change it some more, you have to git add those later changes again!

git commit [-m 'Description of your change.']

Make a new commit with all the files selected (by using git add).

git restore file [...]

Undo all changes to the given files. Afterwards the files will be like they were in the last commit.

This cannot be undone!

git restore --staged [file] [...]

Deselect the given files. Afterwards they are no longer staged for the next commit.

This can be undone by git add.

Working Together: Remote Repositories

To collaborate on a project, the most common way is to have one central repository (on gitlab in our case) and each team member has a copy of that repository (see git clone). Team member exchange their code changes by synchronising with the central repository.

git push

Upload your commits to gitlab. This only works if your repository is connected to gitlab, i.e. if you cloned from there or you used git remote add.

If gitlab refuses your commits, most likely someone else has pushed commits before you. In that case, use git pull.

git pull

This does two things:

  1. Download changes made by others. (See also git fetch.)
  2. Join the downloaded changes with yours.

There are multiple strategies for the second step: merge and rebase, in git speak, where merge is the default. Both can result in conflicts when you and someone else has edited the same file.

git fetch

Just downloads changes made by others.

git remote add origin remote-repository

Connect your local repository to a remote one. You only need this, if you created the local repository first and now want to use gitlab.

How To Resolve Conflicts