Git

From Etp
Jump to: navigation, search

General Remarks about git

Git is a distributed version control (vcs) system. A version control system is used to store different versions of files (in our case code) and allows to recreate the process of how the code reached its current state and enables a larger group of people to simultaneously work on the same code. In a distributed vcs, every user has a complete replica and does not depend on a central server.


Some easy steps to your first git repsoitory

 git config --global user.name "Your Name"
 git config --global user.email "Yourmail@physik.uni-muenchen.de"


Setting up a repository with git is extremly easy:

 git init

This will create a git repository in your current directory.

 git add .

This will add all files in the directory and including subdirectory to the repository

 git commit -m "Initial Commit"

With git commit you create a new commit (or "version") of the code for the fist time.

The staging area

One of the important features of git is the so called staging area. In git there are three logical layers:

  • Your local filesystem
  • The staging area
  • The repository

The staging area is a place where you can collect changes to the code before actually submitting them in a commit. The repository is the long-term state of your code, where you can browse all the commits you have created. For a very basic usage of git the staging area is not needed, but it will become very useful later on.

Very basic git Usage

The easiest use-case for git is simply to record all the changes you made to your code to enable you to backtrace changes you made. This might be very useful if your code doesn't work anymore, and you want to check what changes you have made. In order to do this, you have to commit your changes frequently. You can vary the frequency to fit your needs, bit more commits also mean more control over your code, so it is in principle a good idea to commit as often as possible (probably every time you save your code, run your script or try to compile. Committing every 15 minute is not unusual.

Committing changes

To commit something, you have to add the changes you made to the staging area, and then commit from the staging area to the repository.

 git commit -a -m "Message describing your code changes"

Will do both steps in one command. -a simply adds all the changes you made to the staging area, and then commits it to the repository.

Adding new files

Git only tracks the files in the repository. If you create a new file, you have to add it to the repository in order to track it.

 git add newFile.C
 git add *.C

Useful Git Commands

 git status

Gives you some information about the state of your repository, e.g. if there are untracked files, or which files have been modified recently

 git log

Shows you a list of all the commits that have been made (including the commit-id)

 git diff oldCommit newCommit

Shows you the diff between two commits

Git aliases

Git allows you to define aliases, here some examples, add a selection of this to ~/.gitconfig

[alias]
       lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)' --abbrev-commit
       lga = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)' --abbrev-commit --all
       st = status
       co = checkout
       cm = commit
       cmm = commit -m
       rb = rebase
       fa = fetch -a

GUI

There are two nice GUIs for git: gitk and gitg. gitg looks better, gitk has more features to interact with the code. In this tutorial gitk will be used. To start it, go to the directory where your repository is and

 gitk


Links

Good tutorial: http://git-scm.com/book