1

I have gotten into the bad habit of using git reset --hard regularly.

I now discovered that there is git reset --keep, and if I really need to delete something I could even do git stash && git stash drop.

So I'd like to either disable hard resets, or make them ask before they delete uncommitted changes irrecoverably, or at least create a backup. Can this be done without wrapping git in a shell function?

xeruf
  • 1,924
  • 1
  • 17
  • 39
  • I think, it's [not possible](https://stackoverflow.com/a/25886149) without a script... Maybe you can use an alias to create a different command which you would prefer over the already learned one And don't forget, you have always the option of `git reflog` and reset at least to some commited state... – kapsiR Oct 09 '20 at 22:48
  • Yeah I'm all familiar with that, but hard resetting unstaged changes will irreversibly delete them ^^ – xeruf Oct 12 '20 at 10:05

2 Answers2

2

git does not intrinsically support what you're asking for.

You could write a script or Bash function called git and put that ahead of git in your PATH. The script or function would just check for "forbidden commands" and otherwise run the regular git program (e.g. /usr/bin/git).

John Zwinck
  • 223,042
  • 33
  • 293
  • 407
  • It was clearly stated: `Can this be done without wrapping git in a shell function?` – kapsiR Oct 09 '20 at 22:53
  • 1
    @kapsiR: Yes, it can be done by wrapping `git` in a script. Or a C program if you prefer. If the question is "Does `git` support this intrinsically" then my answer would be "No." – John Zwinck Oct 09 '20 at 22:59
  • Ok. You're right, but it would be good to add this to your answer. I'll redo my downvote then – kapsiR Oct 09 '20 at 23:01
1

Git is notorious for not warning the user when performing a potentially dangerous operation. And in any case, opinions can vary over what constitutes danger. Basically, when you use Git at the command line, you are saying you're a power user and you know what you're doing. There's no way to make Git disabuse you of that notion.

Instead, you could use a GUI, such as Sourcetree. It knows that reset --hard is potentially dangerous, and puts up an alert that forces you stop and think hard about what you're doing.

matt
  • 485,702
  • 82
  • 818
  • 1,064
  • by now I am a CLI-native, so unfortunately a GUI isn't really an option ;) – xeruf Oct 12 '20 at 10:05
  • Yes, but the question, “I have gotten into the bad habit of using git reset --hard regularly”, says the opposite. – matt Oct 12 '20 at 12:06