News for hackers. Stuff that matters.
Re:Things I wish everyone knew about Git (Part I) by lisper on Wednesday June 29, 2022 @03:10PM (#31924443)
> But if I were going to tell everyone just one more thing, it would be:

> It is very hard to permanently lose work.

Unfortunately, this is not quite true. Git checkout will silently and irretrievably clobber all the changes in your working tree [UPDATE: if you do 'git checkout [path]', but that is not an uncommon thing to do.]

It is true that it is very hard to lose work that you have committed. But even this is not necessarily a good thing if, for example, you accidentally committed something that contains sensitive information that you want to delete. (And God help you if you have pushed such a change upstream.)

The sad fact of the matter is that while the underlying data structures are beautiful and tremendously useful, the UI/UX is a dumpster fire.

It's actually not that hard to put a different UI/UX on top of the core. I'm kind of surprised no one has done this.

Re:Things I wish everyone knew about Git (Part I) by cryptonector on Wednesday June 29, 2022 @06:07PM (#31926277)
Never `git checkout -f` or `git reset --hard` unless you know you want to throw away extant changes from the workspace.

That's it. Know that, live that, and you'll never lose work because of Git.

Re:Things I wish everyone knew about Git (Part I) by lisper on Wednesday June 29, 2022 @06:50PM (#31926661)
Nope. There is at least one other case that loses work irretrievably. Details can be found in sibling comments.
Re:Things I wish everyone knew about Git (Part I) by remram on Wednesday June 29, 2022 @09:03PM (#31927596)
git checkout with two arguments will clobber the working copy, no -f required
Re:Things I wish everyone knew about Git (Part I) by stu2b50 on Wednesday June 29, 2022 @06:14PM (#31926345)
You can git reset --hard and still get your work back without much trouble. Resets just move the branch pointer back, after all.
Re:Things I wish everyone knew about Git (Part I) by cryptonector on Wednesday June 29, 2022 @06:19PM (#31926391)
Hard resets throw away extant changes in the workspace that are not in the index or committed. You can't find those changes in the reflog or stash.
Re:Things I wish everyone knew about Git (Part I) by remram on Wednesday June 29, 2022 @09:04PM (#31927605)
It also throws away changes from both the working copy and the staging area.
Re:Things I wish everyone knew about Git (Part I) by cryptonector on Wednesday June 29, 2022 @10:31PM (#31928172)
Only if --hard is given.
Re:Things I wish everyone knew about Git (Part I) by remram on Thursday June 30, 2022 @09:59AM (#31932429)
Which is what we're talking about.
Re:Things I wish everyone knew about Git (Part I) by cryptonector on Thursday June 30, 2022 @10:19AM (#31932633)
Right, so stop typing that at all. Instead use `git checkout -f` to throw away extant changes. The `-f`/`--force` option is a lot clearer in intent than `--hard`.
Re:Things I wish everyone knew about Git (Part I) by remram on Thursday June 30, 2022 @02:02PM (#31936159)
The GP was talking about "moving the branch pointer", which checkout does not do. I am encouraging you (for the second time) to read the thread you are replying into...

git checkout can also nuke changes from both working copy and staging area. `git checkout <path>` will nuke working copy (and auto-completion is likely to give you a path when you were looking for a branch), `git checkout <ref> <path>` will nuke both. No --force required in either case, so let's not pretend that you can't lose changes without a scary-looking flag like --force or --hard.

Re:Things I wish everyone knew about Git (Part I) by cryptonector on Thursday June 30, 2022 @02:28PM (#31936526)
> The GP was talking about "moving the branch pointer",

Not explicitly.

`git checkout ref path` implies `--force`, since you're being specific.

I would prefer if Git could keep a GCed pile of saved workspace changes every time you force checkout/reset.

But for the love of all that is good, stop using `git checkout`/`git reset` in the few ways they hurt.

Re:Things I wish everyone knew about Git (Part I) by atq2119 on Wednesday June 29, 2022 @03:29PM (#31924658)
> Unfortunately, this is not quite true. Git checkout will silently and irretrievably clobber all the changes in your working tree.

Not by default it won't.

It can do that, if you explicitly run it with the options to checkout a specific path. But why would you do that if it's not what you want?

Re:Things I wish everyone knew about Git (Part I) by amcoy37 on Wednesday June 29, 2022 @03:46PM (#31924864)
Accidentally running a git command in the wrong folder is pretty easy to do at the command line.

I was, at one point, in the habbit of running "git checkout ." after writing some experimental code. Several times a day, or however often. So of course, I once accidentally ran that command in the wrong repo and obliterated some pending changes that hadn't been commited.

Re:Things I wish everyone knew about Git (Part I) by dotancohen on Wednesday June 29, 2022 @04:23PM (#31925278)
How is that git's fault?

I used to `vim test.c` then `rm Alt-.` very very often. Until one day sure enough I'm in the wrong directory, and I actually saw `Alt-.` complete the filename of an important file, but the brain veto latency is slower than the muscle memory twitch and my pinky continued on over to the Enter key.

I blame only Dotan and changed my work habits. Bash had nothing to do with the incident.

Re:Things I wish everyone knew about Git (Part I) by Someone on Wednesday June 29, 2022 @07:59PM (#31927223)
I don’t see anybody here claiming that’s git’s fault.

But it is an argument supporting the claim being discussed: that the statement “It is very hard to permanently lose work” is not quite true.

Re:Things I wish everyone knew about Git (Part I) by nemetroid on Wednesday June 29, 2022 @08:16PM (#31927330)
Using the command specifically designed to permanently discard work seems ”very hard” to me.
Re:Things I wish everyone knew about Git (Part I) by Someone on Thursday June 30, 2022 @02:12AM (#31929167)
But it’s inconsistent in that. git checkout existing_branch complains with “error: Your local changes to the following files would be overwritten by checkout”* and abort, git checkout existing_directory overwrites files.

That’s what makes it easy to make that mistake, and lose data. It’s safe to use until it suddenly isn’t.

Re:Things I wish everyone knew about Git (Part I) by nemetroid on Thursday June 30, 2022 @04:04AM (#31929860)
I just don't understand why someone would do git checkout existing_directory if they don't want to overwrite files, since that is all the command does.
Re:Things I wish everyone knew about Git (Part I) by eropple on Wednesday June 29, 2022 @04:50PM (#31925537)
I dunno - I can tell zsh to are-you-sure me about an `rm -rf *`, git seems like it should be able to do similarly. Or, as Mercurial does, back up overwritten files to .bak.

These are pretty light asks.

Re:Things I wish everyone knew about Git (Part I) by sagarm on Thursday June 30, 2022 @04:16PM (#31937989)
The sole purpose of git checkout -- file is to overwrite the file you explicitly listed. There should not be a confirmation by default.
Re:Things I wish everyone knew about Git (Part I) by compiler-guy on Wednesday June 29, 2022 @04:11PM (#31925141)
"Just don't make a mistake" is a pretty bad strategy.
Re:Things I wish everyone knew about Git (Part I) by chronometry888 on Wednesday June 29, 2022 @05:26PM (#31925921)
But "save your changes before running a command that can clobber" is a pretty good strategy.
Re:Things I wish everyone knew about Git (Part I) by kjeetgill on Wednesday June 29, 2022 @08:33PM (#31927429)
Agreed though. I always start with teaching people. If you git add + git commit + git branch BACKUP everything you can very safely do anything you want. If you get confused it's trivial to get to a clean state by git reset --hard.

If you give people a way to get to a clean slate it removes a significant amount of the learning curve. In my experience, push/pull/fetch/log/diff are easy enough it's only really merge/rebase management that gets people caught up. Giving people a short fuck up -> undo -> retry cycle lets people learn pretty quick.

Re:Things I wish everyone knew about Git (Part I) by lisper on Wednesday June 29, 2022 @03:57PM (#31924978)
> It can do that, if you explicitly run it with the options to checkout a specific path.

Fair point. I've update my comment to clarify.

> But why would you do that if it's not what you want?

Because sometimes it is what you want. The point is, if you happen to do it when it's not what you want, you're hosed. So it is simply not true that "It is very hard to permanently lose work." It is, in fact, quite easy under certain not-uncommon circumstances.

Re:Things I wish everyone knew about Git (Part I) by atq2119 on Wednesday June 29, 2022 @04:18PM (#31925212)
Fair point. There probably should be standard advice along the lines of "if in doubt, make a 'wip' commit before you do anything". That's good advice even beyond using git itself.
Re:Things I wish everyone knew about Git (Part I) by auraham on Wednesday June 29, 2022 @04:27PM (#31925333)
IIRC mercurial creates backup (.bak) files when restoring a file. As a result, you can keep changes in .bak files even after (accidentally) doing a `git checkout -- file` in mercurial. Git does not have that feature.
Re:Things I wish everyone knew about Git (Part I) by nemetroid on Wednesday June 29, 2022 @08:13PM (#31927315)
> if you do 'git checkout [path]'

The entire point of that command is to discard local changes.

There is some risk of getting the different forms of checkout mixed up, though, so prefer using switch and restore instead. As far as I know, the only use case of checkout not covered by other commands is checking out a commit that doesn’t have a branch attached to it.

Re:Things I wish everyone knew about Git (Part I) by lisper on Wednesday June 29, 2022 @08:37PM (#31927459)
> The entire point of that command is to discard local changes.

I think that's arguable, but it's neither here nor there. Someone who is not well versed in git arcana will be surprised that the level of risk associated with 'git checkout [branch]' is radically different from 'git checkout [directory]'. I think a lot of people will learn this the hard way, especially if they have been told "It is very hard to permanently lose work." It's not true. Permanently losing work is as easy as being careless with git checkout.

Re:Things I wish everyone knew about Git (Part I) by nemetroid on Thursday June 30, 2022 @04:02AM (#31929847)
> Someone who is not well versed in git arcana will be surprised that the level of risk associated with 'git checkout [branch]' is radically different from 'git checkout [directory]'.

I disagree that it's a difference in the level of risk. There's no "risk" that git checkout [path] will discard your uncommitted changes, it's a promise and a certainty.

That said, I agree that the breadth of largely unrelated functionality smushed into git checkout is surprising, but it's also one of the few things about Git's user interface that actually has been addressed by new subcommands.

Re:Things I wish everyone knew about Git (Part I) by wizofaus on Wednesday June 29, 2022 @10:34PM (#31928200)
I haven't learned the hard way thankfully, but I certainly found it very surprising. It's not at all obvious, and the GUIs with git support call it something like "Undo changes" or "Revert" etc.