12 Ways to Undo Mistakes with Git - Part 1
Tags: git, github, webdev
Published: 2020-09-29 15:30:49

Accidentally deleting files... Making typos in your commit messages... Committing on the wrong branch... a lot of mistakes happen when humans write code! But do not despair: Git offers countless tools to undo and recover from small and big mishaps.

In this first part, you'll find 6 short & handy videos that help you learn how to save your neck! _(Stay tuned for the second part with the remaining 6 cases!)_

---

# Discarding All Local Changes in a File
**Apply in cases of:** _"Oh my goodness! 🤔 When did I write that?!!"_

**Disaster summary:** Let's not sugarcoat this: you've had better days. You've worked on a certain file and wish you hadn't. But at least, you haven't committed your changes yet - so let's go ahead and discard all local changes in that file!

{% youtube Scuxvc22nyE %}


# Discarding Chunks / Lines in a File
**Apply in cases of:** _"Mama mia! 🙄 Was that me?!!"_

**Disaster summary:** It's not that you messed up completely. It's just that some parts of your changes aren't as brilliant as they seemed at first. That's no reason to throw the baby out with the bath water: let's keep the changes that make sense and discard those that don't!

{% youtube tVlzUHdFRNo %}


# Discarding All Local Changes
**Apply in cases of:** _"Bloody hell! 🙈 Let’s start over!"_

**Disaster summary:** You had a feeling this morning, and it was correct: you should have stayed in bed. Because you clearly coded yourself into a dead-end street. So let's just discard all uncommitted work and return the project to its last committed state.

{% youtube yjntstLz0qI %}


# Fixing the Last Commit
**Apply in cases of:** _"Holy moly! 😤 Too quick with that commit…"_

**Disaster summary:** Word is on the street that you commit faster than your shadow. While you normally take that as a compliment, in this case it's more of a problem: because you littered the last commit's message with typos. Let's clean this up and make it so that no one ever hears about this!

{% youtube BhZYNfxGDAg %}


# Reverting a Commit in the Middle
**Apply in cases of:** _"Deary me! 😩 How can I get rid of that commit?"_

**Disaster summary:** Recently, it became clear that you and your old friend, commit a4f780b, don't get along so well anymore. Unfortunately, he's been in your code base for quite a while now. So instead of "deleting" him from your commit history, let's neutralize him in a friendly way: by creating a new commit that reverts his effects!

{% youtube SS4W8WbiaCE %}


# Resetting to an Old Revision
**Apply in cases of:** _"Good gravy! 😖 I wish I could turn back time…"_

**Disaster summary:** There was a time when everything was wonderful in your repository, when everything worked. Here's to these good old days! Luckily, you can turn back time and reset your project to an old revision.

{% youtube JTjoaH-kkGc %}

---

It's probably quite obvious by now: **Git is an excellent safety net!** You can undo and repair almost anything! The tricky part is mostly just knowing the corresponding workflows and commands.

In the second part of this little series, I'll show you 6 more cases.

If you don't want to wait, you can [download all 12 cases + **5 extra episodes and 1 cheat sheet**](https://www.git-tower.com/learn/git/first-aid-kit?utm_source=devto&utm_medium=guestpost&utm_campaign=12-ways-to-undo-mistakes) as part of my free **["First Aid Kit for Git"](https://www.git-tower.com/learn/git/first-aid-kit?utm_source=devto&utm_medium=guestpost&utm_campaign=12-ways-to-undo-mistakes)**.

[![first-aid-kit](https://dev-to-uploads.s3.amazonaws.com/i/f9gfafqhh1d8rqgznrjj.png)](https://www.git-tower.com/learn/git/first-aid-kit?utm_source=devto&utm_medium=guestpost&utm_campaign=12-ways-to-undo-mistakes)

---

## About the Author
[Tobias Günther](https://twitter.com/gntr) is the CEO of [Tower](https://www.git-tower.com/?utm_source=devto&utm_medium=guestpost&utm_campaign=12-ways-to-undo-mistakes), the popular Git desktop client that helps more than 100,000 developers around the world to be more productive with Git.