SlideShare ist ein Scribd-Unternehmen logo
1 von 50
Think ahead. Act now.
Think ahead. Act now.
Think ahead. Act now.
Jesse Houwing
Tinkerer, trainer, coach
@jessehouwing
xpirit.com/jesse
Think ahead. Act now.
Think ahead. Act now.
Oh Sh*t - Basics
Think ahead. Act now.
Ohh sh*t I made a typo in the commit message
git commit --amend -m “Centrum"
Think ahead. Act now.
Ohh sh*t I forgot to add a file to my commit
git add file2.txt
git commit --amend --no-edit
Think ahead. Act now.
Ohh sh*t I want to undo my change, but already
pushed it.
git revert <<commit>>
git push
Think ahead. Act now.
Ohh sh*t I totally messed up everything.
del /s /q /a /f .
git clone <your remote>
https://xkcd.com/1597/
Think ahead. Act now.
Ohh sh*t I totally messed up my repo and want to
keep my changes
Copy everything to a safe location
git clone <<your repo>>
git checkout -b fixbranch
Clear the folder
Paste the contents of your old repo (except the .git folder)
git add -A
git commit -m "Restored local changes!"
Think ahead. Act now.
Ohh sh*t I want to clean up my changes before I
share them
Think ahead. Act now.
Ohh sh*t I want to clean up my changes before I
share them
git rebase -i <<commit to start cleanup from>>
https://xkcd.com/1296/
Think ahead. Act now.
Why force pushing is BAD
It messes up the work of others
Think ahead. Act now.
Ohh Sh*t someone force pushed my parent
branch!
Think ahead. Act now.
Ohh Sh*t someone force pushed my parent
branch!
Not all is lost.
Try:
git pull –rebase
Or
git fetch origin/thatbranch
git rebase –onto origin/thatbranch
Think ahead. Act now.
Ohh Sh*t someone force pushed my parent
branch!
That failed huh?
Find the new hash <C> of the commit you last pulled
git rebase --onto origin/master <C> master
Think ahead. Act now.
A B C
master
D
origin/master
Local
A BX
master
Remote
Find B
Think ahead. Act now.
A B C
master
D
Local
BX
origin/master
Fetch origin/master
Think ahead. Act now.
A B C
master
D
Local
BX CX DX
origin/master
rebase --onto origin/master <B> master
master
Think ahead. Act now.
Ohh sh*t! Reset! Reset! Reset!
Think ahead. Act now.
Let’s dig into reset
git reset --mixed (default)
• Move back in history
• Keep all changes in the working directory area.
git reset --soft
• Move back in history
• Keeps the working directory, has all the changes staged.
git reset --hard & git clean -df
• Move back in history
• Reverts all changes
• Removes all files and folders that are not tracked by git
Think ahead. Act now.
git reset --mixed B
A B C
Stagin
g area
Work
dir
master
A B C
Stagin
g area
Work
dir
master
Think ahead. Act now.
git reset --hard B
A B C
Stagin
g area
Work
dir
master
A B C
Stagin
g area
Work
dir
master
Think ahead. Act now.
Ohh sh*t I totally messed but my repo is still
working.
Go back to the last time you pulled
git reset –hard
Go back to a specific commit
git reflog
Find the last known good commit
git reset --hard <<hash>>
Think ahead. Act now.
Ohh sh*t I committed to the wrong branch
Think ahead. Act now.
Ohh sh*t I committed to the wrong branch
git reset HEAD~1 --soft
git stash
git checkout right-branch
git stash pop
git add .
git commit -m
Think ahead. Act now.
A B C
D
right
wrong
stash
Think ahead. Act now.
stash
A B
C
D
right
wrong
Think ahead. Act now.
stash
A B
C2D
right
wrong
Think ahead. Act now.
Ohh sh*t I need to move more than one commit
Think ahead. Act now.
Ohh sh*t I need to move more than one commit
git log
Note the hashes you want to move
git checkout <<right>>
git cherry-pick <<oldest-hash>>..<<latest-hash>>
git checkout <<wrong>>
git reset --hard <<oldest-hash>>^
Think ahead. Act now.
A B C
E
right
wrong
D
Think ahead. Act now.
A B C
E
right
wrong
D
C’ D’
git checkout right
git cherry-pick C..D
Think ahead. Act now.
A
B
= C^
C
E
right
wrong
D
C’ D’
git checkout wrong
git reset --hard C^
Think ahead. Act now.
Ohh sh*t I only need a single file from another
branch
Think ahead. Act now.
This is hard
Because git assumes commits, not files.
Think ahead. Act now.
Ohh sh*t I only need a single file from another
branch
I need the file as-is, ignore other changes
git checkout other-branch -- the-file-I-need.txt
I need specific changes from the other file
git checkout --patch other-branch -- the-file-I-need.txt
git cherry-pick <<hash>>
git reset --mixed
git add <<file to keep>>
git commit -m “cherry-picked one file” <<file to commit>>
git reset --hard
Think ahead. Act now.
Ohh sh*t I need to delete all references to a file
Think ahead. Act now.
Ohh sh*t I need to delete all references to a file
On linux:
git filter-branch --index-filter 'git rm --cached --ignore-unmatch
filename' HEAD
On windows:
All examples are broken due to missing utilities, quoting issues,
powershell interference and other nonsense.
Think ahead. Act now.
Ahhh! The BFG Repo-Cleaner!
Think ahead. Act now.
Start with a fresh mirror clone
git clone --mirror git://example.com/some-big-repo.git
Think ahead. Act now.
Ohh sh*t I need to delete all references to a file
git rm <<file(s) to get rid of>>
git commit -m “remove safeties”
java -jar ..bfg-1.13.0.jar --delete-files <<files to get rid of>>
Think ahead. Act now.
Ohh sh*t I need to remove a password
git rm <<file(s) to get rid of>>
git commit -m “remove safeties”
java -jar ..bfg-1.13.0.jar --replace-text passwords.txt my-repo.git
Think ahead. Act now.
Then force your changes
git reflog expire --expire=now --all && git gc --prune=now –
aggressive
git push --all -force
git push --tags -force
Think ahead. Act now.
Important note
Services will not delete the object from their storage/database
To permanently get rid of the files for real:
CREATE A NEW REPO.
Think ahead. Act now.
It’s easy to make mistakes
And a 100 ways to solve them
Visual Studio doesn’t support them all…
Think ahead. Act now.
But we’ve seen it’s easy to
Fix the last commit
Revert a change
Move changes from one branch to another
Rewrite history
Make our co-workers miserable
Help them recover
As long as you know what you’re doing…
Think ahead. Act now.
Cheat Sheet
Think ahead. Act now.

