Hacker News new | ask | show | jobs
by OhNoMyqueen 1822 days ago
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.
8 comments

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

I use Rider, and this has saved me multiple times!

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

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.

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.
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.

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
hunk?
noun

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

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.

Also from deleting their whole checkout.
Am I doing things wrong if I have never used these commands?
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!
Probably not using git ideally. Do you clean your history after working on a feature?
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.
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.