SlideShare a Scribd company logo
Lecture: Git introduction
Dr. Igor Steinmacher
e-mail: Igor.Steinmacher@nau.edu
Twitter: @igorsteinmacher
INF502
SOFTWARE DEVELOPMENT
METHODOLOGIES
CODE MANAGEMENT/VERSIONING
• Team development
– Code sharing and versioning…
CODE MANAGEMENT/VERSIONING
3
WE WILL FOCUS ON:
4
WHO OFFERS THIS SERVICE
• Your machine! (?)
5
HOW GIT MANAGES FILES OVER TIME
6
GIT - OVERALL
7
GIT LOCAL FLOW - EXAMPLE
8
Commit Branch Merge
F1 F2 F4 F4+
F3 F4*
GIT LOCAL FLOW - EXAMPLE
9
F1 F2 F4
Server
PC
Init
F1 F2 F4
Init
Clone
Push
Class 2
Class 2
Pull
IT’S HANDS ON TIME
• 2 Moments
• Moment 1:
– Local commands: add, commit, branch, merge, conflicts..
• Moment 2
– Interaction with the remote repo: Push, pull
10
KICKING OFF
• git config --global user.name “Igor Steinmacher”
• git config --global user.email “igor.Steinmacher@nau.edu”
• Create a folder / access this folder
• git init
– This folder is a repo
11
For all repos
HANDS ON
• Create a file
• Check the status of the repo
– git status
• Add the file to the index
– git add <filename>
• Check the status
12
HANDS ON
• Our first commit
– git -a -m “Our first commit!!!”
• -a  all files
• -m  will include a commit message
• Check the last commits
– git log
• Check what has been done in the last commit
– git show
13
HANDS ON
• Let’s!
– Change the file
– git status
– git commit …
– git status
– git log
• And this is the basic flow to put your contributions back to the
repo
– add
– commit
– status
– log
– show
14
LET’S BRANCH IT OUT!
• Listing your branches
– git branch
• Create a branch
– git branch <NEW_BRANCH>
• Use another branch
– git checkout <BRANCH_NAME>
• Latest two in one command
– git checkout –b <NEW_BRANCH>
15
WORKING IN A NEW BRANCH
• git checkout -b branchNew
• <Change one existing file here>
– “Hi, my name is Hugh.”
• git commit -a -m “Introducing myself”
• <Check the content of the file>
• git checkout master
• <Check the content of the file>
– What?!?!
16
UPDATING THE MASTER BRANCH
• Usually we branch out for versions, features, bug fixes…
– Later on merging back to master
• How to merge our recently changed file, then?
– In the master branch:
– git merge <other_branch>
• If everything goes smooth… sweet
17
DEALING WITH SMALL CONFLICTS
• Imagine if you change a file in your branch, and someone else
changed the same file
– CONFLICT!!!!
• Can we still merge it?!?!?
– Let’s see:
• Change branch
• Change file
• Commit
• Back to master
• Change the same file
• Commit
• MERGE!
18
USUALLY… FOR THE EASY ONES
Here comes common text before
the area where the conflict happens
and bla bla bla
<<<<<<< HEAD
This is what was in the
master branch
=======
And this…
was in the other branch
>>>>>>> other branch
More text that was common,
and no conflict happened here
19
EXERCISING IT OUT
• Make a new branch called bugfix
• Checkout the bugfix branch with git checkout bugfix
• Create/change a file and commit
• Go back to master with git checkout
• Change/create a file (different from the previous one) and commit
again
• Merge the branch bugfix into master with git merge
20
REBASING IT ALL!!!
• Another way of combining branches
• We can take a set of commits, and copy them in another branch
• Master and our branch are not sync’ed
– Commit made in master
– Commit made in the branch
• From the branch, we can
– git rebase master
– Now the index of the master is “outdated”  HEAD is pointing to
bugfix last commit
• git log --graph --all
21
MOVING FROM HERE TO THERE
• HEAD is the pointer name for the last checked out commit
• We can “detach” the head by “checking out” a specific commit
– git checkout <commit SHA>
• This is not “safe”
– git checkout <branch>
• To get back
• Moving in the commit tree
– Moving one commit at a time with ^
• git checkout HEAD^
• git checkout master^
– Moving a number of times with ~<num>
• git checkout master~3
22
MOVING FROM HERE TO THERE
• We can move a branch!
– git branch -f <BRANCH> HEAD~3
• We can also revert changes
– git reset HEAD~2
• Move the current branch to HEAD - 2 commits position
• Works LOCAL
• git reflog  see previous commits
• git reset <SHA>  SHA of the commit before the reset for
“unresetting”
– git revert HEAD  To reverse changes to send upstream
23
CHERRY-PICKING
• Use when you don’t want to copy ALL commits from a branch to
another
– We can cherry pick those that are of interest
• git cherry-pick <SHA1> <SHA2> <SHA3>
24
DEALING WITH THE REMOTE REPO
25
CREATE A REPO IN GITHUB
26
CLONING TO A LOCAL REPO
• That’s simple! Cloning means bringing all the history to a local
repo
– git clone https://github.com/<owner>/<repo>
• Testing it out
– git clone https://github.com/NAU-OSS/githandson.git
Cloning into 'githandson'...
warning: You appear to have cloned an empty repository.
27
WORKING IN THIS REPO
• Some different commands to deal with remote repo
– git branch –r
– git pull //pulls everything from the remote repo and updates the local
repo
– git fetch //pulls changes from remote repos, but it doesn't integrate any
of this new data into your working files
– git push
• git push <remoteName> <branchName> //push your local changes
to an online repository (git push origin master)
• git push <remoteName> <localBranchName>:<remoteBranchName>
// This pushes the LOCALBRANCHNAME to your REMOTENAME,
but it is renamed to REMOTEBRANCHNAME.
28
USUAL WORKFLOW
• git clone
– branch out to add your changes locally
– your adds/commits
– pull changes to your local repo
– merge your branch back (LOCALLY)
• Resolve any conflict
– push changes back to the remote repo
29
GITHUB WORKFLOW
• Fork + pull-request
– You usually creates a fork for your repo
• “A fork is a copy of a repository. Forking a repository allows you to
freely experiment with changes without affecting the original project”
– Creating a fork
– Then, you usually clone your fork… work, and send a pull request
against the main repo
30
EXAMPLE
31
Server
PC
Pull
master
master
origin/master
master is a local branch
origin/master is a remote branch (a local copy of the branch named
"master" on the remote named "origin")
origin is a remote
EXAMPLE
32
F2
Server
PC
master
master
origin/master
EXAMPLE
33
Server
PC
Pull
master
master
origin/master
EXAMPLE
34
Server
PC
P
u
s
h
master
master
origin/master
Local is outdated
Push
EXAMPLE
35
Server
PC
master
master
origin/master
Push Solution 1:
Fetch +
Rebase +
Push
EXAMPLE
36
Server
PC
master
master
origin/master
Solution 1:
Fetch +
Rebase +
Push
Fetch
EXAMPLE
37
Server
PC
master
master
origin/master
Solution 1:
Fetch +
Rebase +
Push
EXAMPLE
38
Server
PC
master
master
origin/master
Solution 1:
Fetch +
Rebase +
Push
Push
EXAMPLE
39
Server
PC
master
master
origin/master
Push Solution 2:
Pull +
Push
EXAMPLE
40
Server
PC
master
master
origin/master
Solution 2:
Pull +
Push
Pull
EXAMPLE
41
Server
PC
master
master
origin/master
Solution 2:
Pull +
Push
Push
A PULL REQUEST EXAMPLE
• (GitHub) Fork
• (CLI) Clone (the fork)
• (CLI) Commits
• (CLI) Push
• (GitHub) Send Pull Request
– Follow it
– Revise it
– Update it
• Keep your fork up-to-date
– https://help.github.com/articles/syncing-a-fork/
42

