SlideShare a Scribd company logo
1 of 52
USING GIT LIKE A PRO
JESÚS MIGUEL BENITO CALZADA
EDD - 27/10/2018
About me
git config --global user.name “Jesús Miguel Benito Calzada”
git config --global user.email “beni0888@hotmail.com”
@beni0888
https://www.linkedin.com/in/jesusmiguelbenito/
Goals
 Increase our understanding of Git internals
 Get the knowledge to solve cumbersome situations
 Be able to produce a better (cleaner, coherent) Git history
 Have fun!
Git objects
The Git object database store four different
object types:
 Blob
 Tree
 Commit
 Tag
References
 A reference is a “pointer” to a commit.
 It holds the SHA-1 hash of a commit.
 Allows us to refer to commits in a human-friendly way.
 Stored in files under .git/refs directory.
What is a git branch?
What is a git branch?
 It is just a reference to a commit!
 Stored in files under .git/refs/heads (.git/refs/remotes)
 Tags are also references under .git/refs/tags
References – HEAD
 HEAD is a special reference that
points to the commit you have
currently checked out.
 Stored in .git/HEAD file.
 It usually points to another
reference instead of a commit!
 When it points to a commit, we said it
is in a detached state.
Tip
 You can just do as follows:
 After creating a local branch, we need to provide its upstream
before pushing.
> git checkout -b my-branch-with-a-long-name
> git push
fatal: The current branch my-branch-with-a-long-name has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin my-branch-with-a-long-name
> git push origin HEAD
Synchronizing branches
Branches out-of-sync
Merge
 $> git merge master feature
 The most common and easy way of synchronization.
 Produces ugly merge commits.
 Keeps the context of the synchronization.
 Branch history can be difficult to follow.
Merge: This is a mess!!
Rebase
$> git rebase master
Rebase
 Rewrites all feature branch’s commits on top of master branch.
 Much cleaner project history:
 Avoid ugly merge commits.
 Perfectly linear project history.
 Some drawbacks:
 It rewrites the history! Generate new different commits, with different
SHA-1.
 Loses the context of synchronization provided by a merge commit.
 Golden rule: never use it on public branches!!
 Merging vs Rebasing
Lab
 Clone repo: https://github.com/beni0888/gitlikeapro
 Execute a rebase:
 Checkout rebase-no-conflicts
 Rebase it onto rebase-base
 Tip: git log rebase-base…rebase-no-conflicts (show divergent
commits)
 Execute a rebase with conflicts:
 Checkout rebase-with-conflicts
 Rebase it onto rebase-base
 Resolve conflicts and finish rebase
Interactive rebase
git checkout feature
git rebase (--interactive | -i) <commit-ref>
 Allows to edit the history from a commit on.
 It opens an editor showing the list of commits that are going to
be replayed, and allows to perform certain actions on them:
 Join commits: squash, fixup
 Edit commit message: reword
 Edit commit content: edit
 Remove commit: drop
 Reorder commits: just change commit’s order in the editor
Rebase Tip
 Returns the SHA-1 of the base commit of feature branch.
 Useful to interactively rebase the whole branch.
git merge-base master feature
git rebase -i `git merge-base master feature`
Lab
 Execute a interactive rebase:
 Checkout rebase-base branch.
 You can get its origin commit with git merge-base master head
 Do some experimentation: join commits, edit, reorder...
 Be creative!
 Finish the rebase operation and check the results.
Rerere
 Stands for Reuse Recorded Resolution.
 It is kind of machine learning mechanism.
 Learns from your previous decisions on conflicts resolution.
 Apply them automatically reducing the number of manual
interventions.
 Warning! If you do it wrong, it will learn also…
git config --global rerere.enabled 1
Lab
 Run git config rerere.enabled 1
 Checkout rerere-with-conflicts.
 Run a rebase onto rerere-base.
 Solve conflicts and finish the rebase.
 Undo rebase: git reset --hard ORIG_HEAD
 Run the rebase again and see what happens
