SlideShare a Scribd company logo
I H S C
Introduction to git:
An Efficient Distributed Version Control System
pmxal9@nottingham.ac.uk
18th November 2016
Introduction
git alone
git + server
Complementary
1
Version Control System
Stand-alone application managing changes to documents.
Bad Example
Good Example, using Git branches
2
Version Control System
Stand-alone application managing changes to documents.
Bad Example
thesis.pdf
thesis_V1.pdf
thesis_V2.pdf
thesis_V2bis.pdf
thesis_V3_withChris_comments.pdf
thesis_V4_withChris_commentsV2.pdf
Good Example, using Git branches
2
Version Control System
Stand-alone application managing changes to documents.
Bad Example
thesis.pdf
thesis_V1.pdf
thesis_V2.pdf
thesis_V2bis.pdf
thesis_V3_withChris_comments.pdf
thesis_V4_withChris_commentsV2.pdf
Good Example, using Git branches
thesis.pdf
2
Think of nodes as points in time pointing towards their past:
3
Think of nodes as points in time pointing towards their past:
What is the history of H?
3
Think of nodes as points in time pointing towards their past:
What is the history of H? H G F B A
3
Think of nodes as points in time pointing towards their past:
What is the history of H? H G F B A
What is the history of E?
3
Think of nodes as points in time pointing towards their past:
What is the history of H? H G F B A
What is the history of E? E D C B A
3
Think of nodes as points in time pointing towards their past:
What is the history of H? H G F B A
What is the history of E? E D C B A
What is the history of K? K J I C B A
3
Think of nodes as points in time pointing towards their past:
What is the history of H? H G F B A
What is the history of E? E D C B A
What is the history of K? K J I C B A
Each branch is a parallel universe.
Each branch shares a part of its past with other branches.
3
Think of nodes as points in time pointing towards their past:
← nobrexit
← master
← notrump
What is the history of H? H G F B A
What is the history of E? E D C B A
What is the history of K? K J I C B A
Each branch is a parallel universe.
Each branch shares a part of its past with other branches.
→ Depending on where you start, some nodes are unreachable.
3
4
Git Tools
This seminar is about
• init
• add
• branch
• status
• commit
• merge
• clone
• push
• fetch
• pull
and behind the scene
5
Git Tools
This seminar is about
• init
• add
• branch
• status
• commit
• merge



to use git alone
• clone
• push
• fetch
• pull
and behind the scene
5
Git Tools
This seminar is about
• init
• add
• branch
• status
• commit
• merge
• clone
• push
• fetch
• pull



to use git with a server
and behind the scene
5
Git Tools
This seminar is about
• init
• add
• branch
• status
• commit
• merge
• clone
• push
• fetch
• pull
and behind the scene



