SlideShare a Scribd company logo
1 of 44
• Wifi: RAV-TRAINING
• Password: Solve Challenges
• http://xkcd.com/1597/
• Hover text: If that doesn’t fix it, git.txt
contains the phone number of a
friend of mine who understands git.
Just wait through a few minutes of
“It’s really pretty simple, just think of
branches as…” and eventually you’ll
learn the commands that will fix
everything.
• Slides + Demo Code:
• https://github.com/Kwen/sfdevs
Making Git Work For You
Kwen Peterson
Senior Software Developer @ Raven
kwen.peterson@gmail.com
@kwenarik
"You're already in the top tier of developers just
by showing up here tonight. I don't know how
talented you are, how much experience you have,
but you showed up. You're putting yourself out
there because you care to improve. Thanks for
caring.“ – Scott Hanselman
Rebase vs. Merge
• Merging brings two lines of development together while
preserving the ancestry of each commit history.
• In contrast, rebasing unifies the lines of development by
re-writing changes from the source branch so that they
appear as children of the destination branch – effectively
pretending that those commits were written on top of the
destination branch all along.
– Rebase requires the commits on the source branch to be re-
written, which changes their content and their SHAS.
The merge DNA helix vs. rebase OCD
Commit graph (merge)
Commit graph (pure rebase with ff)
Ideal (no fast-forward)
Git merge --no-ff <feature>
Merge
• Pros
– Simpler to use and understand
– The commits on the source branch remain separate from
other branch commits, provided you don’t perform a fast-
forward merge. (This separation can be useful in the case
of feature branches, where you might want to take a
feature and merge it into another branch later.)
– Existing commits on the source branch are unchanged and
remain valid; it doesn’t matter if they’ve been shared.
• Cons
– If the need to merge arises simply because multiple people
are working on the same branch in parallel, the merges
don’t serve any useful historical purpose & create clutter.
Master is the YELLOW commits
Rebase
• Pros
– Simplifies your history.
– Is the most intuitive and clutter-free way to combine
commits from multiple developers in a shared branch.
• Cons
– Slightly more complex, conflicts require resolution on
a per commit basis.
– Rewriting of history has ramifications if you’ve
previously pushed those commits elsewhere.
Why does my boss/team care?
• Merging
– Merge conflicts between your changes and others doesn’t
happen until you merge back into master and resolve them
there, with new commits that happen after the merge commit.
– You are testing your changes in isolation until you merge with
master
• Rebasing
– Merge conflicts between your changes and others happens in
your feature branch and are resolved in your feature branch.
– You are testing your changes with the latest changes from
master in your topic branch.
When should I rebase?
• Fine fine fine. You’ve convinced me to try out this
“rebase” thing. How often / when should I rebase
changes from master into my feature branch?
– Every morning? Wait until I have my feature branch working in
isolation and then do one monster rebase resolving 2 months
worth of differences?
• “It depends.” 
– In my 6.5 years of experience with git, it seems to amount to the
same time/effort either way. You either pay for it in smaller ~10
minute chunks, or in one massive, 2 hour long rebase session at
the end.
Let’s talk about merge conflicts…
Merge Conflicts!
• Git rebase origin/master
• Hit a conflict  git mergetool
• Ok, everything’s fixed  git rebase --continue
– (loop)
• Crap! I messed that up and need to bail out!  git rebase --abort
• Hmm, looks like I made the exact same change in my topic branch
as was already made in master, I need to skip this commit and keep
rebasing  git rebase --skip
• If you have issues with a merge and want to revert it while the
merge is in progress, the equivalent is git reset HEAD --hard.
– Remember, rebase applies commits one at a time and makes you deal with any
conflicts one at a time. Merge simply looks at the <initial state> vs. <cumulative
end state> and gives you a single, massive set of changes to resolve!
Merge Conflicts
• git merge <branch> (git merge feature-47_fixed-stuff)
– Merge changes in the specified branch onto the current branch
– In a merge conflict branch you are ON is local, branch specified in the
merge is remote.
• git rebase <remote>/<branch> (git rebase origin/master)
– Base the current branch you are on off the specified remote branch
– Takes the specified branch as the base, and then applies any changes
you have in your local branch on top of that. For conflicts, the current
branch is called remote, and the branch you specified is treated as local.
• Assuming you are merging feature branches into master or rebase
feature branches with master, local = master, remote = feature in
both situations.
Interactive Rebase (Squash)
• git rebase –i HEAD~3
– pick f392171 Added new feature X
– pick ba9dd9a Added new elements to page design
– pick df71a27 Updated CSS for new elements
• Change to:
– pick f392171 Added new feature X
– squash ba9dd9a Added new elements to page
design
– squash df71a27 Updated CSS for new elements
Squash Demo
(Or git push –f origin master Or git push origin +master:master)
Golden Rule of Rebasing: NEVER EVER* rebase a branch that you
already pushed or that you pulled from another person.
* Except for when you truly want/need to rewrite the history of your repository.
git reflog
• Uses VIM navigation controls: J = down arrow, K = up arrow, Q = quit
• Use the commit hashes, not the HEAD positions! To go back in time and return your
current branch to a prior state, the syntax is git reset 2a7d231 --hard. (This will
return you to the point before the last 2 rebases in the above screenshot.)
Reflog demo
Detached Heads, Submodules
• Detached Head – Tracking a specific commit,
not a branch!
– If git was a book:
• HEAD is where your finger is (current spot)
• Branches are where you put your bookmarks
– If you close the book without a bookmark on that page, you
lose your spot!
• Submodules – Shared ‘child’ git repository
(notorious for detached HEAD issues…)
What does a detached head look like?
Mixed Demo
git worktree (new in 2.5.0)
• Who here has a co-worker that instead of
swapping branches just pulls down multiple
copies of the repo into different folders?
– Allows you to checkout multiple branches
simultaneously (master + sprint/feature)
• Instead of stash, you can you this!
– git worktree add -b hotfix ../hotfix origin/master
• After work/commits are done, simply delete the /hotfix folder!
– All worktree branches use the “parents” .git repo
• Saves space, all worktree branches are dumb/disposable
Code Review Demo
29
2nd Order Diff Trick
• Migrating Textile URLs to Markdown using Regex
– s{"(.+?)":([^.,:s]+)}{[$1]($2)}g  Not even close
– s{"(.+?)":([^.,!)]s]+)}{[$1]($2)}g  Not even close
– s{"(.+?)":([^,!)]s]+)b}{[$1]($2)}g  Getting closer
– s{"(.+?)":([^,!)]s]+)}{[$1]($2)}g  Pretty close!
• git stash save “almost there”
– s{"(.+?)":([^,!)]s]+)}{my($t,$l)=($1,$2);$l=~/(.*?)([.:])?$/;
"[$t]($1)$2"}eg  “minor tweak”
• git diff ‘stash@{0}’
– http://blog.moertel.com/posts/2013-02-18-git-second-order-diff.html
Filter Branch
• git filter-branch --force --index-filter 'git rm --
cached -r --ignore-unmatch MY-BIG-
DIRECTORY-OR-FILE' --prune-empty --tag-
name-filter cat -- --all
– This is SLOW.
– You have to use the full path to the file/folder you
want filtered out.
– You would have to run it twice for /bin/OpenId.dll &
/bin/debug/OpenId.dll (or generate a file with all the
path/filenames, then script it against the above.)
BFG Repo Cleaner
• Written in Scala, requires JVM 7+
• Much faster (10-720x faster)
• Much easier to use (alias java -jar bfg.jar to bfg)
– bfg --delete-files EntityFramework.dll my-repo.git
• Purge EntityFramework.dll out of the 105 bin folders in my
solution in one easy, readable command.
– bfg --strip-blobs-bigger-than 10M my-repo.git
• Purges any file from history that was ever larger than 10Mb
– bfg --delete-files id_{dsa,rsa} my-repo.git
• Purges id_rsa & id_dsa files from any path and point in time
– bfg --delete-folders bin my-repo.git
Raven.git Demo
BFG
• git clone --mirror <old_remote>
• bfg –delete-files Quartz.dll my-repo.git
– <repeat>, inspect your changes. If happy proceed!
• git gc
• cd my-repo.git
• git reflog expire --expire=now --all && git gc --
prune=now –aggressive
• git push <new_remote>
Git LFS
Git LFS Cheatsheet
• Install the commandline git extension, then run
git lfs install or git lfs init
• git lfs track “*.psd”
– Only works going forward, you will have to use BFG
or filter branch to retroactively purge any already
committed large files!
– git-lfs fetch (get me the ACTUAL huge files)
– git-lfs push
– git-lfs status (status on just the LFS tracked files)
– bfg --convert-to-git-lfs '*.psd' --no-blob-protection
Migrate from SVN
• git svn clone http://svn.example.com/project/trunk
– Git svn fetch, git svn rebase
– Git svn dcommit
– Have a non-standard directory layout?
• https://git-scm.com/docs/git-svn
Migrate from Mercurial (hg)
• Hg fast-export plugin (seems to be the most
popular currently)
• hggit Mercurial plugin
• git-hg git plugin
• gitifyhg git plugin
Resources
• Cheat sheets
– https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf
– http://www.git-tower.com/blog/git-cheat-sheet/
– https://www.atlassian.com/dms/wac/images/landing/git/atlassian_git_cheatsheet.pdf
– http://ndpsoftware.com/git-cheatsheet.html
• Recommended
– http://rogerdudler.github.io/git-guide/
– http://www.slideshare.net/lemiorhan/git-branching-
model?utm_source=slideshow&utm_medium=ssemail&utm_campaign=weekly_digest
– GitFlow: https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow
– Worktree: https://github.com/blog/2042-git-2-5-including-multiple-worktrees-and-
triangular-workflows
– Submodules: https://git-scm.com/book/en/v2/Git-Tools-Submodules
• Git Clients
– http://sourcetreeapp.com/ (Mac/Windows GUI)
– http://gitextensions.github.io/ (Windows)
– https://github.com/dahlbyk/posh-git (PowerShell command line)
– http://www.git-tower.com/ (Mac client)
Resources II
• Git LFS
– https://git-lfs.github.com/
– http://www.sitepoint.com/versioning-large-files-with-git-lfs/
– https://help.github.com/articles/installing-git-large-file-storage/
– https://help.github.com/articles/about-git-large-file-storage/
– https://help.github.com/articles/configuring-git-large-file-storage/
– https://github.com/blog/1986-announcing-git-large-file-storage-lfs
– https://about.gitlab.com/2015/11/23/announcing-git-lfs-support-in-gitlab/
– http://stackoverflow.com/questions/33330771/git-lfs-this-exceeds-githubs-file-size-limit-of-
100-00-mb
• BFG
– https://help.github.com/articles/remove-sensitive-data/
– https://rtyley.github.io/bfg-repo-cleaner/
– http://blog.ctp.com/2014/04/15/cleaning-up-git-repositories-with-the-bfg-repo-cleaner/
– Git LFS support: https://github.com/rtyley/bfg-repo-cleaner/releases/tag/v1.12.5
• Filter Branch
– Filter branch stuff: https://multiplestates.wordpress.com/2015/03/04/reducing-the-size-of-a-
git-repository-by-removing-a-file-or-folder-from-its-history/
Kwen Peterson
• @kwenarik
• kwen.peterson@gmail.com
• https://github.com/Kwen/sfdevs
– These slides + Demo Examples
Common git branching strategy
• 3 core branches: master, dev, release
– Master is “the truth” and should always be in a stable,
fully-functional state. This is where new topic/feature
branches are cloned from. (nightly builds)
– Dev is where integration testing happens. This is
where topic branches are pushed and tested PRIOR
to being signed off on as stable enough to go to
master. (continuous integration, test environment)
– Release is where the last live build lives. This exists
so that if an absolutely critical issue arises, we can
make the fix here and release the build, being
confident that the ONLY change going out is the
critical bug fix.
Git Best Practices for Teams
Git Best Practices for Teams

