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
#2 - Restoring a Deleted File
#3 - Discarding Chunks / Lines in a File
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
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
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!
4 Bonus Videos + 1 Cheat Sheet
Get 18 video episodes and a handy cheat sheet - all free! Simply sign up for our newsletter and download the videos!
#6 - Reverting a Commit in the Middle
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
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
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
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
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!
4 Bonus Videos + 1 Cheat Sheet
Get 18 video episodes and a handy cheat sheet - all free! Simply sign up for our newsletter and download the videos!
#11 - Moving a Commit to a New Branch
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
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
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
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!
4 Bonus Videos + 1 Cheat Sheet
Get 18 video episodes and a handy cheat sheet - all free! Simply sign up for our newsletter and download the videos!
#15 - Squashing Multiple Commits Into One
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
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
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!
4 Bonus Videos + 1 Cheat Sheet
Get 18 video episodes and a handy cheat sheet - all free! Simply sign up for our newsletter and download the videos!