Version Control System

WORKSHOP #2
Introduction to
Version Control System
with Git
Workshop #2: VCS & Git, Ristek 2015
Some introduction...
Bobby Priambodo
@bobbypriambodo
Ketua SIG Web Development
Ristek 2015
Workshop #2: VCS & Git, Ristek 2015
What is version control and why
should you care?
Workshop #2: VCS & Git, Ristek 2015
Seems familiar?
Documents/
Tugas2.pdf
Tugas2fix.pdf
Tugas2-revisi.pdf
Tugas2-revisi-fix.pdf
Tugas2-revisi-fix-final.pdf
Tugas2-revisi-fix-finalbgt.pdf
Workshop #2: VCS & Git, Ristek 2015
Seems familiar?
Documents/
Tugas2.pdf
Tugas2fix.pdf
Tugas2-revisi.pdf
Tugas2-revisi-fix.pdf
Tugas2-revisi-fix-final.pdf
Tugas2-revisi-fix-finalbgt.pdf
This is version control!
Although unefficient...
Workshop #2: VCS & Git, Ristek 2015
Version Control System (VCS)
Keeps track of “versions” of your files
(intelligently!)
Workshop #2: VCS & Git, Ristek 2015
Having a database store the versions
Workshop #2: VCS & Git, Ristek 2015
Advantages of VCS
●
Review history of files
●
Compare versions
●
Revert back to a specific version
●
Experiments without worry!
Workshop #2: VCS & Git, Ristek 2015
Git
VCS developed by the folks at Linux
Workshop #2: VCS & Git, Ristek 2015
Centralized vs. Distributed
Workshop #2: VCS & Git, Ristek 2015
Three States of your Files
Working Directory
Directory
Working
Modify
Repository
Commit
Add
Staging Area (index)
Workshop #2: VCS & Git, Ristek 2015
Let's get our hands dirty!
Launch your terminals!
Workshop #2: VCS & Git, Ristek 2015
Questions are encouraged!
Don't let it bother only yourself :p
Workshop #2: VCS & Git, Ristek 2015
Make a directory
$ cd ~
$ mkdir ristekgit
$ cd ristekgit
Workshop #2: VCS & Git, Ristek 2015
Creating a local repository
$ git init
Workshop #2: VCS & Git, Ristek 2015
Some configurations
$ git config --global user.name “Your Name”
$ git config --global user.email [email protected]
Workshop #2: VCS & Git, Ristek 2015
Creating a file
$ touch file.txt
$ echo “Hello, Git!” > file.txt
Workshop #2: VCS & Git, Ristek 2015
Checking the status of your files
$ git status
see “untracked”?
Workshop #2: VCS & Git, Ristek 2015
Adding to staging area
$ git add file.txt
// or $ git add .
Check again with git status!
Workshop #2: VCS & Git, Ristek 2015
Commit!
$ git commit -m “My first awesome commit!”
git status again!
Workshop #2: VCS & Git, Ristek 2015
Make changes
$ cat file.txt
$ echo “My name is [Name].” >> file.txt
Check git status! The file is now 'modified'
Workshop #2: VCS & Git, Ristek 2015
Stage and commit
$ git add .
$ git status
$ git commit -m “Added my name”
$ git status
Workshop #2: VCS & Git, Ristek 2015
See commit history
$ git log
Workshop #2: VCS & Git, Ristek 2015
Compare two versions
$ git diff [checksum]
// compare with current version
$ git diff [checksum1] [checksum2]
// compare two different versions
Workshop #2: VCS & Git, Ristek 2015
Undo commits!
$ git reset [checksum]
// or 'git reset HEAD~1' to revert to last commit
$ git status
Workshop #2: VCS & Git, Ristek 2015
git reset --hard
Workshop #2: VCS & Git, Ristek 2015
Make other changes
$ echo "I forgot to put this line" >> file.txt
Stage and commit!
Check git log; the commit tree is now updated
Workshop #2: VCS & Git, Ristek 2015
Peek at another version
$ cat file.txt
$ git checkout [checksum]
$ cat file.txt
$ git checkout master
Workshop #2: VCS & Git, Ristek 2015
Basic git workflow
●
Make changes
●
Stage
●
Commit
Repeat!
Workshop #2: VCS & Git, Ristek 2015
Advices
●
Commit early, commit often!
–
●
Commits = save points
Use descriptive commit messages
–
Do: “Hapus image dari header”, “Mindahin db.php ke
direktori config”
–
Don't: “Ubah banyak banget”, “Yeay jalan!”
Workshop #2: VCS & Git, Ristek 2015
Working together!
Workshop #2: VCS & Git, Ristek 2015
Sign up for GitHub!
@ github.com
Workshop #2: VCS & Git, Ristek 2015
Four States of your Files
Modify
Pull
Repository
Working Directory
Directory
Working
Commit
Push
Workshop #2: VCS & Git, Ristek 2015
Add
Staging Area (index)
Player 1: Create GitHub repo
+ New Repository
Name: ristek-git
Create Repo!
Workshop #2: VCS & Git, Ristek 2015
Player 1: Push existing repo!
$ git remote add origin
https://github.com/[username]/ristek-git.git
$ git push -u origin master
Workshop #2: VCS & Git, Ristek 2015
Player 2: Clone a repository
$ cd ..
$ git clone [repo-url]
$ cd [repo-name]
Workshop #2: VCS & Git, Ristek 2015
Player 1: Push more changes
$ touch anotherfile.txt
Stage and commit
$ git push origin master
Workshop #2: VCS & Git, Ristek 2015
Player 2: Pull changes
$ git fetch origin master
$ git merge origin/master
// one line: git pull origin master
Workshop #2: VCS & Git, Ristek 2015
Let's try conflicts!
Player 1:
Player 2:
$ echo “From player one” >>
file.txt
$ echo “From player two” >>
file.txt
$ cat file.txt
$ cat file.txt
$ # add, commit
$ # add, commit
$ git push
$ git pull -------> conflicts!
Workshop #2: VCS & Git, Ristek 2015
Player 2: Resolving conflicts
Open file.txt with a text editor
Delete the HEAD and checksum
Add, commit
Workshop #2: VCS & Git, Ristek 2015
Branching and Merging
One branch is not enough
Workshop #2: VCS & Git, Ristek 2015
Player 2: Create new branch
$ git branch player-two
$ git checkout player-two
// one line: git checkout -b player-two
Workshop #2: VCS & Git, Ristek 2015
Player 2: Modify file and push
$ echo “Change from new branch” >> file.txt
Add, commit
$ git push -u origin player-two
Workshop #2: VCS & Git, Ristek 2015
Player 1: Fetch changes
$ git fetch
Workshop #2: VCS & Git, Ristek 2015
Player 1: Review changes
$ git checkout player-two
$ cat file.txt
$ git checkout master
Workshop #2: VCS & Git, Ristek 2015
Player 1: Merge changes and push
$ git merge player-two
$ cat file.txt --> successfully merged!
$ git status
$ git push
Workshop #2: VCS & Git, Ristek 2015
Review!
●
Basic flow
–
●
Working with remote repos
–
●
Modify, stage, commit
Push, pull, fetch, merge, resolving conflicts
Branching and merging
Workshop #2: VCS & Git, Ristek 2015
Hands-on: done!
You all did great!
Workshop #2: VCS & Git, Ristek 2015
In practice
●
Project Manager (PM) divides feature
implementation
●
Programmer codes in separate feature branches
●
When done, programmer ask for pull request
●
●
PM (or other code manager) merge branches to
master
Master == working code!
Workshop #2: VCS & Git, Ristek 2015
Appendix: Git Flows
No “one-flow-to-rule-them-all”
Workshop #2: VCS & Git, Ristek 2015
Git Flow
Workshop #2: VCS & Git, Ristek 2015
Github Flow
Workshop #2: VCS & Git, Ristek 2015
Reminder: This is just the basics!
●
World of Git (and VCS) is much more broader
●
For example: do you know that Git has GUI?
●
BUT, it is sufficient for survival as a developer :D
Workshop #2: VCS & Git, Ristek 2015
VCS
A tool every developer need to master
Workshop #2: VCS & Git, Ristek 2015
Thank you!
Bobby Priambodo
@bobbypriambodo
Workshop #2: VCS & Git, Ristek 2015