More Related Content

What's hot

CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...Skills Matter Talks
 
Oozie Hug May 2011
Oozie Hug May 2011Oozie Hug May 2011
Oozie Hug May 2011mislam77
 
When Two Worlds Collide: Java and Ruby in the Enterprise
When Two Worlds Collide: Java and Ruby in the EnterpriseWhen Two Worlds Collide: Java and Ruby in the Enterprise
When Two Worlds Collide: Java and Ruby in the Enterprisebenbrowning
 
Basic principles of Git
Basic principles of GitBasic principles of Git
Basic principles of Gitphuongvohuy
 
Leveraging Composer in Existing Projects
Leveraging Composer in Existing ProjectsLeveraging Composer in Existing Projects
Leveraging Composer in Existing ProjectsMark Niebergall
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011Bachkoutou Toutou
 
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet Campmodern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet CampPuppet
 
Migrating to a Bazel-based CI System: 6 Learnings - Or Shachar
Migrating to a Bazel-based CI System: 6 Learnings - Or ShacharMigrating to a Bazel-based CI System: 6 Learnings - Or Shachar
Migrating to a Bazel-based CI System: 6 Learnings - Or ShacharWix Engineering
 
Pointing Fingers? DDM to the Rescue
Pointing Fingers? DDM to the RescuePointing Fingers? DDM to the Rescue
Pointing Fingers? DDM to the RescueKeith Brooks
 
