Home Blog Newsletter

Git introducton

This giude was adpoted from a workshop held at Science Hack day 2017 link

What is git?

Git is code versioning standard. There are many git hosting services available: GitHub, GitLab, BitBucket, Visual Studio Team Services,…

With git you can have your code safely on a remote server, ready to be accessed by whomever you grant access. With the proper use of git all previous versions of your documents are saved and available.

On Linux, chances are that git command line interface is already installed. On Windows and Mac computers I recommend you download git tools. You can open a bash terminal with right click in your preffered folder and clicking Git Bash Here.

Create project

Create new project. It is recommended that you include a README.md file in your repository (like this one) that gives a description of your project.

You can write the README file using the Markdown descriptive language.

Basic git commands

First let us clarify the difference between local and remote repository. Remote repository is the code that is stored on remote server like GitLab server at ETH in our case. Local repository is the repository containing code on your personal laptop or PC. When first creating a local copy of a remote git repository you can use the git clone command. The actual url will depend on your username and repository, but for this repository this command would look like this:

git clone git@bitbucket.org:ZigaBrencic/new_developer.git

Once you have a local copy you will never have to call git clone for this repository again.

You have at your disposal the following basic commands:

git basics

Creating new project

Now let’s go back to our computer and creat a new project:

mkdir git_intro
git init
git remote add origin https://gitlab.ethz.ch/ziga.brencic/git_intro.git
git add .
git commit -m "Initial commit"
git push -u origin master

What a typical workflow might look like

$ git status  # Make sure I have no local changes
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

$ git pull  # Get new changes from remote server
Already up to date.
$ touch new_file.txt  # Create a new file
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)


nothing added to commit but untracked files present (use "git add" to track)
$ git add new_file.txt  # Add new file to staging
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   new_file.txt
$ git commit -m "Create new file"  # Commit the changes
[master 6b1ff71] Create new file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_file.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
$ git push  # Push the new file into the remote repository
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://gitlab.ethz.ch/ziga.brencic/git_intro.git
   817f344..6b1ff71  master -> master


Sometimes you want to create a new branch of commits. Maybe to create a new feature or to fix a bug without disturbing the master branch, which is the default branch name.

git branch

You can create a new branch with the command

git branch <branch-name>

You can switch between branches with the command

git checkout <branch-name>

Joining new branches back into the master branch

Once you complete a feature or resolve a bug you might wish to port the changes back to the master branch.

There are two ways of joining branches; merging branches and rebasing branches.

Git merge

git merge <branch-name> - merges <branch-name> into the current branch and creates a so-called merge commit where it commits the different changes from both branches.

git merge

Git rebase

git rebase <branch-name> - applies the changes from <branch-name> and then re-applies the changes of the current branch on top.

git rebase

Typical workflow

$ git branch new-feature  # Create a new branch
$ git checkout new-feature  # Move to the new brach
$ echo "Adding a line to README" >> README.md  # Edit a file
$ git add README.md  # Add a file to staging
$ git commit -m "Update README"  # Commit the file
$ git checkout master  # Move back to the master branch
$ git pull  # Fetch the new changes on master
$ git checkout new-feature  # Move back to the new branch
$ git rebase master  # Join changes from master to the new branches
$ git checkout master  # Move to the master branch
$ git merge new-feature  # Merge the changes into the master branch


git fire

Want to get weekly updates on how to write software with less code. Then sign up for the newsletter: