This services extend Git and they offer more features like pull requests, code reviews…
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
It was often awful to refactor your code because of their limitations
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 …
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
If you use Windows, you can download Git with https://git-scm.com/downloads [this link]
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.19.0
It’s important to provide some information to Git, especially when you push your changes to a centralized repository.
~$ git config --global user.name "Guillaume EHRET Dev-Mind" ~$ git config --global user.name "firstname.lastname@example.org"
Go in your working directory, and in a terminal type
~$ git clone https://github.com/Dev-Mind/git-demo.git
The remote project
git-demo is downloaded on your computer. You get all the history, all the files, all the branches …
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/
.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
Let’s start by creating files in this
~$ echo "Hello world" > hello.txt ~$ echo "<html><body><h1>Hello World</h1></body></html>" > hello.html ~$ echo "Hello world" > hello.md ~$ ls hello.html hello.txt hello.md
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
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 hello.md create mode 100644 hello.txt
-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)
We will delete the
hello.txt file, update` hello.md` and add a
~$ rm hello.md ~$ echo "My first Git example" > hello.md ~$ 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 ?
Use the log command
~$ git log commit 7b7d8e69a06af284c9da7aa4a8c28835d23318fe (HEAD -> master) Author: Guillaume EHRET Dev-Mind <email@example.com> Date: Wed Sep 26 23:22:46 2018 +0200 Second commit commit a7d5b843ebc65ac6e94c37872d6a936e1c03a6b5 Author: Guillaume EHRET Dev-Mind <firstname.lastname@example.org> Date: Wed Sep 26 23:08:00 2018 +0200 First commit</email@example.com></firstname.lastname@example.org>
We find our two commits, with the names we have set, at what time …
We will update a file and run the
~$ 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
You can use a
reset to go back to the state of the last commit
~$ git reset --hard ~$ git diff
Run the following command to see the current branch
~$ git branch * master
Character * shows the current 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 test
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
~$ git checkout test ~$ git branch master * test
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 ?
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 <email@example.com> Date: Thu Sep 27 00:00:00 2018 +0200 Third commit commit 9fd87d1ffc654a74105f3f279032e7f88d3d265b (master) (2) Author: Guillaume EHRET Dev-Mind <firstname.lastname@example.org> Date: Wed Sep 26 23:51:10 2018 +0200 Second commit ...</email@example.com></firstname.lastname@example.org>
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"
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
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).
Create an account under Github
Once you are logged in on Github, you can create your first repository
Github provides you commands to connect this remote repository to your local repository
To link your local project type the following commands
~$ git checkout master ~$ git remote add origin https://github.com/Dev-Mind/git-demo.git (1) ~$ git push -u origin master (2)
[.small]#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
feat/new_hair branch and
push the changes on Github.
If you want to retrieve local changes made by your colleagues do
~$ git pull
git fetch command retrieves the history of all branches locally.
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.
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)
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
conflict resolution. IDEs will simplify some tasks. You can find more resources on the web http://try.github.io/
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