to use git on collaborative projects
5
Goals
• Create new repo(sitory)
• Put some code in remote server
• Use GitX to check things work as wanted
• Wait for collaborators to change the code
• Get the changes on my computer
• Experiment in a different branch
6
Requirements
Terminal/Command Prompt
Depending on you Operating System
Command-Line Basics
• pwd Current working directory
• cd <d> Change directory to <d>
• ls <d> List out directory <d>
• cp <a> <fd> Copy <a> to <fd>
• echo <s> Display input <s>
• cat <f> Display file <f>
• rm <f> Remove <f>
Text Editor
vim, nano, emacs...
7
Introduction
git alone
git + server
Complementary
8
$HOME alone
Most of your workflow with git is on your local computer
9
git States
git has 3 main states files can reside in:
• Commited: data is safely stored in your local database
• Modified: file has been changed but not committed yet
• Staged: file marked as going into next commit snapshot
10
git States
git has 3 main states files can reside in:
• Commited: data is safely stored in your local database
• Modified: file has been changed but not committed yet
• Staged: file marked as going into next commit snapshot
leading to three main sections of a Git project:
• git directory,
• working directory,
• staging area.
Pro Git, Chacon&Straub
10
git States
Pro Git, Chacon&Straub
At any moment, a file is in one of these states:
Untracked Modified: Staged: Commited:
: > a echo a >> lol git add lol git commit -m "l"
git status git status git status git status
11
A Basic Workflow
Edit files vim <file>
Stage the changes git add <file>
Review your changes git status / git diff
Commit the changes git commit -m "<msg>"
Edit files .....
Stage ............
Commit ........
12
Commit → Safety
Commited data (stored in the repo) is fairly hard to remove.
It makes it hard to lose your work once it’s commited.
Commit:
• Create snapshot of whole repo + history
• Add a diff in database
• Generate SHA-1 40 bytes hexadecimal string of it
$ git log --pretty=oneline
71c1395efd640a476ab78d62c3da13caa9388065 Work git seminar
96b4cfa3158141bf0e0321780876358114e7c229 Testing gitignore
0c4efa32205c8f2faef415dd7cc0f484bfdab1e2 some changes
7d8bfb1b04fefc641e9556933ee7a45b956086e6 rm DS_Stores
76b68b6f2fb82f3cfbc1cc52b31f217267d85ce9 Commit
f683feade8ba48bda78fabc20ce4aceba4563470 First commit
13
Branches
Branching means you diverge from the main line of development
and continue to do work without messing with that main line.
Git’s killer feature is its branching model
14
Working with Branches
Creating a branch...
15
Working with Branches
Creating a branch...
takes 41 bytes,
15
Working with Branches
Creating a branch...
takes 41 bytes,
is virtually instantaneous,
15
Working with Branches
Creating a branch...
takes 41 bytes,
is virtually instantaneous,
creates a reference easy to come back to.
15
Working with Branches
Creating a branch...
takes 41 bytes,
is virtually instantaneous,
creates a reference easy to come back to.
A branch is simply a pointer to a commit.
Pro Git, Chacon&Straub
15
Working with Branches
Creating a branch...
takes 41 bytes,
is virtually instantaneous,
creates a reference easy to come back to.
A branch is simply a pointer to a commit.
Pro Git, Chacon&Straub
Creating a branch is like saving the game before battling a boss.
Plato, about Git, 429 BC
15
References make commits reachable
Attaching your modifications to a branch enables you to go back
to them by following pointers.
16
References make commits reachable
Attaching your modifications to a branch enables you to go back
to them by following pointers.
← featureX
← master
← issue539
16
Working with Branches
git branch newB to create a new branch,
git checkout coolFeature to move to coolFeature branch,
git checkout -b newB locally create a new branch & switch to.
To check in which branch you are: git status / git branch
17
Experimenting in New Branch
By default, original branch is called master.
HEAD is a symbolic reference to the branch you are currently in.
18
Experimenting in New Branch
By default, original branch is called master.
HEAD is a symbolic reference to the branch you are currently in.
Workflow:
• Create new branch, git branch nB
18
Experimenting in New Branch
By default, original branch is called master.
HEAD is a symbolic reference to the branch you are currently in.
Workflow:
• Create new branch, git branch nB
• Checkout to this branch, git checkout -b nB
18
Experimenting in New Branch
By default, original branch is called master.
HEAD is a symbolic reference to the branch you are currently in.
Workflow:
• Create new branch, git branch nB
• Checkout to this branch, git checkout -b nB
• Develop new features in it, git add/commit/add/commit..
18
Experimenting in New Branch
By default, original branch is called master.
HEAD is a symbolic reference to the branch you are currently in.
Workflow:
• Create new branch, git branch nB
• Checkout to this branch, git checkout -b nB
• Develop new features in it, git add/commit/add/commit..
• Merge, git checkout B; git merge nB
18
Experimenting in New Branch
By default, original branch is called master.
HEAD is a symbolic reference to the branch you are currently in.
Workflow:
• Create new branch, git branch nB
• Checkout to this branch, git checkout -b nB
• Develop new features in it, git add/commit/add/commit..
• Merge, git checkout B; git merge nB
• Delete branch. git branch -d nB
18
Merging
Git has two strategies for merging, depending on reachability:
Reachable: Fast-forward merging, just moves a pointer.
19
Merging
Git has two strategies for merging, depending on reachability:
Reachable: Fast-forward merging, just moves a pointer.
Pro Git, Chacon&Straub 19
Merging
Git has two strategies for merging, depending on reachability:
Reachable: Fast-forward merging, just moves a pointer.
Unreachable: Recursive merging, commit with two parents.
Pro Git, Chacon&Straub
19
Merging
Git has two strategies for merging, depending on reachability:
Reachable: Fast-forward merging, just moves a pointer.
Unreachable: Recursive merging, commit with two parents.
Pro Git, Chacon&Straub
19
Hands-on: just on your machine
Five minutes to use on your computer:
• git init Initialise a git skeleton inside chosen folder
• git add <F> Start tracking or stage changes <F>
• git commit -m "<M>" Commit changes with msg <M>
• git status Get general state information
• git diff Get patch stage information
• git branch <B> Create a new branch
• git checkout <B> Switch to branch <B>
• git merge <B> Merge branch <B> with current branch
20
Introduction
git alone
git + server
Complementary
21
Remote Server
Download git repo located at given url into new directory
git clone <url>
Fetch the current version of current repo on remote server
git fetch
Push your current git repo to remote server
git push
Check your remote servers:
git remote -v
22
Quicker than fetching + merging
git pull = ‘git fetch‘ + ‘git merge FETCH_HEAD‘
23
Hands-on: Let’s Collaborate
• create an empty repo "ourRepo" using a web browser
• git clone to local computer
• git add a README.md file with information for users
• git add to start tracking files, git commit, git push
• Users git pull (first clone), apply some changes and git push
(pull request since you don’t have the authorisations)
• Do pull request 24
Hands-on: Let’s Collaborate
• create an empty repo "ourRepo" using a web browser
cd path/to/myRepos
• git clone to local computer
git clone git@bitbucket.org:allevity/ourRepo.git
• git add a README.md file with information for users
a="Clone project and follow README instructions"
echo $a > ourRepo/letsPlayAGame
cp Seminars/git/introGit.pdf ourRepo/introGit.pdf
• git add to start tracking files, git commit, git push
git add -A; git commit -m "First commit"; git push
• Users git pull (first clone), apply some changes and git push
(pull request since you don’t have the authorisations)
Fork to your account yourName
git clone git@bitbucket.org:yourName/ourRepo.git
git add -A; git commit -m "your msg"; git push
• Do pull request 24
Checking Commits since 3y 4m 5h 6s ago
Literally...
git log --since="3 years 4 months 5 hours 6 seconds ago"
Check what your collaborators have commited during your sleep:
git log --since="15 hours ago"
25
Tagging
Tags are pointers towards a fixed commit:
git tag -a V1.4 -m "Thesis_V1.4 with Chris comments"
To list all tags or look at a specific one:
git tag
git show V1.4
26
How does git work?
...
27
XKCD 28
Introduction
git alone
git + server
Complementary
29
Workflows in Centralised VS Distributed Systems
Centralised Integration Manager Benevolent Dictator
Pro Git, Chacon&Straub
Centralised
server special, merges difficult, communication slow & necessary.
Distributed
server is just copy, easy to merge, most work is local.
https://www.youtube.com/watch?v=_yQlKEq-Ueg
30
Which Web-based Git Repository Hosting Service?
Short answer:
• GitHub is the biggest player, but no private repo for free*
• Bitbucket has free private repos, but visualisation and
traffic info missing
* Just tried https://education.github.com/ to get free repos.
They say it might take a few weeks...
31
gitignore
A .gitignore file forces git to not track some files or folders,
according to regular expression patterns.
Example
$ cat .gitignore
# Don’t keep this sub repository
mySubRepo/*
# Data too big to fit on server
data/*
# LaTeX minitoc, generated when executing LaTex code
*.mlt
*.mtc[0-9]*
# No .txt in log/, except thisisimportant.txt
log/**/*.txt
!log/thisisimportant.txt
Use templates:
https://github.com/github/gitignore 32
Raising & Solving Issues
When you spot a problem in Git-based project, raise an issue.
33
Dealing with Conflicts
When you change a line of code that a collaborator changed
before you, git will refuse your push.
Hence, always git pull before starting to work.
34
Deleting Branches
Safe (checks commit remains reachable):
git branch -d myBranch
Unsafe (may make commits unreachable):
git branch -D myBranch
35
Deleting Branches
Even if a commit is not reachable, knowing its SHA-1 is enough
to get it back in our graph.
To create a branch pointing to a commit that was not reachable:
git branch -D myBranch
# Deleted branch myBranch (was e11cd74).
git branch myBranchBack e11cd74
36
Changing the past
It is considered rude to change the history of a public project.
But if you need to, git rebase moves commits in the graph.
37
A Word About Security
Thanks to its checksum-based implementation, Git is secure:
• Delete project, only keep its last commit SHA-1 (41 bytes),
• Download a copy from a completely insecure source,
• Check its SHA-1 is the same as yours.
You can trust this is exactly your project: no corruption on it.
38
A Word About Security
Thanks to its checksum-based implementation, Git is secure:
• Delete project, only keep its last commit SHA-1 (41 bytes),
• Download a copy from a completely insecure source,
• Check its SHA-1 is the same as yours.
You can trust this is exactly your project: no corruption on it.
Maybe your projects aren’t that important.
My projects, they’re important.
There’s a reason I care.
Linus Torvalds, speaking to Google about Git, 2007.
38
Some useful commands
Tracking Files: some options
• Add all files
git add -a
• Add a given file/folder
git add myFile src/myFolder/
• Choose what changes to add manually (‘y’ or ‘n’ changes)
git add -p
39
Some useful commands
Skip staging area, staging all files already tracked:
git commit -am "<M>"
Unstage a file (reset without option only changes staging area):
git add lol
git reset HEAD lol
Change a commit:
git commit -m ’initial commit’
git add forgotten_file
git commit --amend
40
Some useful commands
Remove changes done to a tracked & modified & unstaged file:
echo "new change" >> lol
git checkout -- lol # SOME DATA IS LOST!
Check what you commit:
git diff
git status -s with two columns: staging area & working area
41
Some useful commands
Search in repo:
git grep ’some string’
Show list of commits:
git log # All commits
git log -S ’some stuff’ # Search string
git log --author ’supervisor’ # Did he work?
git log --oneline --abbrev-commit --all --graph --decorate
Make aliases:
git config --global alias.lol 
"log --oneline --graph --decorate"
42
Some useful tools
GitX to visualise your git tree
43
Summary
git init git checkout
git clone git merge
git add git push
git status git fetch
git commit git pull
git branch git log
44
Remerciements
Thank you for your attention
Linus for awesome work
Scott Chacon for wonderful Git material
GitHub & Bitbucket for free repos
45
References
Online Hands-on
https://try.github.io
Introduction to Git with Scott Chacon of GitHub, 2011:
https://www.youtube.com/watch?v=ZDR433b0HJY
Linus Torvald, Google HQ, 2007:
https://www.youtube.com/watch?v=4XpnKHJAok8
A gentle intro:
http://think-like-a-git.net/
Scott Chacon’s free e-book
https://git-scm.com/book/en/v2
46

