Managing your sources with Git (EN)

Développement  Source 

This course presents Git & Github. Git is used to centralize and log the changes on your code.

Comprendre Git

For information about Git you can find

For who, why ?

A version control system (VCS: version control system) helps to manage the history of the changes made by

  • people

  • teams

  • or only one person

who collaborate on a project

When the project evolves people can at any time find an old version of the code.

Git answers these questions quickly

  • What changes have been made?

  • Who made the changes?

  • When were the changes made?

  • Why were the changes made?

Git is a decentralized source management software (DVCS) created in 2005 by Linus Torvald (creator of the Linux kernel). The sources are stored both on the computer of each contributor of the project, but also on a centralized server.

Figure 1. logo Git

Today, most of the open source or commercial projects developed use Git (90% of developers, according last stackoveflow insights).

Git allows access to all files, branches, versions (releases) or tags of a project to authorized people.

Basically you don’t need a centralized solution wirth Git, but online solutions have emerged like Github or Gitlab.

Figure 2. logo Github
Figure 3. logo Gitlab

This services extend Git and they offer more features like pull requests, code reviews…​

What did we ever do before ?

Without Git it was difficult to follow the history, we had multiple copies of project

Early solutions, like CSV or SVN (Subversion) were quickly limited. It was often awful to refactor your code because of their limitations

What’s a repository ?

A Git repository contains all the files and directories associated with your project but you they contain also all the history of the modifications made on each one.

Publishing changes is called a commit. Each commit is linked to another (this is a chained list). Commits can be organized into several parallel branches

Projects are stand-alone units, and anyone with a copy of the repository can access the entire code and its history. You can do different operations like logging, cloning, branch creation, commit, merging …​

Figure 4. Centralized repository, local repository

Generally on a project we have a main branch master. And we open different branches in parallel to fix bugs, add new features. These branches are merged on this main branch when they are completes

Figure 5. Branch Principle

Install Git

If you use Windows, you can download Git here

On a Linux (Fedora)

sudo yum install git

On Linux (Debian, Ubuntu) use

sudo apt-get install git

To check your install, display the git version in terminal

~$ git --version

git version 2.29.0

It’s important to provide some information to Git, especially when you push your changes to a centralized repository.

~$ git config --global "Guillaume EHRET Dev-Mind"
~$ git config --global ""

Recover a Git project

Go in your working directory, and in a terminal type

~$ git clone

The remote project git-demo is downloaded on your computer. You get all the history, all the files, all the branches …​

Initialize a Git repository

It’s better to start from 0. Delete the directory to recreate it

~$ cd ..
~$ rm -rf git-demo
~$ mkdir git-demo

To link this directory to Git, launch

~$ git init

Initialized empty Git repository in /home/devmind/Workspace/java/git-demo/.git/

A .git directory has been added and it contains the different files, logs, traces

~$ cd git-demo/
~$ ls -la
total 28
drwxr-xr-x  3 devmind devmind  4096 sept. 26 22:46 .
drwxr-xr-x 10 devmind devmind  4096 sept. 26 22:46 ..
drwxr-xr-x  8 devmind devmind  4096 sept. 26 22:46 .git

Linking files to Git

Let’s start by creating files in this git-demo directory

~$ echo "Hello world" > hello.txt
~$ echo "<html><body><h1>Hello World</h1></body></html>" > hello.html
~$ echo "Hello world" >
~$ ls
hello.html  hello.txt

Use the git add command to indicate that a new or multiple files will be handled by git.

~$ git add hello.txt (1)
~$ git add .         (2)

1. Adds hello.txt file
2. Adds all the files

At this moment, our files are not saved in Git. They are in a staging area. We have only a snapshot of the changes

Saving modifications

The following command saves the snapshot in the project history and completes the change tracking process. In short, a commit works like taking a photo. We freeze in time what we have done.

~$ git commit -m "First commit"     (1)

[master a7d5b84] First commit       (2)
 3 files changed, 3 insertions(+)   (3)
 create mode 100644 hello.html
 create mode 100644
 create mode 100644 hello.txt

1. -m to add a message. It’s very important to be able to understand why a commit was made
2. Git displays the name of the branch =⇒ master and the hash of the commit` a7d5b84` (this is the number of the photo)

Know the current state

We will delete the hello.txt file, update`` and add a hello.adoc file

~$ rm
~$ echo "My first Git example" >
~$ echo "Hello World" > hello.adoc

Launch the following command to know where Git is

~$ git status

What do you see ?

~$ git add .
~$  git commit -m "Second commit"

[master 7b7d8e6] Second commit
 3 files changed, 2 insertions(+), 2 deletions(-)
 create mode 100644 hello.adoc
 delete mode 100644 hello.txt
~$ git status

What do you see ?

See history

Use the log command

~$ git log

commit 7b7d8e69a06af284c9da7aa4a8c28835d23318fe (HEAD -> master)
Author: Guillaume EHRET Dev-Mind <>
Date:   Wed Sep 26 23:22:46 2018 +0200

    Second commit

