SlideShare a Scribd company logo
1 of 75
Download to read offline
Carlos Duarte Do Nascimento
@chesterbr • http://chester.me
git fail --force
make it up with your pull requests
Let’s write some code!
Whoops...
WTF?
It’s either “git for dummies”...
Hovertext:“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.”
Git - 30/Out/2015
https://xkcd.com/1597/
Neither rookie nor expert
Good professionals will try to become
proficient in their tools - not become
specialists in all of them
Expert Advice
● “Do a git pull once in a while”
● “Never use git pull”
● “Always rebase!”
● “Never rebase!!!”
● ...
How does that end?
Proposal
Revisit git concepts from the
perspective of a typical workflow,
avoiding theory overload and the
abuse of rules and cookbooks
Carlos Duarte Do Nascimento
(Chester)
@chesterbr • http://chester.me
Git non-specialist
https://www.shopify.com/careers
Git
A system that allows people to work
with source code in an orderly and
simultaneous fashion
Commit
When a meaningful change is made,
you take it to the stage in order to
take a snapshot (commit)
A92347C2…1F2493E34: Increase submit button size
DescriptionID
Commit == snapshot
“Git is all about composing and saving
snapshots of your project and then
working with and comparing those
snapshots”
http://gitref.org/basic/
Branch
Successive commits form a timeline
One can commit into alternative
timelines (branches) and later integrate
those with the main timeline (master)
Group work
Your commits “live” in your local
repository (.git/)
You can push commits to other
people’s repositories (remotes), and
also pull commits from there
Organizing your group
This flow of commits/branches can be
organized in several ways (workflows)
A central repository that
enables code reviews by means
of pull requests helps a lot...
● Refresh master (remote ⇒ local)
● Create new branch from there
● Commits, commits, commits!
● Push, PR (my branch ⇒ master)
●
●
But what if...
● ...CI* fails?
● ...other devs suggest changes?
● ...master changed while I worked?
*Continuous Integration
Conflict!
Image © 1995 GAINAX/Project Eva.
Manual merge
$ git checkout master
$ git pull
$ git checkout my-branch
...
$ git commit
$ git push
...
$ git push --force
#NOT
It can get really bad
● Irreconcilable conflicts
● Alien commits on my PR
● Commit is there, code is not
● The @#%@ button is still grey
● ...
How can we fix/avoid that?
By understanding what is happening on
a pull request to identify (and avoid)
traps, or, as a last resource, to rebuild
our PR with minimal effort
Pull request
Pull Merge request
“wants to merge?”
git merge
Includes commits from another branch in
the current one, without changing them
(an extra commit at the end will
consolidate changes from both sides)
How can that go wrong?
Consolidating changes from two
different timelines becomes more
complex as they diverge
Avoid the problem
Whenever possible, create small,
isolated and short-living branches
If you can’t avoid it
There are several ways to make a
branch compatible with master again
Our workflow works well with rebase
git rebase
Rebuild the branch from its original
point (by default), creating new commits
identical to the original ones (by default)
(hint: these defaults won’t help you)
What can I do with rebase?
● (re-)base your changes on a more
recent master
(e.g.: git fetch; git rebase origin/master)
● Simplify your commits
(e.g.: git rebase --interactive master)
How can that go wrong?
Updating the branch and simplifying
commits at the same time is tricky
Rewriting a branch that is already
published causes incompatibilities
PR prep suggestion (1)
Recreate your branch from a
more recent master
(without changing the commits)
git checkout master
git pull
git checkout my-branch
git rebase master
PR prep suggestion (2)
Simplify your commits
(without changing the branch point)
git rebase –-interactive master
Suggestion ≠ rule
If the master didn’t change (much),
don’t botter updating
If your commits are clear,
don’t bother interacting
After the PR is created
New commits can be added to the
remote, just git push them
Rebase, however, isn’t that simple
(why?)
Rebases change the past!!!
(uhhh... so what?)
Image © Universal Studios.All rights Reserved
Is that a real problem?
A git push from a rebased branch has
to replace the remote branch history
(hence the --force requirement)
Workflow to the rescue
If the workflow says
only the creator
commits on a PR,
rebasing its branch
should not cause
any trouble
Image © 2016Twentieth Century Fox Film Corporation.
Avoiding further trouble
Just like before the first push, don’t
change the start point and
simplify commits at the same time
A new commit is always less
tricky than a rebase
Master is sacred
Branches / pull requests are only
relevant during their lifetime
It hit the fan - now what?
git cherry-pick
Reproduces in your branch the
changes from a single commit
(even a branch-less one)
git cherry-pick id
Cherry-pick good commits
Instead of recreating changes manually,
we can cherry-pick the original
commits into a fresh branch
But we need to find those commits...
git log x git reflog
git log lists commits created in the
current branch (lots of search options)
git reflog lists any operations that
affected the local repository in any way
git reflog
f84195a HEAD@{0}: checkout: moving from 1468-more-resilience-on-in
d5a8868 HEAD@{1}: commit: Log the invalid listing on offer-less of
79e7c98 HEAD@{2}: commit: Better test naming and more detailed log
31e2ac1 HEAD@{3}: checkout: moving from master to 1468-more-resili
f84195a HEAD@{4}: rebase finished: returning to refs/heads/master
f84195a HEAD@{5}: pull --rebase --autostash: checkout f84195a626e9
ac12705 HEAD@{6}: rebase finished: returning to refs/heads/master
ac12705 HEAD@{7}: pull --rebase --autostash: checkout ac127053051c
...
#howto
1. Find all the commits that were
supposed to be on your PR
git log • git reflog •
#howto
2. Sync your master with the server
and create a brand new branch from it
git checkout master
git pull
git checkout -b new-branch
#howto
3.Apply the commits
git cherry-pick id1 id2 id3...
#howto
4. Replace the old branch with the new
one (on local and remote repos)
git checkout master
git branch -D my-branch
git branch -m new-branch my-branch
git checkout my-branch
git push –-set-upstream origin my-branch
--force
Life is good again!
Conclusion (1)
You don’t need to know everything
about git, but it’s a good idea to have a
solid understanding of some concepts
(e.g.: commit, branch, merge, rebase)
Conclusion (2)
As long as it was committed, there is
always a way to recover your work.
Avoiding push accidents
Image © CAPCOM Ltd..All rights Reserved
Don’t use the --force, Luke
Make a habit of trying “vanilla” git
push before adding any arguments
(e.g., origin my-branch or --force)
On a new branch
$ git checkout -b new-branch
Switched to a new branch 'new-branch'
$ git commit -am "my changes"
[new-branch eabe2c9] my changes
1 file changed, 2 insertions(+)
$ git push
fatal: The current branch new-branch has no upstream branch
To push the current branch and set the remote as upstream,
use
git push --set-upstream origin new-branch
$ git push --set-upstream origin new-branch
After a rebase
$ git rebase -i master
Switched to a new branch 'new-branch'
$ git push
To github.com:Shopify/some-repo.git
! [rejected] new-branch -> new-branch (non-fast-forward)
error: failed to push some refs to 'github.com:Shopify/
some-repo.git'
hint: Updates were rejected because the tip of your current
branch is behind
$ git push --force
Know where you are
Always show the current branch on
your command line prompt (bash):
export PS1="h:W u[033[32m]
$(__git_ps1)[033[00m]$ "
MyComputer:myproject me (my-branch)$
GUI x CLI
Image CC BY-SAWikipedia user Sandstein
gitk
GitHub Desktop
Tig
Stay fresh
Update your master to the latest
remote without leaving your branch
git pull --rebase --autostash
For git < 2.9, use the git-up gem
gem install git-up
git up
Non-brute force
Rewrite history, but only if no one else
added commits to the branch
git push --force-with-lease
Branch prefix shortcuts
git push --delete my-branch
git push --force my-branch
can be shortened to
git push :my-branch
git push +my-branch
(just be careful not to add + by habit)
Thank you!
Carlos Duarte Do Nascimento
@chesterbr • http://chester.me
shopify.com/careers
http://slideshare.net/chesterbr
This presentation is available under the Creative Commons “by-nc” 3.0 license
(available at https://creativecommons.org/licenses/by-nc/3.0/),
noticing the exceptions below.
Images and text from third parties were included (with due credits wherever possible)
under a fair use assumption (or, for Brazilian media, under Art. 46 of Law 9610/98)
and/or under their respective licenses. Omissions are unintended and corrections
welcome. Such content is excluded from the license above.
GitHub, the GitHub logo and mascot are trademarks of GitHub, Inc.
Shopify and the Shopify logo are trademaks of Shopify, Inc.
The opinions stated here belong solely to the author, not offically representing his
employer’s opinions, nor any of of the persons or companies mentioned in any extent.
Credits And License

More Related Content

What's hot

How to use any static site generator with GitLab Pages.
How to use any static site generator with GitLab Pages. How to use any static site generator with GitLab Pages.
How to use any static site generator with GitLab Pages. Ivan Nemytchenko
 
Git flow Introduction
Git flow IntroductionGit flow Introduction
Git flow IntroductionDavid Paluy
 
Git For The Android Developer
Git For The Android DeveloperGit For The Android Developer
Git For The Android DeveloperEffective
 
Git Educated About Git - 20 Essential Commands
Git Educated About Git - 20 Essential CommandsGit Educated About Git - 20 Essential Commands
Git Educated About Git - 20 Essential CommandsJeremy Lindblom
 
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...The Linux Foundation
 
Git workflow in agile development
Git workflow in agile developmentGit workflow in agile development
Git workflow in agile developmentZack Siri
 
GitHub Talk - Cody Carnachan
GitHub Talk - Cody CarnachanGitHub Talk - Cody Carnachan
GitHub Talk - Cody CarnachanCody Carnachan
 
Why Aren't You Using Git Flow?
Why Aren't You Using Git Flow?Why Aren't You Using Git Flow?
Why Aren't You Using Git Flow?John Congdon
 
Brief tutorial on Git
Brief tutorial on GitBrief tutorial on Git
Brief tutorial on Git聖文 鄭
 
Git Flow - An Introduction
Git Flow - An IntroductionGit Flow - An Introduction
Git Flow - An IntroductionKnoldus Inc.
 
A painless self-hosted Git service: Gitea
A painless self-hosted Git service: GiteaA painless self-hosted Git service: Gitea
A painless self-hosted Git service: GiteaBo-Yi Wu
 

What's hot (20)

Git basics for beginners
Git basics for beginnersGit basics for beginners
Git basics for beginners
 
Advanced Git
Advanced GitAdvanced Git
Advanced Git
 
沒有 GUI 的 Git
沒有 GUI 的 Git沒有 GUI 的 Git
沒有 GUI 的 Git
 
Git advanced
Git advancedGit advanced
Git advanced
 
Git flow
Git flowGit flow
Git flow
 
Github
GithubGithub
Github
 
Wokshop de Git
Wokshop de Git Wokshop de Git
Wokshop de Git
 
Git workflows
Git workflowsGit workflows
Git workflows
 
How to use any static site generator with GitLab Pages.
How to use any static site generator with GitLab Pages. How to use any static site generator with GitLab Pages.
How to use any static site generator with GitLab Pages.
 
Git flow Introduction
Git flow IntroductionGit flow Introduction
Git flow Introduction
 
My Git workflow
My Git workflowMy Git workflow
My Git workflow
 
Git For The Android Developer
Git For The Android DeveloperGit For The Android Developer
Git For The Android Developer
 
Git Educated About Git - 20 Essential Commands
Git Educated About Git - 20 Essential CommandsGit Educated About Git - 20 Essential Commands
Git Educated About Git - 20 Essential Commands
 
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...
 
Git workflow in agile development
Git workflow in agile developmentGit workflow in agile development
Git workflow in agile development
 
GitHub Talk - Cody Carnachan
GitHub Talk - Cody CarnachanGitHub Talk - Cody Carnachan
GitHub Talk - Cody Carnachan
 
Why Aren't You Using Git Flow?
Why Aren't You Using Git Flow?Why Aren't You Using Git Flow?
Why Aren't You Using Git Flow?
 
Brief tutorial on Git
Brief tutorial on GitBrief tutorial on Git
Brief tutorial on Git
 
Git Flow - An Introduction
Git Flow - An IntroductionGit Flow - An Introduction
Git Flow - An Introduction
 
A painless self-hosted Git service: Gitea
A painless self-hosted Git service: GiteaA painless self-hosted Git service: Gitea
A painless self-hosted Git service: Gitea
 

Similar to git fail --force (make it up with your pull requests)

Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control SystemVictor Wong
 
How to Really Get Git
How to Really Get GitHow to Really Get Git
How to Really Get GitSusan Tan
 
Honestly Git Playground 20190221
Honestly Git Playground 20190221Honestly Git Playground 20190221
Honestly Git Playground 20190221Shinho Kang
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With GitNick Quaranto
 
Git-ing out of your git messes
Git-ing out of  your git messesGit-ing out of  your git messes
Git-ing out of your git messesKatie Sylor-Miller
 
GIT in a nutshell
GIT in a nutshellGIT in a nutshell
GIT in a nutshellalignan
 
Checkitmobile advanced git
Checkitmobile advanced gitCheckitmobile advanced git
Checkitmobile advanced gitGerrit Wanderer
 
Introduction to Git for Artists
Introduction to Git for ArtistsIntroduction to Git for Artists
Introduction to Git for ArtistsDavid Newbury
 
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
 
Git Tricks — git utilities that make life git easier
Git Tricks — git utilities that make life git easierGit Tricks — git utilities that make life git easier
Git Tricks — git utilities that make life git easierChristoph Matthies
 
Git - Get Ready To Use It
Git - Get Ready To Use ItGit - Get Ready To Use It
Git - Get Ready To Use ItDaniel Kummer
 
Managing releases effectively through git
Managing releases effectively through gitManaging releases effectively through git
Managing releases effectively through gitMohd Farid
 

Similar to git fail --force (make it up with your pull requests) (20)

Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control System
 
How to Really Get Git
How to Really Get GitHow to Really Get Git
How to Really Get Git
 
Honestly Git Playground 20190221
Honestly Git Playground 20190221Honestly Git Playground 20190221
Honestly Git Playground 20190221
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With Git
 
Switching to Git
Switching to GitSwitching to Git
Switching to Git
 
Git-ing out of your git messes
Git-ing out of  your git messesGit-ing out of  your git messes
Git-ing out of your git messes
 
GIT in a nutshell
GIT in a nutshellGIT in a nutshell
GIT in a nutshell
 
Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)
 
Mastering GIT
Mastering GITMastering GIT
Mastering GIT
 
Checkitmobile advanced git
Checkitmobile advanced gitCheckitmobile advanced git
Checkitmobile advanced git
 
Git tips
Git tipsGit tips
Git tips
 
Introduction to Git for Artists
Introduction to Git for ArtistsIntroduction to Git for Artists
Introduction to Git for Artists
 
Git github
Git githubGit github
Git github
 
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
 
Git Tricks — git utilities that make life git easier
Git Tricks — git utilities that make life git easierGit Tricks — git utilities that make life git easier
Git Tricks — git utilities that make life git easier
 
Git - Get Ready To Use It
Git - Get Ready To Use ItGit - Get Ready To Use It
Git - Get Ready To Use It
 
Github integration-kostyasha
Github integration-kostyashaGithub integration-kostyasha
Github integration-kostyasha
 
Git
GitGit
Git
 
Jedi Mind Tricks in Git
Jedi Mind Tricks in GitJedi Mind Tricks in Git
Jedi Mind Tricks in Git
 
Managing releases effectively through git
Managing releases effectively through gitManaging releases effectively through git
Managing releases effectively through git
 

More from Carlos Duarte do Nascimento

More from Carlos Duarte do Nascimento (11)

git fail --force (faça as pazes com seus pull requests)
git fail --force (faça as pazes com seus pull requests)git fail --force (faça as pazes com seus pull requests)
git fail --force (faça as pazes com seus pull requests)
 
ruby2600 - an Atari 2600 emulator written in Ruby
ruby2600 - an Atari 2600 emulator written in Rubyruby2600 - an Atari 2600 emulator written in Ruby
ruby2600 - an Atari 2600 emulator written in Ruby
 
Atari 2600 VCS Programming
Atari 2600 VCS ProgrammingAtari 2600 VCS Programming
Atari 2600 VCS Programming
 
Desenvolvimento de Aplicações para o Google App Engine (CPBR5)
Desenvolvimento de Aplicações para o Google App Engine (CPBR5)Desenvolvimento de Aplicações para o Google App Engine (CPBR5)
Desenvolvimento de Aplicações para o Google App Engine (CPBR5)
 
Programação para Atari 2600
Programação para Atari 2600Programação para Atari 2600
Programação para Atari 2600
 
Mashups: Criando Valor na Web 2.0 (BandTec)
Mashups: Criando Valor na Web 2.0 (BandTec)Mashups: Criando Valor na Web 2.0 (BandTec)
Mashups: Criando Valor na Web 2.0 (BandTec)
 
Aplicativos Mobile: Da Idéia ao Produto (ou não)
Aplicativos Mobile: Da Idéia ao Produto (ou não)Aplicativos Mobile: Da Idéia ao Produto (ou não)
Aplicativos Mobile: Da Idéia ao Produto (ou não)
 
Apontador API (para programadores Python)
Apontador API (para programadores Python)Apontador API (para programadores Python)
Apontador API (para programadores Python)
 
Mashups: Criando Valor na Web 2.0
Mashups: Criando Valor na Web 2.0Mashups: Criando Valor na Web 2.0
Mashups: Criando Valor na Web 2.0
 
Cruzalinhas - Palestra Relâmpago no Fisl 11
Cruzalinhas - Palestra Relâmpago no Fisl 11Cruzalinhas - Palestra Relâmpago no Fisl 11
Cruzalinhas - Palestra Relâmpago no Fisl 11
 
SlideMeme - Habilitando o SlideShare dentro do Yahoo! Meme - Yahoo! Open Hack...
SlideMeme - Habilitando o SlideShare dentro do Yahoo! Meme - Yahoo! Open Hack...SlideMeme - Habilitando o SlideShare dentro do Yahoo! Meme - Yahoo! Open Hack...
SlideMeme - Habilitando o SlideShare dentro do Yahoo! Meme - Yahoo! Open Hack...
 

Recently uploaded

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
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
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
 
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
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
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
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
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
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
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
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 

Recently uploaded (20)

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
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
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...
 
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
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
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
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
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...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
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...
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 

git fail --force (make it up with your pull requests)

  • 1. Carlos Duarte Do Nascimento @chesterbr • http://chester.me git fail --force make it up with your pull requests
  • 5.
  • 6.
  • 7. It’s either “git for dummies”...
  • 8.
  • 9.
  • 10. Hovertext:“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.” Git - 30/Out/2015 https://xkcd.com/1597/
  • 11. Neither rookie nor expert Good professionals will try to become proficient in their tools - not become specialists in all of them
  • 12. Expert Advice ● “Do a git pull once in a while” ● “Never use git pull” ● “Always rebase!” ● “Never rebase!!!” ● ...
  • 14. Proposal Revisit git concepts from the perspective of a typical workflow, avoiding theory overload and the abuse of rules and cookbooks
  • 15. Carlos Duarte Do Nascimento (Chester) @chesterbr • http://chester.me Git non-specialist
  • 17.
  • 18. Git A system that allows people to work with source code in an orderly and simultaneous fashion
  • 19. Commit When a meaningful change is made, you take it to the stage in order to take a snapshot (commit) A92347C2…1F2493E34: Increase submit button size DescriptionID
  • 20. Commit == snapshot “Git is all about composing and saving snapshots of your project and then working with and comparing those snapshots” http://gitref.org/basic/
  • 21. Branch Successive commits form a timeline One can commit into alternative timelines (branches) and later integrate those with the main timeline (master)
  • 22. Group work Your commits “live” in your local repository (.git/) You can push commits to other people’s repositories (remotes), and also pull commits from there
  • 23. Organizing your group This flow of commits/branches can be organized in several ways (workflows) A central repository that enables code reviews by means of pull requests helps a lot...
  • 24. ● Refresh master (remote ⇒ local) ● Create new branch from there ● Commits, commits, commits! ● Push, PR (my branch ⇒ master) ● ●
  • 25. But what if... ● ...CI* fails? ● ...other devs suggest changes? ● ...master changed while I worked? *Continuous Integration
  • 26. Conflict! Image © 1995 GAINAX/Project Eva.
  • 27. Manual merge $ git checkout master $ git pull $ git checkout my-branch ... $ git commit $ git push ... $ git push --force
  • 28. #NOT
  • 29. It can get really bad ● Irreconcilable conflicts ● Alien commits on my PR ● Commit is there, code is not ● The @#%@ button is still grey ● ...
  • 30. How can we fix/avoid that? By understanding what is happening on a pull request to identify (and avoid) traps, or, as a last resource, to rebuild our PR with minimal effort
  • 33. git merge Includes commits from another branch in the current one, without changing them (an extra commit at the end will consolidate changes from both sides)
  • 34. How can that go wrong? Consolidating changes from two different timelines becomes more complex as they diverge
  • 35. Avoid the problem Whenever possible, create small, isolated and short-living branches
  • 36. If you can’t avoid it There are several ways to make a branch compatible with master again Our workflow works well with rebase
  • 37. git rebase Rebuild the branch from its original point (by default), creating new commits identical to the original ones (by default) (hint: these defaults won’t help you)
  • 38. What can I do with rebase? ● (re-)base your changes on a more recent master (e.g.: git fetch; git rebase origin/master) ● Simplify your commits (e.g.: git rebase --interactive master)
  • 39. How can that go wrong? Updating the branch and simplifying commits at the same time is tricky Rewriting a branch that is already published causes incompatibilities
  • 40. PR prep suggestion (1) Recreate your branch from a more recent master (without changing the commits) git checkout master git pull git checkout my-branch git rebase master
  • 41. PR prep suggestion (2) Simplify your commits (without changing the branch point) git rebase –-interactive master
  • 42. Suggestion ≠ rule If the master didn’t change (much), don’t botter updating If your commits are clear, don’t bother interacting
  • 43. After the PR is created New commits can be added to the remote, just git push them Rebase, however, isn’t that simple (why?)
  • 44. Rebases change the past!!! (uhhh... so what?) Image © Universal Studios.All rights Reserved
  • 45. Is that a real problem? A git push from a rebased branch has to replace the remote branch history (hence the --force requirement)
  • 46. Workflow to the rescue If the workflow says only the creator commits on a PR, rebasing its branch should not cause any trouble Image © 2016Twentieth Century Fox Film Corporation.
  • 47. Avoiding further trouble Just like before the first push, don’t change the start point and simplify commits at the same time A new commit is always less tricky than a rebase
  • 48. Master is sacred Branches / pull requests are only relevant during their lifetime
  • 49. It hit the fan - now what?
  • 50. git cherry-pick Reproduces in your branch the changes from a single commit (even a branch-less one) git cherry-pick id
  • 51. Cherry-pick good commits Instead of recreating changes manually, we can cherry-pick the original commits into a fresh branch But we need to find those commits...
  • 52. git log x git reflog git log lists commits created in the current branch (lots of search options) git reflog lists any operations that affected the local repository in any way
  • 53. git reflog f84195a HEAD@{0}: checkout: moving from 1468-more-resilience-on-in d5a8868 HEAD@{1}: commit: Log the invalid listing on offer-less of 79e7c98 HEAD@{2}: commit: Better test naming and more detailed log 31e2ac1 HEAD@{3}: checkout: moving from master to 1468-more-resili f84195a HEAD@{4}: rebase finished: returning to refs/heads/master f84195a HEAD@{5}: pull --rebase --autostash: checkout f84195a626e9 ac12705 HEAD@{6}: rebase finished: returning to refs/heads/master ac12705 HEAD@{7}: pull --rebase --autostash: checkout ac127053051c ...
  • 54. #howto 1. Find all the commits that were supposed to be on your PR git log • git reflog •
  • 55. #howto 2. Sync your master with the server and create a brand new branch from it git checkout master git pull git checkout -b new-branch
  • 56. #howto 3.Apply the commits git cherry-pick id1 id2 id3...
  • 57. #howto 4. Replace the old branch with the new one (on local and remote repos) git checkout master git branch -D my-branch git branch -m new-branch my-branch git checkout my-branch git push –-set-upstream origin my-branch --force
  • 58. Life is good again!
  • 59. Conclusion (1) You don’t need to know everything about git, but it’s a good idea to have a solid understanding of some concepts (e.g.: commit, branch, merge, rebase)
  • 60. Conclusion (2) As long as it was committed, there is always a way to recover your work.
  • 61. Avoiding push accidents Image © CAPCOM Ltd..All rights Reserved
  • 62. Don’t use the --force, Luke Make a habit of trying “vanilla” git push before adding any arguments (e.g., origin my-branch or --force)
  • 63. On a new branch $ git checkout -b new-branch Switched to a new branch 'new-branch' $ git commit -am "my changes" [new-branch eabe2c9] my changes 1 file changed, 2 insertions(+) $ git push fatal: The current branch new-branch has no upstream branch To push the current branch and set the remote as upstream, use git push --set-upstream origin new-branch $ git push --set-upstream origin new-branch
  • 64. After a rebase $ git rebase -i master Switched to a new branch 'new-branch' $ git push To github.com:Shopify/some-repo.git ! [rejected] new-branch -> new-branch (non-fast-forward) error: failed to push some refs to 'github.com:Shopify/ some-repo.git' hint: Updates were rejected because the tip of your current branch is behind $ git push --force
  • 65. Know where you are Always show the current branch on your command line prompt (bash): export PS1="h:W u[033[32m] $(__git_ps1)[033[00m]$ " MyComputer:myproject me (my-branch)$
  • 66. GUI x CLI Image CC BY-SAWikipedia user Sandstein
  • 67. gitk
  • 69. Tig
  • 70.
  • 71. Stay fresh Update your master to the latest remote without leaving your branch git pull --rebase --autostash For git < 2.9, use the git-up gem gem install git-up git up
  • 72. Non-brute force Rewrite history, but only if no one else added commits to the branch git push --force-with-lease
  • 73. Branch prefix shortcuts git push --delete my-branch git push --force my-branch can be shortened to git push :my-branch git push +my-branch (just be careful not to add + by habit)
  • 74. Thank you! Carlos Duarte Do Nascimento @chesterbr • http://chester.me shopify.com/careers http://slideshare.net/chesterbr
  • 75. This presentation is available under the Creative Commons “by-nc” 3.0 license (available at https://creativecommons.org/licenses/by-nc/3.0/), noticing the exceptions below. Images and text from third parties were included (with due credits wherever possible) under a fair use assumption (or, for Brazilian media, under Art. 46 of Law 9610/98) and/or under their respective licenses. Omissions are unintended and corrections welcome. Such content is excluded from the license above. GitHub, the GitHub logo and mascot are trademarks of GitHub, Inc. Shopify and the Shopify logo are trademaks of Shopify, Inc. The opinions stated here belong solely to the author, not offically representing his employer’s opinions, nor any of of the persons or companies mentioned in any extent. Credits And License