More Related Content

What's hot (19)

PPTX
Mastering git - Workflow
Tahsin Abrar
 
PDF
Git and Version Control at Atlogys
Atlogys Technical Consulting
 
PPTX
Git tutorial
Pham Quy (Jack)
 
PPTX
Git Basic
Luke Luo
 
PPTX
Git & Github
Aman Lalpuria
 
PPTX
Mastering GIT
Hasnaeen Rahman
 
PDF
GIT_In_90_Minutes
vimukthirandika
 
PDF
Improving your workflow with git
Dídac Ríos
 
PPTX
Git n git hub
Jiwon Baek
 
PPT
Git Introduction
Gareth Hall
 
PDF
Git essentials
Otto Kekäläinen
 
PDF
Git tutorial
mobaires
 
PDF
How to Really Get Git
Susan Tan
 
PDF
Introduction to Git
Colin Su
 
PPTX
Git in 10 minutes
Safique Ahmed Faruque
 
PDF
SCM for Android Developers Using Git
Tony Hillerson
 
PDF
Git real slides
Lucas Couto
 
PDF
Introduction to Git
Yan Vugenfirer
 
PDF
An Introduction to Git (even for non-developers)
John Anderson
 
Mastering git - Workflow
Tahsin Abrar
 
Git and Version Control at Atlogys
Atlogys Technical Consulting
 
