Git for beginners

In this session we provide a introduction into the version control system git. Git is installed on Kapteyn machines by default.

The slides are available here.

Initial commit

Creating a repository

Directly from the remote repository:

$ git clone [<url>]

Locally and add then a remote repository:

$ git init
$ git add remote [<name of remote>] [<url of remote>]

Adding files/directories to the staging area

$ git add file

With git status we can check which files are already in the staging area and which are still untracked.

Commit

$ git commit -m "[<description>]"

Branches

Initially, after creating the git repository, we are by default on the branch master. We can create another branch by

$ git branch [<name of branch>]

This command just creates the branch but we have not switched to it. This is done by

$ git checkout [<name of branch>]

After some commits to this new branch, we may want to merge this new branch with the existing master branch. First we need to switch again to the master branch.

$ git checkout master

Then we can do the merge:

$ git merge [<name of branch>]

If git does not find any merge conflict, the merge is done automatically. However, if git reports a merge conflict, we need to resolve this conflict by checking the files where the conflict has been reported for. After editing the corresponding files, these changes need to be commited, i.e. executing git add -u `` and ``git commit.

Synchronization with remote repository

We can push the changes we have made in our local repository to a remote repository by

$ git push [<name of remote>]

By default the name of the first remote repository is origin.

If our local repository is behind the remote repository, we can update our local repository by

$ git pull

or alternatively (when there is a merge conflict)

$ git fetch
$ git merge

git pull is basically git fetch and git merge in one command. git fetch just fetches the updates from the remote repository but does not merge it into the current working branch. This is done by git merge.

Screencast