More Related Content

ODP
Git tech talk
PPTX
Techoalien git
PPTX
Techoalien git
PPTX
Techoalien git
PDF
Git with the flow
PPTX
Git One Day Training Notes
PPT
PDF
Did you git yet?
Git tech talk
Techoalien git
Techoalien git
Techoalien git
Git with the flow
Git One Day Training Notes
Did you git yet?

Similar to Git first steps (20)

PPTX
Mastering GIT
PDF
Don't fear the rebase
PPT
Git basic
PPTX
Git and Github.pptx
PPTX
Git - Simplified For Testers
PDF
Git tutorial
PPTX
MakingGitWorkForYou
PDF
Git Init (Introduction to Git)
PDF
Git training v10
PDF
Source Code Management with Git
PPTX
Git workshop
PDF
Advanced Git Tutorial
PPTX
Git 101 - An introduction to Version Control using Git
PPTX
Intro to git and git hub
PPTX
PPT
Git-GitHub.ppt Diploma in computer. engineering
PPT
Report about the dangers of git and github on the environment
PPT
Distributed Version control using Git and Github
PPTX
An introduction to Git
PPTX
Git and GitHub Workshop of GDG on Campus UNSTPB
Mastering GIT
Don't fear the rebase
Git basic
Git and Github.pptx
Git - Simplified For Testers
Git tutorial
MakingGitWorkForYou
Git Init (Introduction to Git)
Git training v10
Source Code Management with Git
Git workshop
Advanced Git Tutorial
Git 101 - An introduction to Version Control using Git
Intro to git and git hub
Git-GitHub.ppt Diploma in computer. engineering
Report about the dangers of git and github on the environment
Distributed Version control using Git and Github
An introduction to Git
Git and GitHub Workshop of GDG on Campus UNSTPB
Ad