Weitere ähnliche Inhalte

Ähnlich wie Ohh shit git

Git Started With Git
Git Started With GitGit Started With Git
Git Started With GitNick Quaranto
 
How to Really Get Git
How to Really Get GitHow to Really Get Git
How to Really Get GitSusan Tan
 
Introduction of Git
Introduction of GitIntroduction of Git
Introduction of GitWayne Chen
 
Git Basics (Professionals)
 Git Basics (Professionals) Git Basics (Professionals)
Git Basics (Professionals)bryanbibat
 
Git - (a) Gentle InTroduction
Git - (a) Gentle InTroductionGit - (a) Gentle InTroduction
Git - (a) Gentle InTroductionBruno Bossola
 
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]Cool Git Tricks (That I Learn When Things Go Badly) [1/2]
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]Carina C. Zona
 
Introduction to Git for Artists
Introduction to Git for ArtistsIntroduction to Git for Artists
Introduction to Git for ArtistsDavid Newbury
 
slides.pdf
slides.pdfslides.pdf
slides.pdfvidsvagi
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshopthemystic_ca
 
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
 

Ähnlich wie Ohh shit git (20)

Don't fear the rebase
Don't fear the rebaseDon't fear the rebase
Don't fear the rebase
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With Git
 
How to Really Get Git
How to Really Get GitHow to Really Get Git
How to Really Get Git
 
