-
Notifications
You must be signed in to change notification settings - Fork 3
5. Need to undo something?
We've all been there. Here are some tips for undoing things via git.
Most of my advice can be summed up in the form of this amazing website, all code examples take from here: Dangit, Git?!
First and foremost, it's important to be following git best practices in order to be able to undo your mistakes. If you've been committing to the master branch or making very large commits, you're gonna have a bad time.
With that said, I know how hard it can be to remember to follow best practices at all times. Thankfully, git is pretty handy and most things can be undone, even if you forget to create a new branch. A few tips:
It's much easier to delete an entire branch when things go south than to roll back commits. If you start working on the master branch and realize you should've been on a new one, no big deal!
# create a new branch from the current state of master
git branch some-new-branch-name
# remove the last commit from the master branch
git reset HEAD~ --hard
git checkout some-new-branch-name
# your commit lives in this branch now :)
While reverting commits is pretty easy, reverting to a specific time in history is much easier when commits are small and frequent.
To go back to specific commit:
# find the commit you need to undo
git log
# use the arrow keys to scroll up and down in history (or check github)
# once you've found your commit, save the hash
git revert [saved hash]
# git will create a new commit that undoes that commit
# follow prompts to edit the commit message
# or just save and commit
This works even if you've made multiple commits since that one!
If, however, your commit was quite large and changed multiple files, you can use this set of commands to go undo just the file in question:
# find a hash for a commit before the file was changed
git log
# use the arrow keys to scroll up and down in history
# once you've found your commit, save the hash
git checkout [saved hash] -- path/to/file
# the old version of the file will be in your index
git commit -m "Wow, you don't have to copy-paste to undo"
Writing good commit messages is more of an art than a science. Reading through git log that looks like "trying this thing... oops that didn't work... trying again... I hope this works!" will not help you when it comes time to revert commits.
If you're working on a feature, try to use present tense and describe what changed: "remove link from header" and "refactor addIt function" are good examples. On our team, if you're committing a grunt build, include "dev build: [message]" or "live build: [message]." Note: the live build should almost always be its own commit!
In case you realize after making a commit that you need to fix your message, this one's for you:
git commit --amend
# follow prompts to change the commit message
Try to use all the above methods before resetting the HEAD. Git reflog is a magic time machine and will fix just about anything, but having a record of all your changes is very useful and can come in handy down the road!
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
Did something make it to production that shouldn't have? No time to figure out what happened and you just need to undo it, pronto? Use github to undo the latest PR: Reverting a pull request
Git is vast and changes its features from time to time. For example, they're rolling out an experimental feature to use new terminology instead of "checkout": Experimental alternatives to git checkout
I've only covered the undo's I use most during my regular workflow. To see more, definitely check out Dangit, Git?! or google what you're trying to do! If you've never used the feature before, I highly recommend trying it on a new branch before nuking all your work. 🤣 Good luck!