This came about after a complete disaster. I hope you emply one of the following strategies or something different as long as it accomplishes saving you from this evil fate!
I had a terrible ear infaction spreading into my jaw and eyes, it was killing me! While working late with a client, I "nah'd" in another clients directory and destroyed what was a week of work. There are so many things that combined together to cause this disaster. But in the end, a millisecond after I have hit enter I knew I was in trouble and after purchasing several recovery apps, all garbage I realized I was boned and the best hting I could do was a 2 pronged attack.
- Avoid working when I'm clearly too messed up.
- Make a safer version of nah, becasue anything that happens once, will happen again!
There are 2 strategies I employed when trying this out...
- make nah delete files to the trash, where you can restore.
- make nah purge all the files as expected but move them in an organized fashion to a safe timestamped backup that can be cleared.
brew install trash
In an aliases file, I use .aliases and import it into bash or zsh but you can drop it into .bash_aliases or .zshrc .bash_profile basically whatever you are running
alias nah='git ls-files --exclude-standard -om | xargs trash -F; git reset --hard'
Doing this will send you nah to the Trash Bin where you can restore your files.
This gives you a 1 step backup. Delete your trashes or nah somewhere else and you are screwed. It would have saved me, but it's more high risk than I want.
The alternative, scenario adds a more robust system.
Nothing to install here, but you will need to actually go outside you aliases files and make full on bash scripts. You need to do this becasue xargs does not install a full terminal but only a minimal shell, so your aliases and functions will not be available. However, scripts in your path will be avilable.
This version will allow you to nah to your hearts content. It will move all the appropriate files to a hidden .nahs folder in your project directory. you can restore
I opt for easy to remember lcations. I added a shell-scripts
directory right in my home directory. It's easy to find and remeber what its for. It's not in my path but you can link it to any shell available path like /usr/local/bin or ~/.composer/vendor/bin or whatever you like. Make sure the files are shell executable.
The folowwing files are needed.
- A place for your root commands
.aliases
for me. safe-nah.sh
in my shell scripts directorysafe-nah-restore.sh
in my shell scripts directory- Add the .nahs/ directory to your global gitignore file.