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
Ohh shit git
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
Ohh shit git
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.

More Related Content

KEY
Git Magic: Versioning Files like a Boss
PDF
git. WTF is it doing anyway?
KEY
Git Distributed Version Control System
PDF
Git - tips and tricks
PDF
Fixup and autosquash
PDF
Get on with git
PDF
Essential git fu for tech writers
PDF
git in 5 slides
Git Magic: Versioning Files like a Boss
git. WTF is it doing anyway?
Git Distributed Version Control System
Git - tips and tricks
Fixup and autosquash
Get on with git
Essential git fu for tech writers
git in 5 slides

Similar to Ohh shit git (20)

PDF
Don't fear the rebase
PDF
Git Started With Git
PDF
How to Really Get Git
ODP
Introduction of Git
ODP
How to use git without rage
ODP
Introduction to Git (Greg Lonnon)
PPTX
Git like a pro EDD18 - Full edition
PPTX
PDF
Git Basics (Professionals)
KEY
Working with Git
PDF
Loading...git
ODP
Git - (a) Gentle InTroduction
PDF
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]
PDF
Introduction to Git for Artists
PDF
slides.pdf
PDF
slides.pdf
PPTX
Git in 10 minutes
PPTX
Introduction To Git Workshop
KEY
Git Tech Talk
PDF
Git Tricks — git utilities that make life git easier
Don't fear the rebase
Git Started With Git
How to Really Get Git
Introduction of Git
How to use git without rage
Introduction to Git (Greg Lonnon)
Git like a pro EDD18 - Full edition
Git Basics (Professionals)
Working with Git
Loading...git
Git - (a) Gentle InTroduction
Cool Git Tricks (That I Learn When Things Go Badly) [1/2]
Introduction to Git for Artists
slides.pdf
slides.pdf
Git in 10 minutes
Introduction To Git Workshop
Git Tech Talk
Git Tricks — git utilities that make life git easier
Ad

More from Jesse Houwing (11)

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

Recently uploaded (20)

PDF
MCP Security Tutorial - Beginner to Advanced
PPTX
assetexplorer- product-overview - presentation
PDF
AI Guide for Business Growth - Arna Softech
PDF
Cost to Outsource Software Development in 2025
PDF
Microsoft Office 365 Crack Download Free
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Types of Token_ From Utility to Security.pdf
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
Visual explanation of Dijkstra's Algorithm using Python
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PPTX
Tech Workshop Escape Room Tech Workshop
PDF
Website Design Services for Small Businesses.pdf
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PDF
DNT Brochure 2025 – ISV Solutions @ D365
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
MCP Security Tutorial - Beginner to Advanced
assetexplorer- product-overview - presentation
AI Guide for Business Growth - Arna Softech
Cost to Outsource Software Development in 2025
Microsoft Office 365 Crack Download Free
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Types of Token_ From Utility to Security.pdf
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
GSA Content Generator Crack (2025 Latest)
Visual explanation of Dijkstra's Algorithm using Python
Oracle Fusion HCM Cloud Demo for Beginners
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Tech Workshop Escape Room Tech Workshop
Website Design Services for Small Businesses.pdf
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
DNT Brochure 2025 – ISV Solutions @ D365
How to Use SharePoint as an ISO-Compliant Document Management System

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
  • 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
  • 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

Editor's Notes

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