%%
date:: [[2023-04-11]]
parent::
%%
# [[Undo options for when you stuff things up in Git]]
When you inevitably stuff things up in [[Git]] and are at the point where you just want to blow away whatever local changes you have and take what's on the remote, here are some things you can try.
## Stash
Maybe you forgot to `pull` before making changes. To temporarily undo the *uncommitted staged and unstaged* changes you've made, try stashing:
```bash
git stash
```
That command locally saves the changes you've made safely, but also lets you fall back to match the `HEAD` (latest) commit. Then you can do whatever you need to do, like `pull`, and then you can decide whether you want to apply the changes you made on top of that.
If you do want to make those changes again, do one of the following:
```bash
git stash pop
```
`pop` applies the changes and blows away the stash (because it's "popped" now). If you want to keep the stashed changes "saved", try this instead:
```bash
git stash apply
```
`apply` applies the changes on top of the working copy but then also keeps the stash. Could be good if you want to apply those changes to multiple branches, for example.
## Pull
So the stashing trick didn't work? You've probably tried `pull` as well. Time to get more destructive. Try this:
```bash
git pull --force
```
The `--force` flag pulls down the contents of the remote repository while ignoring any conflicts with local copies. This definitely overwrites stuff you've changed locally, but that's why you're here, right?
## Revert
OK, maybe you're lucky and you just stuffed up in the last commit. Try:
```bash
git revert HEAD
```
The `HEAD` part refers to the last commit. This is kind of like an "undo", but not-- it's actually better, because it instead creates a *new* commit that reverses the last commit, so you still keep the history of your stuff up.
You can also do:
```bash
git revert HEAD~3
```
if you want to revert the fourth last commit or you can pass the exact commit you want to revert like this:
```bash
git revert <SHA>
```
When you do this, reverting doesn't "undo" all the commits after the one that you specified-- it's smarter than that, so it tries to just reverse the specific changes in that commit and it leaves the rest of it alone.
Reverting is less destructive than resetting. But if you're still reading, then it's time to try resets.
## Reset
Every commit on git has a [[SHA]] hash that identifies it. Go and find the right one you want to fall back to-- ideally one *before* you stuffed everything up. Try these:
- The commit history on GitHub
- `git blame` if you're trying to find out when a particular file was touched and by whom - this can help you track down the commit that broke stuff (it was probably you)
- [[Fork (app)]], [[GitHub Desktop]], or [[Atlassian SourceTree]] to go through the commits visually.
Be super sure about which commit you want to fall back to, because you're definitely going to be blowing away *all commits made after that point*. Take backups if you have to.
Then go for it:
```bash
git reset <SHA> --hard
```
## The nuclear option
If you're still reading, you know what? If you're sure you want to definitely delete everything locally, just delete the whole damn local repo and do a fresh `git clone`. Sometimes it's better than trying to fix stuff.