News for hackers. Stuff that matters.
Re:Git undo: We can do better by OhNoMyqueen on Monday June 21, 2021 @12:11PM (#27580591)
The most painful mistakes beginers endure with git are irrevocable code removal from the current working directory because of bad usage of `git clean`, `git reset` (or `git merge` if you're brutish enough). This problem cannot be solved by using git, because git doesn't have any reference to such code.
Re:Git undo: We can do better by abdusco on Monday June 21, 2021 @12:33PM (#27580805)
If you're using JetBrains products, you can view the history of the changes for the whole project and revert mistakes by right-clicking the project > Show Local Changes.

https://twitter.com/abdusdev/status/1403050600925962247

Re:Git undo: We can do better by GordonS on Wednesday June 30, 2021 @06:08AM (#27686791)
I use Rider, and this has saved me multiple times!

JetBrains IDEs also have the best UI I've come across for conflict merges.

Re:Git undo: We can do better by anamexis on Monday June 21, 2021 @12:28PM (#27580759)
I agree, these are the only times I've irreversibly gotten myself into trouble.

I think it would be possible to solve these with git, by doing something like automatically running a `git stash --include-untracked` before an operation that can clobber untracked files.

Re:Git undo: We can do better by Leherenn on Monday June 21, 2021 @04:30PM (#27584019)
A bit less painful, in the sense that you probably won't lose work, but still quite a headscratcher is line endings issues. Not amount of `git checkout .` or `git reset --hard` will help, and you just have massive diffs with no obvious change. I recloned the repo quite a few times in my younger days over this, and I still have to think deeply to be sure to use the correct configuration.
Re:Git undo: We can do better by kahkeng on Monday June 21, 2021 @02:02PM (#27581808)
I find it pretty useful to use the `--patch` or `-p` option (also a mnemonic for "prompt") to various commands:

`git add -p`: prompt which hunks should be added to staging

`git checkout -p`: prompt which unstaged hunks should be thrown away

`git checkout HEAD^ -p`: prompt which hunks from HEAD should be discarded

`git reset -p`: prompt which currently-staged hunks should be unstaged

`git reset HEAD^ -p`: prompt which hunks from the HEAD commit should be unstaged

Nice thing is that you get interactive yes/no prompts with a preview of the change for each hunk, and you can also quit early if you realize it's not the command you want.

Doesn't fully address the potential for lost changes, but slows it down by adding an interactive roadblock for each change.

Re:Git undo: We can do better by seoaeu on Tuesday June 22, 2021 @12:06AM (#27587881)
Frustratingly `git add -p` adds a new and exciting way to accidentally lose "work": You can type `git commit -a` and all of a sudden all your manual selections of different hunks to include/exclude are irreversibly gone
Re:Git undo: We can do better by secondcoming on Monday June 21, 2021 @05:11PM (#27584557)
hunk?
Re:Git undo: We can do better by cerved on Tuesday June 22, 2021 @03:51AM (#27589069)
noun

1. a large piece of something, especially food, cut or broken off a larger piece.

Re:Git undo: We can do better by alxndrr on Tuesday June 22, 2021 @04:41AM (#27589367)
Tower for Mac supports undoing (and redoing) all working tree actions like staging/unstaging/discarding individual chunks or complete files, undoing applying a stash, everything. Just hit CMD-Z. The feature is coming to Windows soon as well!

I am the CTO of Tower, a Git client for Mac and Windows.

Re:Git undo: We can do better by juped on Monday June 21, 2021 @12:19PM (#27580678)
Also from deleting their whole checkout.
Re:Git undo: We can do better by nsomaru on Monday June 21, 2021 @12:27PM (#27580748)
Am I doing things wrong if I have never used these commands?
Re:Git undo: We can do better by jdauriemma on Monday June 21, 2021 @12:42PM (#27580891)
Try not to think of it in terms of right and wrong. `git clean` is useful, though not "can't-live-without-it" useful. I couldn't imagine my workflow without `git reset`, though, and I recommend checking it out!
Re:Git undo: We can do better by jonahx on Monday June 21, 2021 @01:24PM (#27581339)
Probably not using git ideally. Do you clean your history after working on a feature?
Re:Git undo: We can do better by rjmunro on Monday June 21, 2021 @01:54PM (#27581713)
No, IMHO you are doing things right. I virtually never use those commands, I've been using git for well over 10 years, and have totally got used to all it's quirks.
Re:Git undo: We can do better by rags2riches on Monday June 21, 2021 @01:10PM (#27581173)
Don't forget `git checkout`!

When I help people get started with git, I tell them several times to commit as often as they can. If they committed it, I can probably save them when they think they lost something.