Git training course
Git concepts with examples
Agenda
2
�
Let's make it interactive!��Any questions is more than welcome during the presentation and…��remember that there are no dumb questions!
�“There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand the world. There is no such thing as a dumb question.” ― Carl Sagan
3
What is git?
4
Git concepts with examples
Stefano Cilloni
What is version control?
5
Git concepts with examples
Stefano Cilloni
So... what is git?
6
Git concepts with examples
Stefano Cilloni
What is git for
What git is NOT for
7
Git concepts with examples
Stefano Cilloni
CLI vs GUI
but... GUIs are good for:
We will see Meld Merge tool.��Also, VS Code version control feature is very good!
8
Git concepts with examples
Stefano Cilloni
Git from CLI
9
Git concepts with examples
Stefano Cilloni
10
Why concepts?
11
Git concepts with examples
Stefano Cilloni
Git concepts
12
Git concepts with examples
Stefano Cilloni
Git terms in following slides are orange
Git repository or “repo”
13
Git concepts with examples
Stefano Cilloni
$ tree -a git_repository
git_repository� ├─ .git� ├─ README� └─ src� └─ main.c�
Practice Lab — init and clone
# initialize a new git repository
mkdir /tmp/alice-repo
cd /tmp/alice-repo
git init
# -------------- or ------------------�# clone an existing remote repository
�git clone <git-remote-address> /tmp/alice-repo��cd /tmp/alice-repo
cd /tmp/alice-repo�
# git needs your name and email for commits metadata��git config --global user.name "My Name"�git config --global user.email "email@example.com"���
# ask git about the repository status
git status
14
Git concepts with examples
Stefano Cilloni
practice lab #1
Here is the final result of “alice repo”:�https://gitlab.com/nyxgear/alice-repo
The Three States
Git concepts
15
Git concepts with examples
Stefano Cilloni
modified
staged
committed
You have modified the file.�
(Not marked for commit, yet)
(Not committed into the git database)
A modified file is marked to go into your next commit / save.
Committed means that the data is safely stored in your local git database.
That leads to… 3 “sections” of a Git repository
Git concepts
16
Git concepts with examples
Stefano Cilloni
The working tree or working directory is one version of the project and it is made by all the files you work on.��Git gets these files from its database (the .git folder) and places them on disk for you to use.
The staging area is a “zone”/”state” that stores information about what will go into your next commit.
Called “staging area” or “index”.
The .git directory is the database Git uses to store history and all information about the project.�It is copied when you clone the repository from another computer or it gets created on the initialization of a repository.
A basic git workflow
17
Git concepts with examples
Stefano Cilloni
modify
selectively stage
commit
Practice Lab — committing
# NOTE. Setup is from practice lab #1�cd /tmp/alice-repo���# create a new file�echo 'Hello git!' > hello.txt��# git, is there something new?�git status
# add the file to the staging area�git add hello.txt
# commit�git commit -m 'My first commit in git'�
�# check status again�git status
# check log to see commits history�git log
18
Git concepts with examples
Stefano Cilloni
practice lab #2
Recap: The Three States
A particular version of a file stored in the .git directory.
File that was modified and added to the staging area.
File that was changed since the last time it was checked out but has not been staged.
19
Git concepts with examples
Stefano Cilloni
modified file
staged file
committed file
Snapshots, not differences
Git concepts
20
Git concepts with examples
Stefano Cilloni
Differences of File A (from v2) included in version 4
Delta differences that makes �version 5
Snapshots, not differences
21
Git concepts with examples
Stefano Cilloni
Link to file A: version 2
Snapshot of file A
(because changed in version 4)
Snapshot that makes version 5
snapshots commits
are
Snapshots, not differences: comparison
22
Git concepts with examples
Stefano Cilloni
Link to file A: version 2
Snapshot of file A
(because changed in version 4)
Snapshot that makes version 5
Delta differences that makes version 5
Differences of File A (from v2) included in version 4
Other VCSs
git
Snapshots, not differences: summary
23
Git concepts with examples
Stefano Cilloni
Git Only Adds Data
Git concepts
24
Git concepts with examples
Stefano Cilloni
Git Has Integrity
Git concepts
25
Git concepts with examples
Stefano Cilloni
Practice Lab — inspect content of commit
# NOTE. Setup is from practice lab #2��cd /tmp/alice-repo��# check project history�git log
�# copy the checksum for commit with title�'My first commit in git'
�# inspect changes within that commit�git show <your-checksum>��
# Now, let’s add new changes and then let’s see how git stores them��echo 'This change deletes previous content' > hello.txt
echo 'Here is another file' > foo.txt��# add all modified files to staging area�git add --all # or: git add .
# commit�git commit -m 'My second commit'
# check log showing modified files�git log --stat��# check log showing single changes in commits �git log --patch
26
Git concepts with examples
Stefano Cilloni
practice lab #3
Every Operation Is Local
Git concepts
27
Git concepts with examples
Stefano Cilloni
Git is distributed
Git concepts
28
Git concepts with examples
Stefano Cilloni
Practice Lab — remotes
# NOTE. Setup is from practice lab #3
# create another local repository�mkdir /tmp/bob-repo
cd /tmp/bob-repo
# initialize it�git init��# --- required setup ----�git config receive.denyCurrentBranch updateInstead�# -----------------------
# link the two repositories to synchronize them
cd /tmp/alice-repo
# add reference to the other repository copy �git remote add remote-bob-copy ../bob-repo��# see remotes �git remote -v
�# send/push stored commits of master to remote�git push remote-bob-copy master��cd /tmp/bob-repo && ls�
# check project history in bob’s repository copy�git log
29
Git concepts with examples
Stefano Cilloni
practice lab #4
For this lab, you can work offline!
The Commits Graph
Git concepts
30
Git concepts with examples
Stefano Cilloni
branch operation
merge operation
Examples of some project graphs!
Each commit is made of
31
Git concepts with examples
Stefano Cilloni
Do you remember the working directory?
Commits graph
Is the entire history of changes occurred in the project�
Working directory
think to it as a “window” through which you see one specific state in time � of your project ��Indeed, Git is a “time machine”! ��The git checkout command “moves this window”/your working directory through the project history, visualizing the state of the project you pick
32
Git concepts with examples
Stefano Cilloni
Git References
Git concepts
HEAD
branches
tags
remotes
33
Git concepts with examples
Stefano Cilloni
Contents of this slide are simplified and reworked for clarity of teaching. Complete guide at https://git-scm.com/book/en/v2/Git-Internals-Git-References
Git References — HEAD
34
Git concepts with examples
Stefano Cilloni
HEAD in the commit graph
35
Git concepts with examples
Stefano Cilloni
HEAD
$ tree -a git_repository
git_repository� ├─ .git� ├─ README� ├─ src� │ ├─ function_X.c� │ ├─ function_Y.c� │ ├─ function_Z.c� │ └─ main.c� └─ VERSION
HEAD
$ tree -a git_repository
git_repository� ├─ .git� ├─ README� └─ src� ├─ function_X.c� ├─ feature_RED� │ ├─ module_A.c� │ ├─ module_B.c� │ ├─ module_C.c� │ └─ feature_red.c� └─ main.c�
HEAD
$ tree -a git_repository
git_repository� ├─ .git� └─ README
HEAD
$ tree -a git_repository
git_repository� ├─ .git� ├─ README� └─ src� ├─ function_X.c� ├─ feature_RED� │ ├─ module_A.c� │ └─ feature_red.c� └─ main.c�
Git References — branches
36
Git concepts with examples
Stefano Cilloni
Branches in the commit graph
37
Git concepts with examples
Stefano Cilloni
feature-export-to-pdf
HEAD should always �point to a branch or tag��������If not, so pointing directly to a commit, �git warns you:� “You are in 'detached HEAD' state.”
HEAD
main
new-release-0.3.1
refactor-library-inclusion
Practice Lab — intro to branches
# NOTE. Setup is from practice lab #4
cd /tmp/alice-repo
# check where the HEAD is currently pointing at�git log
#------------------------�# create a new branch�git checkout -b another-branch
# check where the HEAD is currently pointing at�git log
# make changes and make a new commit�echo "coffee" > bar.txt�git add .�git commit -m "commit on other branch"��git log # see HEAD position��# see branches�git branch -v�
#------------------------�# switch back to master / main branch�git checkout master
ls # the file bar.txt should not be present
git log # see HEAD position��#------------------------�# make changes and make a new commit�echo "with more changes" >> foo.txt�git add .�git commit -m "3rd commit on master"
# notice the graph, branches and HEAD�git log --all --graph��# copy commit hash for “My first git commit”
git checkout <copied-hash>
# git should warn you��# go back to the branch another-branch�git checkout another-branch
38
Git concepts with examples
Stefano Cilloni
practice lab #5
Git References — tags
39
Git concepts with examples
Stefano Cilloni
Tags in the commit graph
40
Git concepts with examples
Stefano Cilloni
feature-export-to-pdf
main
new-release-0.3.1
refactor-library-inclusion
v0.2.0
v0.1.0
v0.3.1
preview-export-pdf
HEAD
HEAD should always �point to a branch or tag
Practice Lab — tags
# NOTE. Setup is from practice lab #5
cd /tmp/alice-repo��# see current tags�git tag -n
git log --all
# copy commit hash for “My second commit”
git checkout <copied-hash>
# mark the current project state with a tag�git tag v0.1.0
# see current tags�git tag -n
#------------------------�# create another tag
# move back to see master�git checkout master
# mark the current checked out commit with a tag�git tag v0.2.0
# see current tags�git tag -n
�#------------------------�# move HEAD to desired tag�git checkout <tag-name>
41
Git concepts with examples
Stefano Cilloni
practice lab #6
Git References — remotes
42
Git concepts with examples
Stefano Cilloni
remote name
Distributed but… also “centralized”
Git concepts
By convention one of the remotes is considered “the main one”. �Its name, by convention, is “origin”�
43
Git concepts with examples
Stefano Cilloni
Practice Lab — push to remote repository
# NOTE. Setup is from practice lab #6
cd /tmp/alice-repo��# see current remotes �git remote -v
# add the GitLab remote -- you can get this link from the “clone” button in the project page�git remote add origin git@gitlab.com:<your-username>/alice-repo.git
# check remotes again�git remote -v
# push current project history to GitLab copy of the repository�git push origin master
44
Git concepts with examples
Stefano Cilloni
practice lab #7
45
Git
CVS
46
Git concepts with examples
Stefano Cilloni
Git vs. CVS
1
Git
CVS
47
Git concepts with examples
Stefano Cilloni
Git vs. CVS
2
Git
CVS
48
Git concepts with examples
Stefano Cilloni
Git vs. CVS
3
Why Migrate
49
Git concepts with examples
Stefano Cilloni
Git vs. CVS
Thank you
Git training course
�Git concepts with examples
Credits and References
51
Git concepts with examples
Stefano Cilloni
LICENSE
All contents and material in the current presentation are licensed under
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
52
Git concepts with examples
Stefano Cilloni