Docker, Kubernetes, and Mesos recipes for Java developers
Docker, Kubernetes, and Mesos recipes for Java developersDocker, Kubernetes, and Mesos recipes for Java developers
Docker, Kubernetes, and Mesos recipes for Java developersArun Gupta
 
Inform2015 - What's New in Domino 9 & 9.0.1 for Admins
Inform2015 - What's New in Domino 9 & 9.0.1 for AdminsInform2015 - What's New in Domino 9 & 9.0.1 for Admins
Inform2015 - What's New in Domino 9 & 9.0.1 for AdminsJared Roberts
 
How to use the new Domino Query Language
How to use the new Domino Query LanguageHow to use the new Domino Query Language
How to use the new Domino Query LanguageTim Davis
 

What's hot (13)

CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
 
Oozie Hug May 2011
Oozie Hug May 2011Oozie Hug May 2011
Oozie Hug May 2011
 
When Two Worlds Collide: Java and Ruby in the Enterprise
When Two Worlds Collide: Java and Ruby in the EnterpriseWhen Two Worlds Collide: Java and Ruby in the Enterprise
When Two Worlds Collide: Java and Ruby in the Enterprise
 
Basic principles of Git
Basic principles of GitBasic principles of Git
Basic principles of Git
 
Leveraging Composer in Existing Projects
Leveraging Composer in Existing ProjectsLeveraging Composer in Existing Projects
Leveraging Composer in Existing Projects
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011
 
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet Campmodern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
 
