News for hackers. Stuff that matters.
Re:Shellfirm: Intercept risky patterns at the command line by pca006132 on Sunday July 31, 2022 @03:37AM (#32293944)
I frequently do `git reset --hard`, knowing what I am doing, until I found that I probably need the code a few days later.

Perhaps I just need a tool to intercept this and backup the affected files somewhere. The files can be deleted after some time (a week? or never?). And when I regret doing resetting the files, I can just dig through the backup and try to find it.

Re:Shellfirm: Intercept risky patterns at the command line by ekimekim on Sunday July 31, 2022 @10:37AM (#32295784)
My solution for this is a script that commits all my changes then tags the resulting commit with a basic descriptive name. Something like this:

    git discard bad-idea-didnt-work
Which creates a tag discarded-bad-idea-didnt-work, then resets back to the previous commit.

I can still find and rescue the changes later by looking at my tags.

Others have suggested using the stash for this but I prefer to keep my stash stack free for shorter-term contexts, eg. if I'm halfway through something but need to go checkout another branch briefly.

Re:Shellfirm: Intercept risky patterns at the command line by skykooler on Sunday July 31, 2022 @12:43PM (#32296755)
Would you mind sharing that script? It sounds useful.
Re:Shellfirm: Intercept risky patterns at the command line by matsemann on Sunday July 31, 2022 @05:49AM (#32294471)
You've already gotten tips based on git. But if you use a JetBrains IDE, you also have the local history there. Or can use the shelve functionality, which is like git's stash but a bit smoother.
Re:Shellfirm: Intercept risky patterns at the command line by staindk on Sunday July 31, 2022 @08:50AM (#32295177)
I don't know how JetBrains IDEs do 'local histories' but FWIW VSCode recently also implemented "local file history"[1]. I have not had to use it to salvage anything yet, but I'm sure that day will come.

[1] https://twitter.com/code/status/1511463379210317824

Re:Shellfirm: Intercept risky patterns at the command line by schoen on Sunday July 31, 2022 @03:57AM (#32294056)
I was surprised to learn a few months ago from https://news.ycombinator.com/item?id=28958613 that the "git reset --hard" does not actually delete the files and you can still recover references to them using "git reflog".

I'm pretty sure most git users (who know what "git reset" is) don't realize this!

Re:Shellfirm: Intercept risky patterns at the command line by pca006132 on Sunday July 31, 2022 @04:00AM (#32294081)
You need to commit them first. If you did not commit them, it doesn't work. But yeah, maybe I can just define an alias to add+commit+reset, which should solve this problem.
Re:Shellfirm: Intercept risky patterns at the command line by wruza on Sunday July 31, 2022 @04:44AM (#32294265)
I just stopped worrying and commit everything into my own repos, even half baked or temporary, making sure that a commit message or a file name signals it clearly. It’s a versioning system for this exact purpose, not a ceremonial sanctuary.
Re:Shellfirm: Intercept risky patterns at the command line by npteljes on Sunday July 31, 2022 @08:44AM (#32295141)
Exactly. To me, some branches are quality, some are quantity.
Re:Shellfirm: Intercept risky patterns at the command line by ithkuil on Sunday July 31, 2022 @08:55AM (#32295196)
You can also stash them which is effectively a commit into a special stash stack
Re:Shellfirm: Intercept risky patterns at the command line by neilparikh on Sunday July 31, 2022 @03:51AM (#32294016)
You could just call `git stash` instead of `git reset --hard`. It'll have the same effect on your working directory, while also "backing up" the changes.

Stashes could be cleared using a cron job.

Re:Shellfirm: Intercept risky patterns at the command line by pca006132 on Sunday July 31, 2022 @03:56AM (#32294048)
iirc stash works more in a stack like manner? Say if you do git stash, and then do git rebase -i to clean up the history, proceed to write some other features, git stash again to clean it up, etc. Can you recover the files for the first stash?
Re:Shellfirm: Intercept risky patterns at the command line by Delk on Sunday July 31, 2022 @07:43AM (#32294901)
Yes, you can. If you save, apply or pop with git stash with no arguments, it works like a stack by default, but you can also access the other entries. For example `git stash pop stash@{n}`, where n is the index of the entry in the stash "stack", applies the nth stash entry on top of the working tree and then removes it from the stored stashes. The latest entry is at index zero, so stash@{0} would be the last saved/pushed one.

You can see a list of saved stashes and their indices with `git stash list`.

Re:Shellfirm: Intercept risky patterns at the command line by staindk on Sunday July 31, 2022 @08:56AM (#32295200)
Ah you can use pop with other stashes too, thanks for the tip... though that feels a bit like a semantic misnomer (? unsure what term would be best to describe what I'm thinking - but 'pop' kind of feels like it should always be applicable only to the top element in a stack. I'd be scared that popping stash@{5} would apply all of stashes 1 to 5 at once lol).

I've been using `git stash apply stash@{n}`, which works just fine, but the stash is then not removed from your list and results in clutter.

Re:Shellfirm: Intercept risky patterns at the command line by Delk on Sunday July 31, 2022 @12:22PM (#32296596)
> though that feels a bit like a semantic misnomer

It does, but then, this is the git command-line interface. :)

My understanding is that the stash pop command is simply an apply plus the removal of the successfully applied stash. The man page reflects this. It does not pop everything above element in the list.

Re:Shellfirm: Intercept risky patterns at the command line by amichal on Sunday July 31, 2022 @03:59AM (#32294071)
git stash list?
Re:Shellfirm: Intercept risky patterns at the command line by timfi on Sunday July 31, 2022 @03:49AM (#32294008)
In situations like this you could use `git stash` instead of completely deleting it.
Re:Shellfirm: Intercept risky patterns at the command line by IshKebab on Sunday July 31, 2022 @04:22AM (#32294178)
Yeah git reflog will show you the previous commit (in a pretty confusing way tbh but it does the job). The original files will get GC'd by git at some point.