Sometimes things can go wrong…
Git reset to the rescue!
 Versatile and powerful command for undoing changes.
 Similar to checkout in some aspects.
 Potentially dangerous: destructive operation.
 It allows to:
 Undo changes.
 Unstage files.
git reset
Reset
git reset [--soft|--mixed|--hard] C
Reset main modes
 Soft:
 Keep undone changes on the index.
 Mixed:
 Default mode, keep undone changes in the working tree, but
without staging them (out of the index).
 Hard:
 Totally removes the undone changes, they are not keep neither in
the index, nor in the working tree.
 Use it with care!!
 Great explanation in stackoverflow
Reset vs Revert
 Revert is a safe operation, it takes a commit and generates
a new commit which inverses the specified commit.
 Revert add a new commit to the history, but does not modify
the existent ones.
 Reset rewrites the history to make the branch match the
state of an specific commit.
 Reset vs Revert vs Checkout
git revert <commit-ref>
What really happens when rebasing?
What really happens when rebasing?
 New commits are generated at the tip of the target branch.
 But the original old commits are still available in the repo.
 Although they are unreachable, there is no reference pointing
to them.
Reflog
 Reference logs, or "reflogs", record when the tips of branches
and other references were updated in the local repository.
 Remember: a reference is a pointer to a commit.
 Tracks refs updates up to 90 days by default.
 It allows us to recover from errors by accessing to
“unreachable commits”.
 Subcommands: show, expire and delete.
Reflog subcommands
 Show:
Show the log for a reference, or all reference (--all option).
 Expire:
Prunes older reflog entries, rarely used by end users.
 Delete:
Removes single entries from the log, typically not used by end users
neither.
Reflog - show
 Shows the reflog of the HEAD ref.
git reflog
git reflog show HEAD
f90e666 (HEAD -> master) HEAD@{0}: checkout: moving from branch to master
d92a3b7 (branch) HEAD@{1}: rebase -i (finish): returning to refs/heads/branch
d92a3b7 (branch) HEAD@{2}: rebase -i (start): checkout 19e164a71d87ffa0902c71e92da9f0b61bcc858f
d92a3b7 (branch) HEAD@{3}: rebase -i (finish): returning to refs/heads/branch
d92a3b7 (branch) HEAD@{4}: rebase -i (pick): BRANCH - Add e
da961c4 HEAD@{5}: rebase -i (pick): BRANCH - Add d
5eeef42 HEAD@{6}: rebase -i (fixup): BRANCH - Add c
84d25d0 HEAD@{7}: rebase -i (start): checkout 19e164a71d87ffa0902c71e92da9f0b61bcc858f
Reflog - show
 Shows the reflogs for all references
git reflog show --all
git reflog stash
 Reflog also tracks the stash references.
Undoing a rebase: reflog + reset
1. Look in the reflog for the commit at the tip of the branch
at the moment prior to the rebase.
2. Git reset --hard to that commit
> git reflog
1544114 (HEAD -> feature) HEAD@{0}: rebase finished: returning to refs/heads/feature
1544114 (HEAD -> feature) HEAD@{1}: rebase: Add bar
3b021d4 HEAD@{2}: rebase: Add foo
d92a3b7 (master) HEAD@{3}: rebase: checkout master
bf12c54 HEAD@{4}: commit: Add bar
e99a22b HEAD@{5}: commit: Add foo
…
> git reset --hard HEAD@{4}
HEAD before rebase
Undoing changes: ORIG_HEAD
 ORIG_HEAD is a special reference that stores the previous state
of HEAD. It is set by commands that have possibly dangerous
behaviour (rebase, merge, reset, etc), to be able to easily
revert them.
 Undoing a rebase, reset, merge:
git reset --hard ORIG_HEAD
Looking for errors
Bisect
 Sometimes you discover that a bug has been introduced in
your code base, but you do not know when it happened.
 The bisect command does a binary search through your
commit history to help you identify as quickly as possible
which commit introduced an issue.
 It allows to identify the commit that introduced the issue,
