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
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
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
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
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
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 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
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
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
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
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