Recently uploaded (20)

PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PDF
Classroom Observation Tools for Teachers
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
RMMM.pdf make it easy to upload and study
PDF
Trump Administration's workforce development strategy
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Cell Types and Its function , kingdom of life
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Empowerment Technology for Senior High School Guide
Orientation - ARALprogram of Deped to the Parents.pptx
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
Classroom Observation Tools for Teachers
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
IGGE1 Understanding the Self1234567891011
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
RMMM.pdf make it easy to upload and study
Trump Administration's workforce development strategy
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
UNIT III MENTAL HEALTH NURSING ASSESSMENT
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Chinmaya Tiranga quiz Grand Finale.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
History, Philosophy and sociology of education (1).pptx
What if we spent less time fighting change, and more time building what’s rig...
Cell Types and Its function , kingdom of life
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Empowerment Technology for Senior High School Guide
Ad

Git first steps

  • 1. Lecture: Git introduction Dr. Igor Steinmacher e-mail: Igor.Steinmacher@nau.edu Twitter: @igorsteinmacher INF502 SOFTWARE DEVELOPMENT METHODOLOGIES
  • 2. CODE MANAGEMENT/VERSIONING • Team development – Code sharing and versioning…
  • 5. WHO OFFERS THIS SERVICE • Your machine! (?) 5
  • 6. HOW GIT MANAGES FILES OVER TIME 6
  • 8. GIT LOCAL FLOW - EXAMPLE 8 Commit Branch Merge F1 F2 F4 F4+ F3 F4*
  • 9. GIT LOCAL FLOW - EXAMPLE 9 F1 F2 F4 Server PC Init F1 F2 F4 Init Clone Push Class 2 Class 2 Pull
  • 10. IT’S HANDS ON TIME • 2 Moments • Moment 1: – Local commands: add, commit, branch, merge, conflicts.. • Moment 2 – Interaction with the remote repo: Push, pull 10
  • 11. KICKING OFF • git config --global user.name “Igor Steinmacher” • git config --global user.email “igor.Steinmacher@nau.edu” • Create a folder / access this folder • git init – This folder is a repo 11 For all repos
  • 12. HANDS ON • Create a file • Check the status of the repo – git status • Add the file to the index – git add <filename> • Check the status 12
  • 13. HANDS ON • Our first commit – git -a -m “Our first commit!!!” • -a  all files • -m  will include a commit message • Check the last commits – git log • Check what has been done in the last commit – git show 13
  • 14. HANDS ON • Let’s! – Change the file – git status – git commit … – git status – git log • And this is the basic flow to put your contributions back to the repo – add – commit – status – log – show 14
  • 15. LET’S BRANCH IT OUT! • Listing your branches – git branch • Create a branch – git branch <NEW_BRANCH> • Use another branch – git checkout <BRANCH_NAME> • Latest two in one command – git checkout –b <NEW_BRANCH> 15
  • 16. WORKING IN A NEW BRANCH • git checkout -b branchNew • <Change one existing file here> – “Hi, my name is Hugh.” • git commit -a -m “Introducing myself” • <Check the content of the file> • git checkout master • <Check the content of the file> – What?!?! 16
  • 17. UPDATING THE MASTER BRANCH • Usually we branch out for versions, features, bug fixes… – Later on merging back to master • How to merge our recently changed file, then? – In the master branch: – git merge <other_branch> • If everything goes smooth… sweet 17
  • 18. DEALING WITH SMALL CONFLICTS • Imagine if you change a file in your branch, and someone else changed the same file – CONFLICT!!!! • Can we still merge it?!?!? – Let’s see: • Change branch • Change file • Commit • Back to master • Change the same file • Commit • MERGE! 18
  • 19. USUALLY… FOR THE EASY ONES Here comes common text before the area where the conflict happens and bla bla bla <<<<<<< HEAD This is what was in the master branch ======= And this… was in the other branch >>>>>>> other branch More text that was common, and no conflict happened here 19
  • 20. EXERCISING IT OUT • Make a new branch called bugfix • Checkout the bugfix branch with git checkout bugfix • Create/change a file and commit • Go back to master with git checkout • Change/create a file (different from the previous one) and commit again • Merge the branch bugfix into master with git merge 20
  • 21. REBASING IT ALL!!! • Another way of combining branches • We can take a set of commits, and copy them in another branch • Master and our branch are not sync’ed – Commit made in master – Commit made in the branch • From the branch, we can – git rebase master – Now the index of the master is “outdated”  HEAD is pointing to bugfix last commit • git log --graph --all 21
  • 22. MOVING FROM HERE TO THERE • HEAD is the pointer name for the last checked out commit • We can “detach” the head by “checking out” a specific commit – git checkout <commit SHA> • This is not “safe” – git checkout <branch> • To get back • Moving in the commit tree – Moving one commit at a time with ^ • git checkout HEAD^ • git checkout master^ – Moving a number of times with ~<num> • git checkout master~3 22
  • 23. MOVING FROM HERE TO THERE • We can move a branch! – git branch -f <BRANCH> HEAD~3 • We can also revert changes – git reset HEAD~2 • Move the current branch to HEAD - 2 commits position • Works LOCAL • git reflog  see previous commits • git reset <SHA>  SHA of the commit before the reset for “unresetting” – git revert HEAD  To reverse changes to send upstream 23
  • 24. CHERRY-PICKING • Use when you don’t want to copy ALL commits from a branch to another – We can cherry pick those that are of interest • git cherry-pick <SHA1> <SHA2> <SHA3> 24
  • 25. DEALING WITH THE REMOTE REPO 25
  • 26. CREATE A REPO IN GITHUB 26
  • 27. CLONING TO A LOCAL REPO • That’s simple! Cloning means bringing all the history to a local repo – git clone https://github.com/<owner>/<repo> • Testing it out – git clone https://github.com/NAU-OSS/githandson.git Cloning into 'githandson'... warning: You appear to have cloned an empty repository. 27
  • 28. WORKING IN THIS REPO • Some different commands to deal with remote repo – git branch –r – git pull //pulls everything from the remote repo and updates the local repo – git fetch //pulls changes from remote repos, but it doesn't integrate any of this new data into your working files – git push • git push <remoteName> <branchName> //push your local changes to an online repository (git push origin master) • git push <remoteName> <localBranchName>:<remoteBranchName> // This pushes the LOCALBRANCHNAME to your REMOTENAME, but it is renamed to REMOTEBRANCHNAME. 28
  • 29. USUAL WORKFLOW • git clone – branch out to add your changes locally – your adds/commits – pull changes to your local repo – merge your branch back (LOCALLY) • Resolve any conflict – push changes back to the remote repo 29
  • 30. GITHUB WORKFLOW • Fork + pull-request – You usually creates a fork for your repo • “A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project” – Creating a fork – Then, you usually clone your fork… work, and send a pull request against the main repo 30
  • 31. EXAMPLE 31 Server PC Pull master master origin/master master is a local branch origin/master is a remote branch (a local copy of the branch named "master" on the remote named "origin") origin is a remote
  • 42. A PULL REQUEST EXAMPLE • (GitHub) Fork • (CLI) Clone (the fork) • (CLI) Commits • (CLI) Push • (GitHub) Send Pull Request – Follow it – Revise it – Update it • Keep your fork up-to-date – https://help.github.com/articles/syncing-a-fork/ 42