Introduction of Git
Introduction of GitIntroduction of Git
Introduction of Git
 
How to use git without rage
How to use git without rageHow to use git without rage
How to use git without rage
 
Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)
 
Git like a pro EDD18 - Full edition
Git like a pro EDD18 - Full editionGit like a pro EDD18 - Full edition
Git like a pro EDD18 - Full edition
 
Git
GitGit
Git
 
Git Basics (Professionals)
 Git Basics (Professionals) Git Basics (Professionals)
Git Basics (Professionals)
 
Working with Git
Working with GitWorking with Git
Working with Git
 
Loading...git
Loading...gitLoading...git
Loading...git
 
Git - (a) Gentle InTroduction
Git - (a) Gentle InTroductionGit - (a) Gentle InTroduction
Git - (a) Gentle InTroduction
 
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]Cool Git Tricks (That I Learn When Things Go Badly) [1/2]
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]
 
Introduction to Git for Artists
Introduction to Git for ArtistsIntroduction to Git for Artists
Introduction to Git for Artists
 
slides.pdf
slides.pdfslides.pdf
slides.pdf
 
slides.pdf
slides.pdfslides.pdf
slides.pdf
 
Git in 10 minutes
Git in 10 minutesGit in 10 minutes
Git in 10 minutes
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
 
Git Tech Talk
Git  Tech TalkGit  Tech Talk
Git Tech Talk
 
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
 

Mehr von Jesse Houwing

Trusting the Unknown
Trusting the UnknownTrusting the Unknown
Trusting the UnknownJesse Houwing
 
Azure DevOps Extension Tools
Azure DevOps Extension ToolsAzure DevOps Extension Tools
Azure DevOps Extension ToolsJesse Houwing
 
The new way to extend VSTS Build and Release
The new way to extend VSTS Build and ReleaseThe new way to extend VSTS Build and Release
The new way to extend VSTS Build and ReleaseJesse Houwing
 
Scrum workshop for Project Managers
Scrum workshop for Project ManagersScrum workshop for Project Managers
Scrum workshop for Project ManagersJesse Houwing
 
Let's get agile: An Agile Talk About Agile
Let's get agile: An Agile Talk About AgileLet's get agile: An Agile Talk About Agile
Let's get agile: An Agile Talk About AgileJesse Houwing
 
Techdays 2012 - Better code through reviews and tools
Techdays 2012 - Better code through reviews and toolsTechdays 2012 - Better code through reviews and tools
Techdays 2012 - Better code through reviews and toolsJesse Houwing
 
Techdaysnl - code review features in tfs vnext
Techdaysnl - code review features in tfs vnextTechdaysnl - code review features in tfs vnext
Techdaysnl - code review features in tfs vnextJesse Houwing
 
Growing great (agile) teams
Growing great (agile) teams Growing great (agile) teams
Growing great (agile) teams Jesse Houwing
 

Mehr von Jesse Houwing (11)

Trusting the Unknown
Trusting the UnknownTrusting the Unknown
Trusting the Unknown
 
Azure DevOps Extension Tools
Azure DevOps Extension ToolsAzure DevOps Extension Tools
Azure DevOps Extension Tools
 
Ohh sh*t git
Ohh sh*t gitOhh sh*t git
Ohh sh*t git
 
Dress up my VSTS
Dress up my VSTSDress up my VSTS
Dress up my VSTS
 
The new way to extend VSTS Build and Release
The new way to extend VSTS Build and ReleaseThe new way to extend VSTS Build and Release
The new way to extend VSTS Build and Release
 
When scrum goes bad
When scrum goes badWhen scrum goes bad
When scrum goes bad
 
