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!
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!
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.
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!
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!
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.
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 as part of my free "First Aid Kit for Git".
About the Author
Tobias Günther is the CEO of Tower, the popular Git desktop client that helps more than 100,000 developers around the world to be more productive with Git.