through a large range of commits, in just a few steps.
Bisect
 First, you have to indicate git to start bisecting your history.
 Then you indicate that the current commit is “bad”, or provide
a reference to a previous commit you know is bad.
 And you provide the commit with the last good state known.
 Or just in one command:
> git bisect start
> git bisect bad [<commit-ref>]
> git bisect good v1.2.6-rc
> git bisect start HEAD v1.2.6-rc
Bisect
 After that, git will check out the commit in the middle of
the provided range, so you can test it.
 Git will figure out the number of revisions left to test
before finding the target commit.
Bisecting: 2 revisions left to test after this (roughly 1 step)
[da961c4e9813bbc92c308e4487ef527aa25f3bc7] Foo commit message
Bisect
 After testing the commit, you should inform git whether it is
”good” or “bad” with git bisect (good|bad) commands.
 Git will narrow down the range of remaining revisions, and
will repeat the same process until the commit which
introduced the breakage is found.
 You can skip testing a commit with bisect skip subcommand.
d92a3b789d3e5e7fcee880b106bb6d0d9694f58b is the first bad commit
commit d92a3b789d3e5e7fcee880b106bb6d0d9694f58b
Author: Jesús Miguel Benito Calzada beni0888@hotmail.com
Date: Thu Oct 4 18:49:17 2018 +0200
Added a really cool feature!
Bisect
 You should finish the process with bisect reset command,
to cleanup the bisection state and return to the original
HEAD.
> git bisect reset
Previous HEAD position was d92a3b7... Foo commit message
Switched to branch 'feature'
Lab
 Checkout bisect branch
 File.txt contains several lines with the text “EDD rocks!”, each
one added by a different commit, but one line contains a typo:
“EED” instead of ”EDD”.
 Make use of bisect to identify the commit which introduced the
line with a typo.
 Tip: use the commad grep EED file.txt to check if the commit
is wrong.
Bisect: let’s automate!
 This is a really manual error-prone process, right?
 Bisect provides a run subcommand that allows us to run a
script to determine whether the commit is good or bad.
 The script must exit with 0 if the commit is good, or
whatever code between 1 and 127 (except 125, used to
skip) for a bad commit.
 You can for example run your test suite to check your
commits.
> git bisect start HEAD v2.1.0-rc
> git bisect run vendor/bin/phpunit
> git bisect reset
Lab
 Checkout bisect-run branch.
 Prerequisites:
 You need to have PHP (version >= 5) installed on your computer.
 You need to have composer installed globally, or download its phar from
https://getcomposer.org/download/.
 Run composer install (or ./comporser.phar install).
 The current branch’s test suite has a failing test, and you have to
find the commit that introduce it.
 Make use of bisect run to find the commit which introduced the
error:
 Tip: use vendor/bin/phpunit as script for bisect run.
Oh-my-zsh git plugin
 Oh-my-zsh comes with a really handy git plugin that saves
us to write a lot: https://github.com/robbyrussell/oh-my-
zsh/wiki/Plugin:git
g=git
ga='git add’
gaa='git add --all’
gap='git apply’
gapa='git add --patch’
gau='git add --update’
gb='git branch’
gba='git branch -a’
gbd='git branch -d’
gbda='git branch --no-color --merged | command grep -vE
"^(*|s*(master|develop|dev)s*$)" | command xargs -n 1 git branch -d’
gbl='git blame -b -w’
…
https://goo.gl/forms/mVfvSBHRTmOMSeDG2
Thank you!

More Related Content

What's hot

Introduction To Git For Version Control Architecture And Common Commands Comp...
Introduction To Git For Version Control Architecture And Common Commands Comp...Introduction To Git For Version Control Architecture And Common Commands Comp...
Introduction To Git For Version Control Architecture And Common Commands Comp...SlideTeam
 
My Notes from https://www.codeschool.com/courses/git-real
My Notes from  https://www.codeschool.com/courses/git-realMy Notes from  https://www.codeschool.com/courses/git-real
My Notes from https://www.codeschool.com/courses/git-realEneldo Serrata
 