Migrating to a Bazel-based CI System: 6 Learnings - Or Shachar
Migrating to a Bazel-based CI System: 6 Learnings - Or ShacharMigrating to a Bazel-based CI System: 6 Learnings - Or Shachar
Migrating to a Bazel-based CI System: 6 Learnings - Or Shachar
 
Pointing Fingers? DDM to the Rescue
Pointing Fingers? DDM to the RescuePointing Fingers? DDM to the Rescue
Pointing Fingers? DDM to the Rescue
 
Docker, Kubernetes, and Mesos recipes for Java developers
Docker, Kubernetes, and Mesos recipes for Java developersDocker, Kubernetes, and Mesos recipes for Java developers
Docker, Kubernetes, and Mesos recipes for Java developers
 
Gearman
GearmanGearman
Gearman
 
Inform2015 - What's New in Domino 9 & 9.0.1 for Admins
Inform2015 - What's New in Domino 9 & 9.0.1 for AdminsInform2015 - What's New in Domino 9 & 9.0.1 for Admins
Inform2015 - What's New in Domino 9 & 9.0.1 for Admins
 
How to use the new Domino Query Language
How to use the new Domino Query LanguageHow to use the new Domino Query Language
How to use the new Domino Query Language
 

Similar to Git Best Practices for Teams

