12 Ways to Undo Mistakes with Git - Part 2

Tobias Günther - Oct 6 '20 - - Dev Community

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 second part, you'll find another set of 6 short & handy videos that help you undo mistakes with Git! (Take a look at part 1 if you missed it!)


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!

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!

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!

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!

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!

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!


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, you can download all 12 cases + 5 extra episodes and 1 cheat sheet as part of my free "First Aid Kit for Git".

first-aid-kit


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.

. . . . . . . . . . . . . . . . . .
Terabox Video Player