The Fundamentals of Git
The Fundamentals of GitThe Fundamentals of Git
The Fundamentals of GitDivineOmega
 
How to Really Get Git
How to Really Get GitHow to Really Get Git
How to Really Get GitSusan Tan
 
Introducción a git y GitHub
Introducción a git y GitHubIntroducción a git y GitHub
Introducción a git y GitHubLucas Videla
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptuallyseungzzang Kim
 
GIT in a nutshell
GIT in a nutshellGIT in a nutshell
GIT in a nutshellalignan
 
Version control system
Version control systemVersion control system
Version control systemAndrew Liu
 
Git: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commandsGit: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commandsth507
 
Get Good With Git
Get Good With GitGet Good With Git
Get Good With GitHoffman Lab
 
Git Basics (Professionals)
 Git Basics (Professionals) Git Basics (Professionals)
Git Basics (Professionals)bryanbibat
 
Git: basic to advanced
Git: basic to advancedGit: basic to advanced
Git: basic to advancedYodalee
 

What's hot (20)

Introduction To Git For Version Control Architecture And Common Commands Comp...
Introduction To Git For Version Control Architecture And Common Commands Comp...Introduction To Git For Version Control Architecture And Common Commands Comp...
Introduction To Git For Version Control Architecture And Common Commands Comp...
 
Introduction to Git and GitHub
Introduction to Git and GitHubIntroduction to Git and GitHub
Introduction to Git and GitHub
 
GIT_In_90_Minutes
GIT_In_90_MinutesGIT_In_90_Minutes
GIT_In_90_Minutes
 
Grokking opensource with github
Grokking opensource with githubGrokking opensource with github
Grokking opensource with github
 
Git Basic
Git BasicGit Basic
Git Basic
 
My Notes from https://www.codeschool.com/courses/git-real
My Notes from  https://www.codeschool.com/courses/git-realMy Notes from  https://www.codeschool.com/courses/git-real
My Notes from https://www.codeschool.com/courses/git-real
 
The Fundamentals of Git
The Fundamentals of GitThe Fundamentals of Git
The Fundamentals of Git
 
How to Really Get Git
How to Really Get GitHow to Really Get Git
How to Really Get Git
 
Learn Git Basics
Learn Git BasicsLearn Git Basics
Learn Git Basics
 
Introducción a git y GitHub
Introducción a git y GitHubIntroducción a git y GitHub
Introducción a git y GitHub
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptually
 
GIT in a nutshell
GIT in a nutshellGIT in a nutshell
GIT in a nutshell
 
Version control system
Version control systemVersion control system
Version control system
 
Git: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commandsGit: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commands
 
Git real slides
Git real slidesGit real slides
Git real slides
 
Get Good With Git
Get Good With GitGet Good With Git
Get Good With Git
 
Git github
Git githubGit github
Git github
 
Basic Git
Basic GitBasic Git
Basic Git
 
Git Basics (Professionals)
 Git Basics (Professionals) Git Basics (Professionals)
Git Basics (Professionals)
 
Git: basic to advanced
Git: basic to advancedGit: basic to advanced
Git: basic to advanced
 

Similar to Use Git like a pro - condensed

Similar to Use Git like a pro - condensed (20)

Git tutorial
Git tutorialGit tutorial
Git tutorial
 
Git
GitGit
Git
 
Gitlikeapro 2019
Gitlikeapro 2019Gitlikeapro 2019
Gitlikeapro 2019
 
Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
 
Introduction to Git (part 2)
Introduction to Git (part 2)Introduction to Git (part 2)
Introduction to Git (part 2)
 
Git tutorial
Git tutorialGit tutorial
Git tutorial
 
Git tips
Git tipsGit tips
Git tips
 
Mastering git - Workflow
Mastering git - WorkflowMastering git - Workflow
Mastering git - Workflow
 
Git workshop
Git workshopGit workshop
Git workshop
 
GIT Basics
GIT BasicsGIT Basics
GIT Basics
 