commit a7d5b843ebc65ac6e94c37872d6a936e1c03a6b5
Author: Guillaume EHRET Dev-Mind <>
Date:   Wed Sep 26 23:08:00 2018 +0200

    First commit

We find our two commits, with the names we have set, at what time …​

See current changes

We will update a file and run the diff command

~$ echo "Fichier Asciidoc" > hello.adoc
~$ git diff

You should see, what has been added and removed in the file. When we an (IDE) we will have more visual tools for tracking changes

Go back in the history

You can use a reset to go back to the state of the last commit

~$ git reset --hard
~$ git diff

Working with branches

See the current branch

Run the following command to see the current branch

~$ git branch
* master

Character * shows the current branch

Create a branch

A branch is just a name without special characters or spaces. To create a branch we use git branch [name]

~$ git branch test
~$ git branch
* master

By default the created branch is not active (it does not have *)

A prefix is often used by convention, when you want to name a branch. for example

  • fix/1233-hair-color: a branch to correct (fix) a hair color problem. The ticket number of the bug tracker is often indicated

  • feat/add-glasses: a branch to add a feature

  • chore/upgrade-jquery: a branch to perform a technical task

Change the current branch

~$ git checkout test
~$ git branch
* test

The test branch is now the default

You can make a change and save it

~$ echo "Fichier Asciidoc updated" > hello.adoc
~$ git add .
~$ git commit -m "Third commit"

You can launch the following commands

~$ git log
~$ git checkout master
~$ git log

What do you see ?

Compare 2 branches

We will reuse the git diff command but we specify the 2 branches separated by …​

~$ git diff test...master

This command should show nothing because test is based on` master` and it is just ahead, it contains all master commits

~$ git diff master...test

This time, as test branch is the reference, Git detects that there are differences

~$ git log

commit 4529128a723e0a16cf405b218f37f2da58c5a9fd (HEAD -> test)  (1)
Author: Guillaume EHRET Dev-Mind <>
Date:   Thu Sep 27 00:00:00 2018 +0200

    Third commit

commit 9fd87d1ffc654a74105f3f279032e7f88d3d265b (master)       (2)
Author: Guillaume EHRET Dev-Mind <>
Date:   Wed Sep 26 23:51:10 2018 +0200

    Second  commit

1. Git indicates the HEAD of the test branch
2. Git displays the name of the parent branch =⇒ master and where it is in the history

You can go back to master and create another branch` test2`

~$ git checkout master
~$ git branch test2
~$ git checkout test2
~$ echo "Fichier toto" > toto.adoc
~$ git add .
~$ git commit -m "Fourth commit"

Merge 2 branches

If you want to post your changes from the test branch to` master`, you’re going to do a merge

~$ git checkout master
~$ git merge test

Delete a branch

You can delete a branch if this branch is not the active one

~$ git checkout test
~$ git branch -d test
error: Cannot delete branch 'test' checked out at '/home/devmind/Workspace/java/git-demo'

~$ git checkout master
~$ git branch -d test
Deleted branch test (was 9fd87d1).


You can install a centralized Git repository by using Github Enterprise, Gitlab Enterprise, Gogs. But you can also use an online service

  1. Create an account under Github

  2. Once you are logged in on Github, you can create your first repository

Nouveau projet sous Github
Figure 6. New project under Github

Github provides you commands to connect this remote repository to your local repository

Github vous fournit les commandes pour relier ce dépot
Figure 7. commands to connect this remote repository

To link your local project type the following commands

~$ git checkout master
~$ git remote add origin  (1)
~$ git push -u origin master                                       (2)

1. Git adds remote origin to your local project
2. The push command allows to send what you have on the current branch (local master branch) on the server

Github allows teams to synchronize throughout the life of a project. You can push any branch.

~$ git checkout feat/new_hair
~$ git push -u origin feat/new_hair

These commands

  • activate the feat/new_hair branch and

  • push the changes on Github.

If you want to retrieve local changes made by your colleagues do

~$ git pull

The git fetch command retrieves the history of all branches locally.

Git Project Workflow

When we are working in team, the workflow will be

Create a branch
We usually create a branch from master (or another branch). Some companies have, for example, a branch dev for the current developments, a branch prod that corresponding to what is in production. Branches created on these canonical branches must have a short life duration.

Add commits
Every small changes should be made as often as possible. Each commit, within a branch creates restoration points in the project history.

Open a pull request
When you pushed your changes on Github, you can open a pull request. This allows to discuss about the current development (for example with the people who will test)

Code Review
In a mature development team, your code is always read by another developers. Your code is often also tested by other people. Anyone can make returns on the Pull request. You can make changes on the branch and pushed other commits to fix the review remarks.

Merge +When everything is OK you can click the Merge button on the pull request. GitHub automatically performs the equivalent of a local 'git merge' operation.

Teams can install your updates or continue development


There’s still a lot of things to say about Git and Github. Some concepts can be complex like rebase or conflict resolution. IDEs will simplify some tasks. You can find more resources on the web

This course is there to give you the basics in order to be able to realize the TP using Git. You are now able to save your sources, to share them, to find them easily from one computer to another. So enjoy with Git