August 2019 by Tobias Günther

17 Ways to Undo Mistakes with Git

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. Here are 17 short & handy videos that help you learn how to save your neck!

The videos on this page demonstrate how to undo things both on the Command Line and via the Tower Git client. If you're interested in learning these workflows only in Tower, you can access the complete video series - including the bonus episodes - on our help platform.


#1 - 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!



#2 - Restoring a Deleted File

Apply in cases of: "Heavens above! 😳 Why did I delete that?!!"
Disaster summary: You've deleted a file in good faith. But well... you shouldn't have... Since you haven't committed the deletion to the repository, however, it's quite quick and easy to restore that deleted file!



#3 - 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!



#4 - 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.



#5 - 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!

This episode is included in the video package. Download it for free below!



#6 - 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!



#7 - 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.



#8 - Resetting a File to an Old Revision

Apply in cases of: "Yikes! 🤔 I know this file was correct sometime!"

Disaster summary: Not everything was better in the old days! Just this one particular file, that you now long to restore from a previous revision. Here's how!



#9 - Recovering Deleted Commits

Apply in cases of: "Sweet baby Jesus! 😱 Did I just delete the wrong commits?!!"

Disaster summary: Using "git reset" is a wonderful way to get rid of some unwanted commits. But it's also a wonderful way to get yourself into trouble - if you got rid of the wrong commits! But chin up! You can undo even that!



#10 - Recovering a Deleted Branch

Apply in cases of: "God Almighty! 😵 Did I just delete the wrong branch?!!"

Disaster summary: You were absolutely sure that you wouldn't need a certain branch (and all its valuable commits) anymore when you deleted it. But on second thought... let's see if we can restore it!

This episode is included in the video package. Download it for free below!



#11 - Moving a Commit to a New Branch

Apply in cases of: "Damn it! 😣 I should have created a new branch before committing!"

Disaster summary: You've worked busily on a new feature and solemnly made the first commit - although you haven't created a separate feature branch for it. Let's get that new feature commit off of the "master" branch and move it to its own feature branch!



#12 - Moving a Commit to a Different Branch

Apply in cases of: "Ay, caramba! 🤨 I should have committed this to a different branch!"

Disaster summary: It happens to the best: you thought you were on the right branch, but you really weren't. Here's how to move your commit from the wrong branch to the right one.



#13 - Editing Old Commit Messages

Apply in cases of: "Christ on a cracker! 😱 Did I write this commit message?!"

Disaster summary: Sometimes you hit RETURN and notice instantly that you just made a typo in your commit message. No problem: Git's "amend" feature is here to help! Other times, however, it takes days and weeks before you notice your spelling mistakes - and yet there's still a way to save your good reputation!



#14 - Deleting Old Commits

Apply in cases of: "Knock me over with a feather! 🤔 Did I create that commit?!"

Disaster summary: Buried deep inside your commit history, there's a commit that just shouldn't be there. Let's make it look like it never happened!

This episode is included in the video package. Download it for free below!



#15 - Squashing Multiple Commits Into One

Apply in cases of: "Holy cow! 🐮 Why did I create so many commits??"

Disaster summary: Committing in small, granular doses is actually a virtue. But of course there can be "too much of a good thing". If you've overdone it and want to combine multiple commits into one: here's how!



#16 - Adding a Change to an Old Commit

Apply in cases of: "Jiminy Cricket! 🤭 I forgot to add a change!"

Disaster summary: There's this commit. It's almost perfect. But not quite. So let's add the missing change or correct the little mistake - and make it look like the commit was perfect from the beginning!



#17 - Splitting / Editing an Old Commit

Apply in cases of: "Great Ceasar! 🤕 Can I retry this commit once more?"

Disaster summary: What if you could crack that existing old commit open and change it any way you like? Add more changes, discard others, and make as many commits from it as you want. Help yourself: here's how this works!

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.

To help your memory, we prepared a nice package for you to download:

  • All 17 use cases from above...
  • + 1 extra episode on "Dealing with Merge Conflicts"
  • + 1 handy cheat cheet on "Undoing Things in Git"

Simply sign up for our (free) newsletter below and we'll send you the download link straight to your inbox!


Your Download is in Progress…

Giveaways. Cheat Sheets. eBooks. Discounts. And great content from our blog!