Home > Software design >  Ran 'git checkout HEAD .' in the wrong directory
Ran 'git checkout HEAD .' in the wrong directory

Time:05-11

Oops, I accidentally ran 'get checkout HEAD .' in the wrong directory. It produced the output 'Updated 2 paths from 2096756f' but I have no idea what my local changes were. I assume that I can't find out, but I am wondering how I can protect myself in the future as this command is how I habitually discard local changes. Is there some alias I can create that for this that would protect me from such a mistake in the future?

CodePudding user response:

I accidentally ran 'get checkout HEAD .' in the wrong directory.

This is bad.

It produced the output 'Updated 2 paths from 2096756f' but I have no idea what my local changes were.

And unless you have local backups (zfs snapshots, macOS Time Machine, etc.), they're just gone. Git can't help you get them back: you told Git to discard them from your working tree, and it did.

I assume that I can't find out,

If your OS keeps modification times on files, look for the two most-recently-modified files. Those will be the two that git checkout HEAD . overwrote.

I am wondering how I can protect myself in the future as this command is how I habitually discard local changes.

Change your habits. You can't alias a built in Git command. You can make a git alias or git command of your own that:

  1. checks to see if you're running something "safe", or checks to see if you're using this particular command;
  2. if you're running something you've deemed "safe", runs the real Git command with the same arguments, but if you're running git checkout HEAD ., complains and doesn't do that.

Since you're writing this command-or-alias yourself, you get to choose what goes in the "but" section of step 2. This will (presumably) get you to change your habits.

(I try to use git status and then git restore -- path/to/file these days, so that I know just what I'm restoring; I can cut-and-paste the file name from git status here.)

CodePudding user response:

Is there some alias I can create that for this that would protect me from such a mistake in the future

No. checkout is dangerous and Git will not protect you from yourself.

But an excellent protection is to abandon the use of checkout. Instead use switch and restore. They make it much harder to make a harmful mistake.

  •  Tags:  
  • git
  • Related