Submit

How to change the message on last commit?

· OhShitGit · Mistakes Correction
$ git commit --amend
# follow prompts to change the commit message

Stupid commit message formatting requirements.

How to move accidentally committed changes to a brand new branch

· OhShitGit · Acidents Recovery
# create a new branch from the current state of master
$ git branch some-new-branch-name
# this is were your commit will live :)
# now go back to master
$ git checkout master
# remove the commit from the master branch
$ git reset HEAD~ --hard
# whoola!! you are back on track

the other way of doing this:

# 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

A lot of people suggest using cherry-pick for this situation too, so take your pick on whatever one makes the most sense to you!

yet another way:

$ 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

How to revert last X commits

· OhShitGit · Mistakes Correction
$ git reflog
# you will see a list of every thing you've done in git, across all branches!
# each one has an index [email protected]{index}
# find the one before you broke everything
$ git reset [email protected]{index}
# magic time machine

You can use this to get back stuff you accidentally deleted, or just to remove some stuff you tried that broke the repo.

How to run a diff on unstagged changes?

· OhShitGit · Solving Mistories
$ git diff --staged

Git won't do a diff of files that have been add-ed to your staging area without this flag. File under ¯(ツ)/¯ (yes, this is a feature, not a bug, but it's baffling and non-obvious the first time it happens to you!)

Want to Contribute?

Send me an Email, ping me on Twitter or submit a pull request on Github.

Sign up below to receive updates, tips and more handy tutorials when git screws up. It’s good stuff & I don’t spam.