Scrum workshop for Project Managers
Scrum workshop for Project ManagersScrum workshop for Project Managers
Scrum workshop for Project Managers
 
Let's get agile: An Agile Talk About Agile
Let's get agile: An Agile Talk About AgileLet's get agile: An Agile Talk About Agile
Let's get agile: An Agile Talk About Agile
 
Techdays 2012 - Better code through reviews and tools
Techdays 2012 - Better code through reviews and toolsTechdays 2012 - Better code through reviews and tools
Techdays 2012 - Better code through reviews and tools
 
Techdaysnl - code review features in tfs vnext
Techdaysnl - code review features in tfs vnextTechdaysnl - code review features in tfs vnext
Techdaysnl - code review features in tfs vnext
 
Growing great (agile) teams
Growing great (agile) teams Growing great (agile) teams
Growing great (agile) teams
 

Kürzlich hochgeladen

Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
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
 
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
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
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
 
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
 
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
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
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
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
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
 
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
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
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
 

Kürzlich hochgeladen (20)

Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
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...
 
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
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
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
 
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
 
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
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
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...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
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
 
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
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
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 - ...
 

Ohh shit git

  • 2. Think ahead. Act now. Think ahead. Act now. Jesse Houwing Tinkerer, trainer, coach @jessehouwing xpirit.com/jesse
  • 4. Think ahead. Act now. Oh Sh*t - Basics
  • 5.
  • 6. Think ahead. Act now. Ohh sh*t I made a typo in the commit message git commit --amend -m “Centrum"
  • 7. Think ahead. Act now. Ohh sh*t I forgot to add a file to my commit git add file2.txt git commit --amend --no-edit
  • 8. Think ahead. Act now. Ohh sh*t I want to undo my change, but already pushed it. git revert <<commit>> git push
  • 9. Think ahead. Act now. Ohh sh*t I totally messed up everything. del /s /q /a /f . git clone <your remote> https://xkcd.com/1597/
  • 10. Think ahead. Act now. Ohh sh*t I totally messed up my repo and want to keep my changes Copy everything to a safe location git clone <<your repo>> git checkout -b fixbranch Clear the folder Paste the contents of your old repo (except the .git folder) git add -A git commit -m "Restored local changes!"
  • 11. Think ahead. Act now. Ohh sh*t I want to clean up my changes before I share them
  • 12. Think ahead. Act now. Ohh sh*t I want to clean up my changes before I share them git rebase -i <<commit to start cleanup from>> https://xkcd.com/1296/
  • 13. Think ahead. Act now. Why force pushing is BAD It messes up the work of others
  • 14. Think ahead. Act now. Ohh Sh*t someone force pushed my parent branch!
  • 15. Think ahead. Act now. Ohh Sh*t someone force pushed my parent branch! Not all is lost. Try: git pull –rebase Or git fetch origin/thatbranch git rebase –onto origin/thatbranch
  • 16. Think ahead. Act now. Ohh Sh*t someone force pushed my parent branch! That failed huh? Find the new hash <C> of the commit you last pulled git rebase --onto origin/master <C> master
  • 17. Think ahead. Act now. A B C master D origin/master Local A BX master Remote Find B
  • 18. Think ahead. Act now. A B C master D Local BX origin/master Fetch origin/master
  • 19. Think ahead. Act now. A B C master D Local BX CX DX origin/master rebase --onto origin/master <B> master master
  • 20. Think ahead. Act now. Ohh sh*t! Reset! Reset! Reset!
  • 21. Think ahead. Act now. Let’s dig into reset git reset --mixed (default) • Move back in history • Keep all changes in the working directory area. git reset --soft • Move back in history • Keeps the working directory, has all the changes staged. git reset --hard & git clean -df • Move back in history • Reverts all changes • Removes all files and folders that are not tracked by git
  • 22. Think ahead. Act now. git reset --mixed B A B C Stagin g area Work dir master A B C Stagin g area Work dir master
  • 23. Think ahead. Act now. git reset --hard B A B C Stagin g area Work dir master A B C Stagin g area Work dir master
  • 24. Think ahead. Act now. Ohh sh*t I totally messed but my repo is still working. Go back to the last time you pulled git reset –hard Go back to a specific commit git reflog Find the last known good commit git reset --hard <<hash>>
  • 25. Think ahead. Act now. Ohh sh*t I committed to the wrong branch
  • 26.
  • 27. Think ahead. Act now. Ohh sh*t I committed to the wrong branch git reset HEAD~1 --soft git stash git checkout right-branch git stash pop git add . git commit -m
  • 28. Think ahead. Act now. A B C D right wrong stash
  • 29. Think ahead. Act now. stash A B C D right wrong
  • 30. Think ahead. Act now. stash A B C2D right wrong
  • 31. Think ahead. Act now. Ohh sh*t I need to move more than one commit
  • 32. Think ahead. Act now. Ohh sh*t I need to move more than one commit git log Note the hashes you want to move git checkout <<right>> git cherry-pick <<oldest-hash>>..<<latest-hash>> git checkout <<wrong>> git reset --hard <<oldest-hash>>^
  • 33. Think ahead. Act now. A B C E right wrong D
  • 34. Think ahead. Act now. A B C E right wrong D C’ D’ git checkout right git cherry-pick C..D
  • 35. Think ahead. Act now. A B = C^ C E right wrong D C’ D’ git checkout wrong git reset --hard C^
  • 36. Think ahead. Act now. Ohh sh*t I only need a single file from another branch
  • 37. Think ahead. Act now. This is hard Because git assumes commits, not files.
  • 38. Think ahead. Act now. Ohh sh*t I only need a single file from another branch I need the file as-is, ignore other changes git checkout other-branch -- the-file-I-need.txt I need specific changes from the other file git checkout --patch other-branch -- the-file-I-need.txt git cherry-pick <<hash>> git reset --mixed git add <<file to keep>> git commit -m “cherry-picked one file” <<file to commit>> git reset --hard
  • 39. Think ahead. Act now. Ohh sh*t I need to delete all references to a file
  • 40. Think ahead. Act now. Ohh sh*t I need to delete all references to a file On linux: git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD On windows: All examples are broken due to missing utilities, quoting issues, powershell interference and other nonsense.
  • 41. Think ahead. Act now. Ahhh! The BFG Repo-Cleaner!
  • 42. Think ahead. Act now. Start with a fresh mirror clone git clone --mirror git://example.com/some-big-repo.git
  • 43. Think ahead. Act now. Ohh sh*t I need to delete all references to a file git rm <<file(s) to get rid of>> git commit -m “remove safeties” java -jar ..bfg-1.13.0.jar --delete-files <<files to get rid of>>
  • 44. Think ahead. Act now. Ohh sh*t I need to remove a password git rm <<file(s) to get rid of>> git commit -m “remove safeties” java -jar ..bfg-1.13.0.jar --replace-text passwords.txt my-repo.git
  • 45. Think ahead. Act now. Then force your changes git reflog expire --expire=now --all && git gc --prune=now – aggressive git push --all -force git push --tags -force
  • 46. Think ahead. Act now. Important note Services will not delete the object from their storage/database To permanently get rid of the files for real: CREATE A NEW REPO.
  • 47. Think ahead. Act now. It’s easy to make mistakes And a 100 ways to solve them Visual Studio doesn’t support them all…
  • 48. Think ahead. Act now. But we’ve seen it’s easy to Fix the last commit Revert a change Move changes from one branch to another Rewrite history Make our co-workers miserable Help them recover As long as you know what you’re doing…
  • 49. Think ahead. Act now. Cheat Sheet

Hinweis der Redaktion

  1. Xpirit Community noemen
  2. Nicely explained here: https://stackoverflow.com/questions/14787776/how-to-pull-after-a-forced-git-push
  3. https://rtyley.github.io/bfg-repo-cleaner/