Git tutorial
Pham Quy (Jack)
 
Git Basic
Luke Luo
 
Git & Github
Aman Lalpuria
 
Mastering GIT
Hasnaeen Rahman
 
GIT_In_90_Minutes
vimukthirandika
 
Improving your workflow with git
Dídac Ríos
 
Git n git hub
Jiwon Baek
 
Git Introduction
Gareth Hall
 
Git essentials
Otto Kekäläinen
 
Git tutorial
mobaires
 
How to Really Get Git
Susan Tan
 
Introduction to Git
Colin Su
 
Git in 10 minutes
Safique Ahmed Faruque
 
SCM for Android Developers Using Git
Tony Hillerson
 
Git real slides
Lucas Couto
 
Introduction to Git
Yan Vugenfirer
 
An Introduction to Git (even for non-developers)
John Anderson
 

Similar to Introduction to git, an efficient distributed version control system (20)

PPT
Fundamentals and basics of Git and commands
DivyanshGupta922023
 
PPTX
Git and GitHub
Priya Nayak
 
PPT
390a gitintro 12au
Nguyen Van Hung
 
PDF
Git for developers
Hacen Dadda
 
PPT
Git
Vijay Kani
 
PPTX
sample.pptx
UshaSuray
 
PDF
Advanced Git Tutorial
Sage Sharp
 
