Skip to content

Instantly share code, notes, and snippets.

@j33ty
Created May 14, 2019 08:40
Show Gist options
  • Save j33ty/2213aceeeebe909a04897c45e0bd845e to your computer and use it in GitHub Desktop.
Save j33ty/2213aceeeebe909a04897c45e0bd845e to your computer and use it in GitHub Desktop.
git-oh-shit.sh
################
# I did something terribly wrong, please tell me git has a magic time machine!
git reflog
# You will see a list of every thing you've done in git, across all branches!
# Each one has an index HEAD@{index}
# Find the one before you broke everything
git reset HEAD@{index}
# Magic time machine
################
# I committed and immediately realized I need to make one small change!
# Make your change
git add . # or add individual files
git commit --amend
# Follow prompts to change or keep the commit message
# Now your last commit contains that change!
################
# I need to change the message on my last commit!
git commit --amend
# Follow prompts to change the commit message
################
# I accidentally committed something to master that should have been on a brand new branch
# Create a new branch from the current state of master
git branch some-new-branch-name
# Remove the commit from the master branch
git reset HEAD~ --hard
git checkout some-new-branch-name
# Your commit lives in this branch now
################
# Git diff including staged files
git diff --staged
################
# I accidentally committed to the wrong branch!
# Method 1
# Undo the last commit, but leave the changes available
git reset HEAD~ --soft
git stash
# Move to the correct branch
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here"
# Now your changes are on the correct branch
# Method 2
# Use cherry-pick
git checkout name-of-the-correct-branch
# Grab the last commit to master
git cherry-pick master
# Delete it from master
git checkout master
git reset HEAD~ --hard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment