mantus.ai

EVERYBODY DESERVES A SEAT AT THE AI TABLE

How do I fix mistakes and recover lost work?

Undo changes, restore deleted files, and get back to any previous state. Turn Git's safety net into your confidence.

You've learned Git's core workflow, but mistakes happen. Files get deleted, changes break things, or you commit something you wish you hadn't. Git's safety net is what makes it powerful. Time to turn that safety into confidence.

Getting back deleted files

When you accidentally delete a file, Git remembers it. Use git checkout HEAD -- filename to restore any file to its last committed state:

git checkout HEAD -- important-file.txt

The HEAD tells Git to grab the file from your most recent commit. The -- separates the command from the filename, preventing confusion if your filename looks like a Git option.

If you deleted multiple files, restore them all at once:

git checkout HEAD -- .

That dot means "everything in this directory." Git will restore any deleted tracked files but leave your other changes alone.

Undoing your last commit

Made a commit too early? The --amend flag lets you fix your most recent commit:

git commit --amend -m "Better commit message"

This replaces your last commit entirely. You can also add files you forgot:

git add forgotten-file.txt
git commit --amend --no-edit

The --no-edit keeps your original commit message. Think of amend as a do-over for your most recent commit.

Warning: Only amend commits you haven't pushed yet. Amending changes the commit ID, which confuses other people if they already have your original commit.

Going back in time safely

Sometimes you need to see your project as it was yesterday or last week. Use git checkout with a commit hash:

git checkout abc1234

Git will move all your files back to that point in time. Your terminal will warn you're in "detached HEAD state." Don't panic. This means you're looking at history, not working on your current branch.

Look around, test things, but don't make commits here. When you're done exploring, return to your current work:

git checkout main

Replace main with whatever your main branch is called.

Undoing specific commits

When you need to undo a commit from your history without losing everything after it, use git revert:

git revert abc1234

This creates a new commit that undoes the changes from commit abc1234. Your history stays intact, but the unwanted changes disappear. It's like adding a commit that says "nevermind about that last thing."

For multiple commits, revert them in reverse order (newest first):

git revert def5678
git revert abc1234

The nuclear option: reset

git reset is powerful and dangerous. It moves your branch pointer backward, potentially losing commits forever. Three levels exist:

git reset --soft HEAD~1 undoes your last commit but keeps all changes staged, ready to commit again.

git reset --mixed HEAD~1 (the default) undoes your last commit and unstages the changes, but keeps them in your working directory.

git reset --hard HEAD~1 undoes your last commit and throws away all changes. This can't be undone easily.

The HEAD~1 means "one commit before HEAD." Use HEAD~2 for two commits back, and so on.

When things go really wrong

Git keeps a log of everything you do called the reflog. Even if you reset too hard or delete the wrong thing, the reflog remembers:

git reflog

This shows every action that moved your HEAD pointer. Find the commit hash where things were working and reset to it:

git reset --hard abc1234

The reflog keeps entries for 90 days by default. As long as you committed your work at some point, Git probably still has it.

Unstaging changes

Added the wrong files to staging? Remove them without losing your changes:

git restore --staged filename.txt

Or unstage everything:

git restore --staged .

Your changes stay in your working directory, just not staged for the next commit.

Git's safety comes from its obsessive record keeping. Every commit, every action gets remembered. The commands above turn that memory into practical rescue tools. Practice them on throwaway projects until they feel natural. When the real emergencies hit, you'll know exactly how to get your work back.