PPTX
Introduction to Git and GitHub
Bioinformatics and Computational Biosciences Branch
 
PPTX
github ppt git ppt on git hub to know ab
infoinnext
 
PPTX
Git_new.pptx
BruceLee275640
 
PPTX
Get going with_git_ppt
Miraz Al-Mamun
 
PPTX
GIT.pptx
Soumen Debgupta
 
PDF
Git 入门 与 实践
Terry Wang
 
PDF
Git 入门与实践
Terry Wang
 
PDF
Git
Terry Wang
 
PPT
Effective Git with Eclipse
Chris Aniszczyk
 
PDF
git.ppt.pdf
Roniel Lopez Alvarez
 
ZIP
Beginner's Guide to Version Control with Git
Robert Lee-Cann
 
KEY
Let's Git this Party Started: An Introduction to Git and GitHub
Kim Moir
 
Fundamentals and basics of Git and commands
DivyanshGupta922023
 
Git and GitHub
Priya Nayak
 
390a gitintro 12au
Nguyen Van Hung
 
Git for developers
Hacen Dadda
 
sample.pptx
UshaSuray
 
Advanced Git Tutorial
Sage Sharp
 
github ppt git ppt on git hub to know ab
infoinnext
 
Git_new.pptx
BruceLee275640
 
Get going with_git_ppt
Miraz Al-Mamun
 
GIT.pptx
Soumen Debgupta
 
Git 入门 与 实践
Terry Wang
 
Git 入门与实践
Terry Wang
 
Effective Git with Eclipse
Chris Aniszczyk
 
Beginner's Guide to Version Control with Git
Robert Lee-Cann
 
Let's Git this Party Started: An Introduction to Git and GitHub
Kim Moir
 
Ad

More from AlbanLevy (7)

PDF
Envelope coding in the cochlear nucleus: a data mining approach
AlbanLevy
 
PDF
Cracking the neural code
AlbanLevy
 
PDF
Object Oriented Programming in Matlab
AlbanLevy
 
PDF
Data mining with Weka
AlbanLevy
 
PDF
Matlab for a computational PhD
AlbanLevy
 
PDF
Mathematics for neuroscience - a gentle introduction (in French)
AlbanLevy
 
PDF
Storytelling for research software engineers
AlbanLevy
 
Envelope coding in the cochlear nucleus: a data mining approach
AlbanLevy
 
Cracking the neural code
AlbanLevy
 
Object Oriented Programming in Matlab
AlbanLevy
 
Data mining with Weka
AlbanLevy
 
Matlab for a computational PhD
AlbanLevy
 
Mathematics for neuroscience - a gentle introduction (in French)
AlbanLevy
 
Storytelling for research software engineers
AlbanLevy
 
Ad

Recently uploaded (20)

PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 