Git and Github slides.pdf
Git and Github slides.pdfGit and Github slides.pdf
Git and Github slides.pdfTilton2
 
Git and Github workshop ppt slide by slide
Git and Github workshop ppt slide by slideGit and Github workshop ppt slide by slide
Git and Github workshop ppt slide by slideRaghavendraVattikuti1
 
Beginner's Guide to Version Control with Git
Beginner's Guide to Version Control with GitBeginner's Guide to Version Control with Git
Beginner's Guide to Version Control with GitRobert Lee-Cann
 
Working with others using git and Github
Working with others using git and GithubWorking with others using git and Github
Working with others using git and GithubJacob Jenkins
 
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)Ahmed El-Arabawy
 
Honestly Git Playground 20190221
Honestly Git Playground 20190221Honestly Git Playground 20190221
Honestly Git Playground 20190221Shinho Kang
 
Git cheat sheet__grey
Git cheat sheet__greyGit cheat sheet__grey
Git cheat sheet__greyKing Hom
 
Git cheat sheet__white
Git cheat sheet__whiteGit cheat sheet__white
Git cheat sheet__whiteKing Hom
 
Git cheat sheet_dark
Git cheat sheet_darkGit cheat sheet_dark
Git cheat sheet_darkKing Hom
 
Git 101: Force-sensitive to Jedi padawan
Git 101: Force-sensitive to Jedi padawanGit 101: Force-sensitive to Jedi padawan
Git 101: Force-sensitive to Jedi padawanJames Ford
 
An introduction to Git
An introduction to GitAn introduction to Git
An introduction to GitMuhil Vannan
 
Git basics a starter on git and its ecosystem
Git basics  a starter on git and its ecosystemGit basics  a starter on git and its ecosystem
Git basics a starter on git and its ecosystemFrançois D'Agostini
 
Essential git for developers
Essential git for developersEssential git for developers
Essential git for developersAidan Casey
 

Similar to Git Best Practices for Teams (20)

Git more done
Git more doneGit more done
Git more done
 
Git and Github slides.pdf
Git and Github slides.pdfGit and Github slides.pdf
Git and Github slides.pdf
 
Git and Github workshop ppt slide by slide
Git and Github workshop ppt slide by slideGit and Github workshop ppt slide by slide
Git and Github workshop ppt slide by slide
 
Beginner's Guide to Version Control with Git
Beginner's Guide to Version Control with GitBeginner's Guide to Version Control with Git
Beginner's Guide to Version Control with Git
 
Working with others using git and Github
Working with others using git and GithubWorking with others using git and Github
Working with others using git and Github
 
Git 101
Git 101Git 101
Git 101
 
Git first steps
Git first stepsGit first steps
Git first steps
 
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
 
Honestly Git Playground 20190221
Honestly Git Playground 20190221Honestly Git Playground 20190221
Honestly Git Playground 20190221
 
Git
Git Git
Git
 
Git cheat sheet__grey
Git cheat sheet__greyGit cheat sheet__grey
Git cheat sheet__grey
 
Git cheat sheet__white
Git cheat sheet__whiteGit cheat sheet__white
Git cheat sheet__white
 
Git cheat sheet_dark
Git cheat sheet_darkGit cheat sheet_dark
Git cheat sheet_dark
 
Git 101: Force-sensitive to Jedi padawan
Git 101: Force-sensitive to Jedi padawanGit 101: Force-sensitive to Jedi padawan
Git 101: Force-sensitive to Jedi padawan
 
Gitlikeapro 2019
Gitlikeapro 2019Gitlikeapro 2019
Gitlikeapro 2019
 
An introduction to Git
An introduction to GitAn introduction to Git
An introduction to Git
 
Git basics a starter on git and its ecosystem
Git basics  a starter on git and its ecosystemGit basics  a starter on git and its ecosystem
Git basics a starter on git and its ecosystem
 
git and github
git and githubgit and github
git and github
 