Git essentials
Git essentialsGit essentials
Git essentials
 
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a gitVincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | Workshop
 
Git training
Git trainingGit training
Git training
 
Git - Version Control System
Git - Version Control SystemGit - Version Control System
Git - Version Control System
 
Git training v10
Git training v10Git training v10
Git training v10
 
Introduction to git, a version control system
Introduction to git, a version control systemIntroduction to git, a version control system
Introduction to git, a version control system
 
Git Memento of basic commands
Git Memento of basic commandsGit Memento of basic commands
Git Memento of basic commands
 
Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)
 

Recently uploaded

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 

Recently uploaded (20)

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 

Use Git like a pro - condensed

  • 1. USING GIT LIKE A PRO JESÚS MIGUEL BENITO CALZADA EDD - 27/10/2018
  • 2. About me git config --global user.name “Jesús Miguel Benito Calzada” git config --global user.email “beni0888@hotmail.com” @beni0888 https://www.linkedin.com/in/jesusmiguelbenito/
  • 3. Goals  Increase our understanding of Git internals  Get the knowledge to solve cumbersome situations  Be able to produce a better (cleaner, coherent) Git history  Have fun!
  • 4. Git objects The Git object database store four different object types:  Blob  Tree  Commit  Tag
  • 5. References  A reference is a “pointer” to a commit.  It holds the SHA-1 hash of a commit.  Allows us to refer to commits in a human-friendly way.  Stored in files under .git/refs directory.
  • 6. What is a git branch?
  • 7. What is a git branch?  It is just a reference to a commit!  Stored in files under .git/refs/heads (.git/refs/remotes)  Tags are also references under .git/refs/tags
  • 8. References – HEAD  HEAD is a special reference that points to the commit you have currently checked out.  Stored in .git/HEAD file.  It usually points to another reference instead of a commit!  When it points to a commit, we said it is in a detached state.
  • 9.
  • 10. Tip  You can just do as follows:  After creating a local branch, we need to provide its upstream before pushing. > git checkout -b my-branch-with-a-long-name > git push fatal: The current branch my-branch-with-a-long-name has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin my-branch-with-a-long-name > git push origin HEAD
  • 13. Merge  $> git merge master feature  The most common and easy way of synchronization.  Produces ugly merge commits.  Keeps the context of the synchronization.  Branch history can be difficult to follow.
  • 14. Merge: This is a mess!!
  • 16. Rebase  Rewrites all feature branch’s commits on top of master branch.  Much cleaner project history:  Avoid ugly merge commits.  Perfectly linear project history.  Some drawbacks:  It rewrites the history! Generate new different commits, with different SHA-1.  Loses the context of synchronization provided by a merge commit.  Golden rule: never use it on public branches!!  Merging vs Rebasing
  • 17. Lab  Clone repo: https://github.com/beni0888/gitlikeapro  Execute a rebase:  Checkout rebase-no-conflicts  Rebase it onto rebase-base  Tip: git log rebase-base…rebase-no-conflicts (show divergent commits)  Execute a rebase with conflicts:  Checkout rebase-with-conflicts  Rebase it onto rebase-base  Resolve conflicts and finish rebase
  • 18. Interactive rebase git checkout feature git rebase (--interactive | -i) <commit-ref>  Allows to edit the history from a commit on.  It opens an editor showing the list of commits that are going to be replayed, and allows to perform certain actions on them:  Join commits: squash, fixup  Edit commit message: reword  Edit commit content: edit  Remove commit: drop  Reorder commits: just change commit’s order in the editor
  • 19.
  • 20.
  • 21. Rebase Tip  Returns the SHA-1 of the base commit of feature branch.  Useful to interactively rebase the whole branch. git merge-base master feature git rebase -i `git merge-base master feature`
  • 22. Lab  Execute a interactive rebase:  Checkout rebase-base branch.  You can get its origin commit with git merge-base master head  Do some experimentation: join commits, edit, reorder...  Be creative!  Finish the rebase operation and check the results.
  • 23. Rerere  Stands for Reuse Recorded Resolution.  It is kind of machine learning mechanism.  Learns from your previous decisions on conflicts resolution.  Apply them automatically reducing the number of manual interventions.  Warning! If you do it wrong, it will learn also… git config --global rerere.enabled 1
  • 24. Lab  Run git config rerere.enabled 1  Checkout rerere-with-conflicts.  Run a rebase onto rerere-base.  Solve conflicts and finish the rebase.  Undo rebase: git reset --hard ORIG_HEAD  Run the rebase again and see what happens
  • 25. Sometimes things can go wrong…
  • 26. Git reset to the rescue!  Versatile and powerful command for undoing changes.  Similar to checkout in some aspects.  Potentially dangerous: destructive operation.  It allows to:  Undo changes.  Unstage files. git reset
  • 28. Reset main modes  Soft:  Keep undone changes on the index.  Mixed:  Default mode, keep undone changes in the working tree, but without staging them (out of the index).  Hard:  Totally removes the undone changes, they are not keep neither in the index, nor in the working tree.  Use it with care!!  Great explanation in stackoverflow
  • 29. Reset vs Revert  Revert is a safe operation, it takes a commit and generates a new commit which inverses the specified commit.  Revert add a new commit to the history, but does not modify the existent ones.  Reset rewrites the history to make the branch match the state of an specific commit.  Reset vs Revert vs Checkout git revert <commit-ref>
  • 30. What really happens when rebasing?
  • 31. What really happens when rebasing?  New commits are generated at the tip of the target branch.  But the original old commits are still available in the repo.  Although they are unreachable, there is no reference pointing to them.
  • 32. Reflog  Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.  Remember: a reference is a pointer to a commit.  Tracks refs updates up to 90 days by default.  It allows us to recover from errors by accessing to “unreachable commits”.  Subcommands: show, expire and delete.
  • 33. Reflog subcommands  Show: Show the log for a reference, or all reference (--all option).  Expire: Prunes older reflog entries, rarely used by end users.  Delete: Removes single entries from the log, typically not used by end users neither.
  • 34. Reflog - show  Shows the reflog of the HEAD ref. git reflog git reflog show HEAD f90e666 (HEAD -> master) HEAD@{0}: checkout: moving from branch to master d92a3b7 (branch) HEAD@{1}: rebase -i (finish): returning to refs/heads/branch d92a3b7 (branch) HEAD@{2}: rebase -i (start): checkout 19e164a71d87ffa0902c71e92da9f0b61bcc858f d92a3b7 (branch) HEAD@{3}: rebase -i (finish): returning to refs/heads/branch d92a3b7 (branch) HEAD@{4}: rebase -i (pick): BRANCH - Add e da961c4 HEAD@{5}: rebase -i (pick): BRANCH - Add d 5eeef42 HEAD@{6}: rebase -i (fixup): BRANCH - Add c 84d25d0 HEAD@{7}: rebase -i (start): checkout 19e164a71d87ffa0902c71e92da9f0b61bcc858f
  • 35. Reflog - show  Shows the reflogs for all references git reflog show --all git reflog stash  Reflog also tracks the stash references.
  • 36. Undoing a rebase: reflog + reset 1. Look in the reflog for the commit at the tip of the branch at the moment prior to the rebase. 2. Git reset --hard to that commit > git reflog 1544114 (HEAD -> feature) HEAD@{0}: rebase finished: returning to refs/heads/feature 1544114 (HEAD -> feature) HEAD@{1}: rebase: Add bar 3b021d4 HEAD@{2}: rebase: Add foo d92a3b7 (master) HEAD@{3}: rebase: checkout master bf12c54 HEAD@{4}: commit: Add bar e99a22b HEAD@{5}: commit: Add foo … > git reset --hard HEAD@{4} HEAD before rebase
  • 37.
  • 38. Undoing changes: ORIG_HEAD  ORIG_HEAD is a special reference that stores the previous state of HEAD. It is set by commands that have possibly dangerous behaviour (rebase, merge, reset, etc), to be able to easily revert them.  Undoing a rebase, reset, merge: git reset --hard ORIG_HEAD
  • 40. Bisect  Sometimes you discover that a bug has been introduced in your code base, but you do not know when it happened.  The bisect command does a binary search through your commit history to help you identify as quickly as possible which commit introduced an issue.  It allows to identify the commit that introduced the issue, through a large range of commits, in just a few steps.
  • 41. Bisect  First, you have to indicate git to start bisecting your history.  Then you indicate that the current commit is “bad”, or provide a reference to a previous commit you know is bad.  And you provide the commit with the last good state known.  Or just in one command: > git bisect start > git bisect bad [<commit-ref>] > git bisect good v1.2.6-rc > git bisect start HEAD v1.2.6-rc
  • 42. Bisect  After that, git will check out the commit in the middle of the provided range, so you can test it.  Git will figure out the number of revisions left to test before finding the target commit. Bisecting: 2 revisions left to test after this (roughly 1 step) [da961c4e9813bbc92c308e4487ef527aa25f3bc7] Foo commit message
  • 43. Bisect  After testing the commit, you should inform git whether it is ”good” or “bad” with git bisect (good|bad) commands.  Git will narrow down the range of remaining revisions, and will repeat the same process until the commit which introduced the breakage is found.  You can skip testing a commit with bisect skip subcommand. d92a3b789d3e5e7fcee880b106bb6d0d9694f58b is the first bad commit commit d92a3b789d3e5e7fcee880b106bb6d0d9694f58b Author: Jesús Miguel Benito Calzada beni0888@hotmail.com Date: Thu Oct 4 18:49:17 2018 +0200 Added a really cool feature!
  • 44. Bisect  You should finish the process with bisect reset command, to cleanup the bisection state and return to the original HEAD. > git bisect reset Previous HEAD position was d92a3b7... Foo commit message Switched to branch 'feature'
  • 45. Lab  Checkout bisect branch  File.txt contains several lines with the text “EDD rocks!”, each one added by a different commit, but one line contains a typo: “EED” instead of ”EDD”.  Make use of bisect to identify the commit which introduced the line with a typo.  Tip: use the commad grep EED file.txt to check if the commit is wrong.
  • 46. Bisect: let’s automate!  This is a really manual error-prone process, right?  Bisect provides a run subcommand that allows us to run a script to determine whether the commit is good or bad.  The script must exit with 0 if the commit is good, or whatever code between 1 and 127 (except 125, used to skip) for a bad commit.  You can for example run your test suite to check your commits. > git bisect start HEAD v2.1.0-rc > git bisect run vendor/bin/phpunit > git bisect reset
  • 47. Lab  Checkout bisect-run branch.  Prerequisites:  You need to have PHP (version >= 5) installed on your computer.  You need to have composer installed globally, or download its phar from https://getcomposer.org/download/.  Run composer install (or ./comporser.phar install).  The current branch’s test suite has a failing test, and you have to find the commit that introduce it.  Make use of bisect run to find the commit which introduced the error:  Tip: use vendor/bin/phpunit as script for bisect run.
  • 48.
  • 49. Oh-my-zsh git plugin  Oh-my-zsh comes with a really handy git plugin that saves us to write a lot: https://github.com/robbyrussell/oh-my- zsh/wiki/Plugin:git g=git ga='git add’ gaa='git add --all’ gap='git apply’ gapa='git add --patch’ gau='git add --update’ gb='git branch’ gba='git branch -a’ gbd='git branch -d’ gbda='git branch --no-color --merged | command grep -vE "^(*|s*(master|develop|dev)s*$)" | command xargs -n 1 git branch -d’ gbl='git blame -b -w’ …
  • 50.

Editor's Notes

  1. You can ask the students to: Squash all “add method” commits Remove “intentionally add a failing commit” Reorder commits: move “add instructions to readme” to the tip of the branch
  2. The main difference between reset and checkout is that checkout only operates on the HEAD ref, it doesn’t change the branch’s tip, while reset operates on both HEAD and branch references.