Introduction to git, an efficient distributed version control system

  • 1. I H S C Introduction to git: An Efficient Distributed Version Control System [email protected] 18th November 2016
  • 2. Introduction git alone git + server Complementary 1
  • 3. Version Control System Stand-alone application managing changes to documents. Bad Example Good Example, using Git branches 2
  • 4. Version Control System Stand-alone application managing changes to documents. Bad Example thesis.pdf thesis_V1.pdf thesis_V2.pdf thesis_V2bis.pdf thesis_V3_withChris_comments.pdf thesis_V4_withChris_commentsV2.pdf Good Example, using Git branches 2
  • 5. Version Control System Stand-alone application managing changes to documents. Bad Example thesis.pdf thesis_V1.pdf thesis_V2.pdf thesis_V2bis.pdf thesis_V3_withChris_comments.pdf thesis_V4_withChris_commentsV2.pdf Good Example, using Git branches thesis.pdf 2
  • 6. Think of nodes as points in time pointing towards their past: 3
  • 7. Think of nodes as points in time pointing towards their past: What is the history of H? 3
  • 8. Think of nodes as points in time pointing towards their past: What is the history of H? H G F B A 3
  • 9. Think of nodes as points in time pointing towards their past: What is the history of H? H G F B A What is the history of E? 3
  • 10. Think of nodes as points in time pointing towards their past: What is the history of H? H G F B A What is the history of E? E D C B A 3
  • 11. Think of nodes as points in time pointing towards their past: What is the history of H? H G F B A What is the history of E? E D C B A What is the history of K? K J I C B A 3
  • 12. Think of nodes as points in time pointing towards their past: What is the history of H? H G F B A What is the history of E? E D C B A What is the history of K? K J I C B A Each branch is a parallel universe. Each branch shares a part of its past with other branches. 3
  • 13. Think of nodes as points in time pointing towards their past: ← nobrexit ← master ← notrump What is the history of H? H G F B A What is the history of E? E D C B A What is the history of K? K J I C B A Each branch is a parallel universe. Each branch shares a part of its past with other branches. → Depending on where you start, some nodes are unreachable. 3
  • 14. 4
  • 15. Git Tools This seminar is about • init • add • branch • status • commit • merge • clone • push • fetch • pull and behind the scene 5
  • 16. Git Tools This seminar is about • init • add • branch • status • commit • merge    to use git alone • clone • push • fetch • pull and behind the scene 5
  • 17. Git Tools This seminar is about • init • add • branch • status • commit • merge • clone • push • fetch • pull    to use git with a server and behind the scene 5
  • 18. Git Tools This seminar is about • init • add • branch • status • commit • merge • clone • push • fetch • pull and behind the scene    to use git on collaborative projects 5
  • 19. Goals • Create new repo(sitory) • Put some code in remote server • Use GitX to check things work as wanted • Wait for collaborators to change the code • Get the changes on my computer • Experiment in a different branch 6
  • 20. Requirements Terminal/Command Prompt Depending on you Operating System Command-Line Basics • pwd Current working directory • cd <d> Change directory to <d> • ls <d> List out directory <d> • cp <a> <fd> Copy <a> to <fd> • echo <s> Display input <s> • cat <f> Display file <f> • rm <f> Remove <f> Text Editor vim, nano, emacs... 7
  • 21. Introduction git alone git + server Complementary 8
  • 22. $HOME alone Most of your workflow with git is on your local computer 9
  • 23. git States git has 3 main states files can reside in: • Commited: data is safely stored in your local database • Modified: file has been changed but not committed yet • Staged: file marked as going into next commit snapshot 10
  • 24. git States git has 3 main states files can reside in: • Commited: data is safely stored in your local database • Modified: file has been changed but not committed yet • Staged: file marked as going into next commit snapshot leading to three main sections of a Git project: • git directory, • working directory, • staging area. Pro Git, Chacon&Straub 10
  • 25. git States Pro Git, Chacon&Straub At any moment, a file is in one of these states: Untracked Modified: Staged: Commited: : > a echo a >> lol git add lol git commit -m "l" git status git status git status git status 11
  • 26. A Basic Workflow Edit files vim <file> Stage the changes git add <file> Review your changes git status / git diff Commit the changes git commit -m "<msg>" Edit files ..... Stage ............ Commit ........ 12
  • 27. Commit → Safety Commited data (stored in the repo) is fairly hard to remove. It makes it hard to lose your work once it’s commited. Commit: • Create snapshot of whole repo + history • Add a diff in database • Generate SHA-1 40 bytes hexadecimal string of it $ git log --pretty=oneline 71c1395efd640a476ab78d62c3da13caa9388065 Work git seminar 96b4cfa3158141bf0e0321780876358114e7c229 Testing gitignore 0c4efa32205c8f2faef415dd7cc0f484bfdab1e2 some changes 7d8bfb1b04fefc641e9556933ee7a45b956086e6 rm DS_Stores 76b68b6f2fb82f3cfbc1cc52b31f217267d85ce9 Commit f683feade8ba48bda78fabc20ce4aceba4563470 First commit 13
  • 28. Branches Branching means you diverge from the main line of development and continue to do work without messing with that main line. Git’s killer feature is its branching model 14
  • 30. Working with Branches Creating a branch... takes 41 bytes, 15
  • 31. Working with Branches Creating a branch... takes 41 bytes, is virtually instantaneous, 15
  • 32. Working with Branches Creating a branch... takes 41 bytes, is virtually instantaneous, creates a reference easy to come back to. 15
  • 33. Working with Branches Creating a branch... takes 41 bytes, is virtually instantaneous, creates a reference easy to come back to. A branch is simply a pointer to a commit. Pro Git, Chacon&Straub 15
  • 34. Working with Branches Creating a branch... takes 41 bytes, is virtually instantaneous, creates a reference easy to come back to. A branch is simply a pointer to a commit. Pro Git, Chacon&Straub Creating a branch is like saving the game before battling a boss. Plato, about Git, 429 BC 15
  • 35. References make commits reachable Attaching your modifications to a branch enables you to go back to them by following pointers. 16
  • 36. References make commits reachable Attaching your modifications to a branch enables you to go back to them by following pointers. ← featureX ← master ← issue539 16
  • 37. Working with Branches git branch newB to create a new branch, git checkout coolFeature to move to coolFeature branch, git checkout -b newB locally create a new branch & switch to. To check in which branch you are: git status / git branch 17
  • 38. Experimenting in New Branch By default, original branch is called master. HEAD is a symbolic reference to the branch you are currently in. 18
  • 39. Experimenting in New Branch By default, original branch is called master. HEAD is a symbolic reference to the branch you are currently in. Workflow: • Create new branch, git branch nB 18
  • 40. Experimenting in New Branch By default, original branch is called master. HEAD is a symbolic reference to the branch you are currently in. Workflow: • Create new branch, git branch nB • Checkout to this branch, git checkout -b nB 18
  • 41. Experimenting in New Branch By default, original branch is called master. HEAD is a symbolic reference to the branch you are currently in. Workflow: • Create new branch, git branch nB • Checkout to this branch, git checkout -b nB • Develop new features in it, git add/commit/add/commit.. 18
  • 42. Experimenting in New Branch By default, original branch is called master. HEAD is a symbolic reference to the branch you are currently in. Workflow: • Create new branch, git branch nB • Checkout to this branch, git checkout -b nB • Develop new features in it, git add/commit/add/commit.. • Merge, git checkout B; git merge nB 18
  • 43. Experimenting in New Branch By default, original branch is called master. HEAD is a symbolic reference to the branch you are currently in. Workflow: • Create new branch, git branch nB • Checkout to this branch, git checkout -b nB • Develop new features in it, git add/commit/add/commit.. • Merge, git checkout B; git merge nB • Delete branch. git branch -d nB 18
  • 44. Merging Git has two strategies for merging, depending on reachability: Reachable: Fast-forward merging, just moves a pointer. 19
  • 45. Merging Git has two strategies for merging, depending on reachability: Reachable: Fast-forward merging, just moves a pointer. Pro Git, Chacon&Straub 19
  • 46. Merging Git has two strategies for merging, depending on reachability: Reachable: Fast-forward merging, just moves a pointer. Unreachable: Recursive merging, commit with two parents. Pro Git, Chacon&Straub 19
  • 47. Merging Git has two strategies for merging, depending on reachability: Reachable: Fast-forward merging, just moves a pointer. Unreachable: Recursive merging, commit with two parents. Pro Git, Chacon&Straub 19
  • 48. Hands-on: just on your machine Five minutes to use on your computer: • git init Initialise a git skeleton inside chosen folder • git add <F> Start tracking or stage changes <F> • git commit -m "<M>" Commit changes with msg <M> • git status Get general state information • git diff Get patch stage information • git branch <B> Create a new branch • git checkout <B> Switch to branch <B> • git merge <B> Merge branch <B> with current branch 20
  • 49. Introduction git alone git + server Complementary 21
  • 50. Remote Server Download git repo located at given url into new directory git clone <url> Fetch the current version of current repo on remote server git fetch Push your current git repo to remote server git push Check your remote servers: git remote -v 22
  • 51. Quicker than fetching + merging git pull = ‘git fetch‘ + ‘git merge FETCH_HEAD‘ 23
  • 52. Hands-on: Let’s Collaborate • create an empty repo "ourRepo" using a web browser • git clone to local computer • git add a README.md file with information for users • git add to start tracking files, git commit, git push • Users git pull (first clone), apply some changes and git push (pull request since you don’t have the authorisations) • Do pull request 24
  • 53. Hands-on: Let’s Collaborate • create an empty repo "ourRepo" using a web browser cd path/to/myRepos • git clone to local computer git clone [email protected]:allevity/ourRepo.git • git add a README.md file with information for users a="Clone project and follow README instructions" echo $a > ourRepo/letsPlayAGame cp Seminars/git/introGit.pdf ourRepo/introGit.pdf • git add to start tracking files, git commit, git push git add -A; git commit -m "First commit"; git push • Users git pull (first clone), apply some changes and git push (pull request since you don’t have the authorisations) Fork to your account yourName git clone [email protected]:yourName/ourRepo.git git add -A; git commit -m "your msg"; git push • Do pull request 24
  • 54. Checking Commits since 3y 4m 5h 6s ago Literally... git log --since="3 years 4 months 5 hours 6 seconds ago" Check what your collaborators have commited during your sleep: git log --since="15 hours ago" 25
  • 55. Tagging Tags are pointers towards a fixed commit: git tag -a V1.4 -m "Thesis_V1.4 with Chris comments" To list all tags or look at a specific one: git tag git show V1.4 26
  • 56. How does git work? ... 27
  • 58. Introduction git alone git + server Complementary 29
  • 59. Workflows in Centralised VS Distributed Systems Centralised Integration Manager Benevolent Dictator Pro Git, Chacon&Straub Centralised server special, merges difficult, communication slow & necessary. Distributed server is just copy, easy to merge, most work is local. https://www.youtube.com/watch?v=_yQlKEq-Ueg 30
  • 60. Which Web-based Git Repository Hosting Service? Short answer: • GitHub is the biggest player, but no private repo for free* • Bitbucket has free private repos, but visualisation and traffic info missing * Just tried https://education.github.com/ to get free repos. They say it might take a few weeks... 31
  • 61. gitignore A .gitignore file forces git to not track some files or folders, according to regular expression patterns. Example $ cat .gitignore # Don’t keep this sub repository mySubRepo/* # Data too big to fit on server data/* # LaTeX minitoc, generated when executing LaTex code *.mlt *.mtc[0-9]* # No .txt in log/, except thisisimportant.txt log/**/*.txt !log/thisisimportant.txt Use templates: https://github.com/github/gitignore 32
  • 62. Raising & Solving Issues When you spot a problem in Git-based project, raise an issue. 33
  • 63. Dealing with Conflicts When you change a line of code that a collaborator changed before you, git will refuse your push. Hence, always git pull before starting to work. 34
  • 64. Deleting Branches Safe (checks commit remains reachable): git branch -d myBranch Unsafe (may make commits unreachable): git branch -D myBranch 35
  • 65. Deleting Branches Even if a commit is not reachable, knowing its SHA-1 is enough to get it back in our graph. To create a branch pointing to a commit that was not reachable: git branch -D myBranch # Deleted branch myBranch (was e11cd74). git branch myBranchBack e11cd74 36
  • 66. Changing the past It is considered rude to change the history of a public project. But if you need to, git rebase moves commits in the graph. 37
  • 67. A Word About Security Thanks to its checksum-based implementation, Git is secure: • Delete project, only keep its last commit SHA-1 (41 bytes), • Download a copy from a completely insecure source, • Check its SHA-1 is the same as yours. You can trust this is exactly your project: no corruption on it. 38
  • 68. A Word About Security Thanks to its checksum-based implementation, Git is secure: • Delete project, only keep its last commit SHA-1 (41 bytes), • Download a copy from a completely insecure source, • Check its SHA-1 is the same as yours. You can trust this is exactly your project: no corruption on it. Maybe your projects aren’t that important. My projects, they’re important. There’s a reason I care. Linus Torvalds, speaking to Google about Git, 2007. 38
  • 69. Some useful commands Tracking Files: some options • Add all files git add -a • Add a given file/folder git add myFile src/myFolder/ • Choose what changes to add manually (‘y’ or ‘n’ changes) git add -p 39
  • 70. Some useful commands Skip staging area, staging all files already tracked: git commit -am "<M>" Unstage a file (reset without option only changes staging area): git add lol git reset HEAD lol Change a commit: git commit -m ’initial commit’ git add forgotten_file git commit --amend 40
  • 71. Some useful commands Remove changes done to a tracked & modified & unstaged file: echo "new change" >> lol git checkout -- lol # SOME DATA IS LOST! Check what you commit: git diff git status -s with two columns: staging area & working area 41
  • 72. Some useful commands Search in repo: git grep ’some string’ Show list of commits: git log # All commits git log -S ’some stuff’ # Search string git log --author ’supervisor’ # Did he work? git log --oneline --abbrev-commit --all --graph --decorate Make aliases: git config --global alias.lol "log --oneline --graph --decorate" 42
  • 73. Some useful tools GitX to visualise your git tree 43
  • 74. Summary git init git checkout git clone git merge git add git push git status git fetch git commit git pull git branch git log 44
  • 75. Remerciements Thank you for your attention Linus for awesome work Scott Chacon for wonderful Git material GitHub & Bitbucket for free repos 45
  • 76. References Online Hands-on https://try.github.io Introduction to Git with Scott Chacon of GitHub, 2011: https://www.youtube.com/watch?v=ZDR433b0HJY Linus Torvald, Google HQ, 2007: https://www.youtube.com/watch?v=4XpnKHJAok8 A gentle intro: http://think-like-a-git.net/ Scott Chacon’s free e-book https://git-scm.com/book/en/v2 46