Essential git for developers
Essential git for developersEssential git for developers
Essential git for developers
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshell
 

Git Best Practices for Teams

  • 1. • Wifi: RAV-TRAINING • Password: Solve Challenges • http://xkcd.com/1597/ • Hover text: If that doesn’t fix it, git.txt contains the phone number of a friend of mine who understands git. Just wait through a few minutes of “It’s really pretty simple, just think of branches as…” and eventually you’ll learn the commands that will fix everything. • Slides + Demo Code: • https://github.com/Kwen/sfdevs
  • 2. Making Git Work For You Kwen Peterson Senior Software Developer @ Raven kwen.peterson@gmail.com @kwenarik
  • 3. "You're already in the top tier of developers just by showing up here tonight. I don't know how talented you are, how much experience you have, but you showed up. You're putting yourself out there because you care to improve. Thanks for caring.“ – Scott Hanselman
  • 4. Rebase vs. Merge • Merging brings two lines of development together while preserving the ancestry of each commit history. • In contrast, rebasing unifies the lines of development by re-writing changes from the source branch so that they appear as children of the destination branch – effectively pretending that those commits were written on top of the destination branch all along. – Rebase requires the commits on the source branch to be re- written, which changes their content and their SHAS.
  • 5. The merge DNA helix vs. rebase OCD
  • 7. Commit graph (pure rebase with ff)
  • 9. Git merge --no-ff <feature>
  • 10. Merge • Pros – Simpler to use and understand – The commits on the source branch remain separate from other branch commits, provided you don’t perform a fast- forward merge. (This separation can be useful in the case of feature branches, where you might want to take a feature and merge it into another branch later.) – Existing commits on the source branch are unchanged and remain valid; it doesn’t matter if they’ve been shared. • Cons – If the need to merge arises simply because multiple people are working on the same branch in parallel, the merges don’t serve any useful historical purpose & create clutter.
  • 11. Master is the YELLOW commits
  • 12. Rebase • Pros – Simplifies your history. – Is the most intuitive and clutter-free way to combine commits from multiple developers in a shared branch. • Cons – Slightly more complex, conflicts require resolution on a per commit basis. – Rewriting of history has ramifications if you’ve previously pushed those commits elsewhere.
  • 13. Why does my boss/team care? • Merging – Merge conflicts between your changes and others doesn’t happen until you merge back into master and resolve them there, with new commits that happen after the merge commit. – You are testing your changes in isolation until you merge with master • Rebasing – Merge conflicts between your changes and others happens in your feature branch and are resolved in your feature branch. – You are testing your changes with the latest changes from master in your topic branch.
  • 14. When should I rebase? • Fine fine fine. You’ve convinced me to try out this “rebase” thing. How often / when should I rebase changes from master into my feature branch? – Every morning? Wait until I have my feature branch working in isolation and then do one monster rebase resolving 2 months worth of differences? • “It depends.”  – In my 6.5 years of experience with git, it seems to amount to the same time/effort either way. You either pay for it in smaller ~10 minute chunks, or in one massive, 2 hour long rebase session at the end.
  • 15. Let’s talk about merge conflicts…
  • 16. Merge Conflicts! • Git rebase origin/master • Hit a conflict  git mergetool • Ok, everything’s fixed  git rebase --continue – (loop) • Crap! I messed that up and need to bail out!  git rebase --abort • Hmm, looks like I made the exact same change in my topic branch as was already made in master, I need to skip this commit and keep rebasing  git rebase --skip • If you have issues with a merge and want to revert it while the merge is in progress, the equivalent is git reset HEAD --hard. – Remember, rebase applies commits one at a time and makes you deal with any conflicts one at a time. Merge simply looks at the <initial state> vs. <cumulative end state> and gives you a single, massive set of changes to resolve!
  • 17. Merge Conflicts • git merge <branch> (git merge feature-47_fixed-stuff) – Merge changes in the specified branch onto the current branch – In a merge conflict branch you are ON is local, branch specified in the merge is remote. • git rebase <remote>/<branch> (git rebase origin/master) – Base the current branch you are on off the specified remote branch – Takes the specified branch as the base, and then applies any changes you have in your local branch on top of that. For conflicts, the current branch is called remote, and the branch you specified is treated as local. • Assuming you are merging feature branches into master or rebase feature branches with master, local = master, remote = feature in both situations.
  • 18. Interactive Rebase (Squash) • git rebase –i HEAD~3 – pick f392171 Added new feature X – pick ba9dd9a Added new elements to page design – pick df71a27 Updated CSS for new elements • Change to: – pick f392171 Added new feature X – squash ba9dd9a Added new elements to page design – squash df71a27 Updated CSS for new elements
  • 20.
  • 21. (Or git push –f origin master Or git push origin +master:master) Golden Rule of Rebasing: NEVER EVER* rebase a branch that you already pushed or that you pulled from another person. * Except for when you truly want/need to rewrite the history of your repository.
  • 22. git reflog • Uses VIM navigation controls: J = down arrow, K = up arrow, Q = quit • Use the commit hashes, not the HEAD positions! To go back in time and return your current branch to a prior state, the syntax is git reset 2a7d231 --hard. (This will return you to the point before the last 2 rebases in the above screenshot.)
  • 24. Detached Heads, Submodules • Detached Head – Tracking a specific commit, not a branch! – If git was a book: • HEAD is where your finger is (current spot) • Branches are where you put your bookmarks – If you close the book without a bookmark on that page, you lose your spot! • Submodules – Shared ‘child’ git repository (notorious for detached HEAD issues…)
  • 25. What does a detached head look like?
  • 26.
  • 28. git worktree (new in 2.5.0) • Who here has a co-worker that instead of swapping branches just pulls down multiple copies of the repo into different folders? – Allows you to checkout multiple branches simultaneously (master + sprint/feature) • Instead of stash, you can you this! – git worktree add -b hotfix ../hotfix origin/master • After work/commits are done, simply delete the /hotfix folder! – All worktree branches use the “parents” .git repo • Saves space, all worktree branches are dumb/disposable
  • 30. 2nd Order Diff Trick • Migrating Textile URLs to Markdown using Regex – s{"(.+?)":([^.,:s]+)}{[$1]($2)}g  Not even close – s{"(.+?)":([^.,!)]s]+)}{[$1]($2)}g  Not even close – s{"(.+?)":([^,!)]s]+)b}{[$1]($2)}g  Getting closer – s{"(.+?)":([^,!)]s]+)}{[$1]($2)}g  Pretty close! • git stash save “almost there” – s{"(.+?)":([^,!)]s]+)}{my($t,$l)=($1,$2);$l=~/(.*?)([.:])?$/; "[$t]($1)$2"}eg  “minor tweak” • git diff ‘stash@{0}’ – http://blog.moertel.com/posts/2013-02-18-git-second-order-diff.html
  • 31. Filter Branch • git filter-branch --force --index-filter 'git rm -- cached -r --ignore-unmatch MY-BIG- DIRECTORY-OR-FILE' --prune-empty --tag- name-filter cat -- --all – This is SLOW. – You have to use the full path to the file/folder you want filtered out. – You would have to run it twice for /bin/OpenId.dll & /bin/debug/OpenId.dll (or generate a file with all the path/filenames, then script it against the above.)
  • 32. BFG Repo Cleaner • Written in Scala, requires JVM 7+ • Much faster (10-720x faster) • Much easier to use (alias java -jar bfg.jar to bfg) – bfg --delete-files EntityFramework.dll my-repo.git • Purge EntityFramework.dll out of the 105 bin folders in my solution in one easy, readable command. – bfg --strip-blobs-bigger-than 10M my-repo.git • Purges any file from history that was ever larger than 10Mb – bfg --delete-files id_{dsa,rsa} my-repo.git • Purges id_rsa & id_dsa files from any path and point in time – bfg --delete-folders bin my-repo.git
  • 34. BFG • git clone --mirror <old_remote> • bfg –delete-files Quartz.dll my-repo.git – <repeat>, inspect your changes. If happy proceed! • git gc • cd my-repo.git • git reflog expire --expire=now --all && git gc -- prune=now –aggressive • git push <new_remote>
  • 36. Git LFS Cheatsheet • Install the commandline git extension, then run git lfs install or git lfs init • git lfs track “*.psd” – Only works going forward, you will have to use BFG or filter branch to retroactively purge any already committed large files! – git-lfs fetch (get me the ACTUAL huge files) – git-lfs push – git-lfs status (status on just the LFS tracked files) – bfg --convert-to-git-lfs '*.psd' --no-blob-protection
  • 37. Migrate from SVN • git svn clone http://svn.example.com/project/trunk – Git svn fetch, git svn rebase – Git svn dcommit – Have a non-standard directory layout? • https://git-scm.com/docs/git-svn
  • 38. Migrate from Mercurial (hg) • Hg fast-export plugin (seems to be the most popular currently) • hggit Mercurial plugin • git-hg git plugin • gitifyhg git plugin
  • 39. Resources • Cheat sheets – https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf – http://www.git-tower.com/blog/git-cheat-sheet/ – https://www.atlassian.com/dms/wac/images/landing/git/atlassian_git_cheatsheet.pdf – http://ndpsoftware.com/git-cheatsheet.html • Recommended – http://rogerdudler.github.io/git-guide/ – http://www.slideshare.net/lemiorhan/git-branching- model?utm_source=slideshow&utm_medium=ssemail&utm_campaign=weekly_digest – GitFlow: https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow – Worktree: https://github.com/blog/2042-git-2-5-including-multiple-worktrees-and- triangular-workflows – Submodules: https://git-scm.com/book/en/v2/Git-Tools-Submodules • Git Clients – http://sourcetreeapp.com/ (Mac/Windows GUI) – http://gitextensions.github.io/ (Windows) – https://github.com/dahlbyk/posh-git (PowerShell command line) – http://www.git-tower.com/ (Mac client)
  • 40. Resources II • Git LFS – https://git-lfs.github.com/ – http://www.sitepoint.com/versioning-large-files-with-git-lfs/ – https://help.github.com/articles/installing-git-large-file-storage/ – https://help.github.com/articles/about-git-large-file-storage/ – https://help.github.com/articles/configuring-git-large-file-storage/ – https://github.com/blog/1986-announcing-git-large-file-storage-lfs – https://about.gitlab.com/2015/11/23/announcing-git-lfs-support-in-gitlab/ – http://stackoverflow.com/questions/33330771/git-lfs-this-exceeds-githubs-file-size-limit-of- 100-00-mb • BFG – https://help.github.com/articles/remove-sensitive-data/ – https://rtyley.github.io/bfg-repo-cleaner/ – http://blog.ctp.com/2014/04/15/cleaning-up-git-repositories-with-the-bfg-repo-cleaner/ – Git LFS support: https://github.com/rtyley/bfg-repo-cleaner/releases/tag/v1.12.5 • Filter Branch – Filter branch stuff: https://multiplestates.wordpress.com/2015/03/04/reducing-the-size-of-a- git-repository-by-removing-a-file-or-folder-from-its-history/
  • 41. Kwen Peterson • @kwenarik • kwen.peterson@gmail.com • https://github.com/Kwen/sfdevs – These slides + Demo Examples
  • 42. Common git branching strategy • 3 core branches: master, dev, release – Master is “the truth” and should always be in a stable, fully-functional state. This is where new topic/feature branches are cloned from. (nightly builds) – Dev is where integration testing happens. This is where topic branches are pushed and tested PRIOR to being signed off on as stable enough to go to master. (continuous integration, test environment) – Release is where the last live build lives. This exists so that if an absolutely critical issue arises, we can make the fix here and release the build, being confident that the ONLY change going out is the critical bug fix.

Editor's Notes

  1. Demo
  2. Demo with code review: git worktree add –b codereview ../Raven-codereview origin/100805-MFG-ATD-Kanban_SortKbRelease
  3. Demo
  4. java -jar /c/python27/bfg-1.12.7.jar --delete-files Quartz.dll /c/Code/Raven.git/