Live data from Hacker News

Oh shit, git: Getting myself out of bad situations

ohshitgit.com

81–90 of 520 posts

Re: Oh shit, git: Getting myself out of bad situations

#81

A git off my lawn moment: Every time I see someone complaining because they have to dive into the reflog to fix their own mistake, all I can hear is "I was operating my table saw without using a push stick and can't understand why I lost a thumb". Friends don't let friends (especially those who don't learn how to use their tools) rewrite shared git history. If you don't understand rebase, amends, etc can do to your (…

On the other hand, doing it is the only way you're going to learn. Just do it in a safe environment, with a snapshot of the repository.

Re: Oh shit, git: Getting myself out of bad situations

#82

Nice. For anyone who hasn't see it, Flight Rules for Git is even more comprehensive: https://github.com/k88hudson/git-flight-rules

I like this. I was trying to do something similar with my Git cheatsheet - https://gist.github.com/JamesSkemp/15fcf0147cb85bb633bf - but it doesn't have the kind of organization I'd like due to the limitations of using a Gist.

Re: Oh shit, git: Getting myself out of bad situations

#83

My last git mistake was pretty terrifying. I decided to try and go back to an old commit on a project on my local machine after about a days work. Somehow I ended up making the commit I wanted to revert to a new branch, then somehow tagged that branch with the name of the commit making git get angry and decide that branch wasn't valid. Then continuing in my ignorance I reset to that branch and tried to checkout only…

Do yourself a favor and learn git properly. It pays of hugely in the long run. The book that clicked for me was Git Internals [1]

[1] https://github.com/pluralsight/git-internals-pdf

Re: Oh shit, git: Getting myself out of bad situations

#85

Adopted a git GUI years ago and haven't looked back. I get looks sometimes, but I can't help but gloat when I can stage and unstage individual lines in less than a second. I think anyone who uses the CLI is either trying too hard or hasn't realized the beauty of a git GUI. Takeaways: - My commit time is usually much faster than coworkers, with higher accuracy (less frequent accidental commits, etc.) - I don't remembe…

My biggest issue with a GUI is that most that I've seen introduce new terms for various stuff, e.g. "sync" in VS Code, or "revert commit" (in Source Tree maybe?) — there's no Git command called "sync" or "revert", so I'm not immediately sure what they do.

In a CLI I know exactly what's happening.

That's just my opinion though. If people feel comfortable working in a GUI all the more power to them.

Re: Oh shit, git: Getting myself out of bad situations

#86

Adopted a git GUI years ago and haven't looked back. I get looks sometimes, but I can't help but gloat when I can stage and unstage individual lines in less than a second. I think anyone who uses the CLI is either trying too hard or hasn't realized the beauty of a git GUI. Takeaways: - My commit time is usually much faster than coworkers, with higher accuracy (less frequent accidental commits, etc.) - I don't remembe…

> I think anyone who uses the CLI is either trying too hard

Why do you think it's okay to share this opinion?

Re: Oh shit, git: Getting myself out of bad situations

#87
post #27
post #11

> Oh shit, I accidentally committed to the wrong branch! I find cherry-picking to be easier in this case. Just checkout the branch and cherry pick commits from 'wrong' branch. https://git-scm.com/docs/git-cherry-pick

But then those commits are still in the wrong branch. If I accidentally commit something to master instead of a development branch, I can't deploy master until my development branch is merged in, as the one commit isn't ready for live.

That's where tools like github and gitlab come in (or a pre-receive hook on the server), which can deny all commits to master. If that's something you want to prevent of course - and tbf, as soon as there's more than one or two people working on a project, I'd lock master down.

Re: Oh shit, git: Getting myself out of bad situations

#88

I've said this before, but the business leadership, and tech leadership, need to think carefully about whether or not they need all of the power of Git. This sums up my concerns: ----------------------- Here are some minor failure modes I’ve seen with Git: 1. a branch that stays open for many months, perhaps even a year (for instance, at Maternity Neighborhood) 2. data is erased for good because someone makes a mista…

I'm not necessarily the biggest git fan in the world, but here's some refutations to some of these concerns:

> 1. a branch that stays open for many months, perhaps even a year (for instance, at Maternity Neighborhood)

This is more of a workflow question than a VCS question, and would be true of any VCS that allows branching (i.e. anything remotely close to a modern VCS)

> 2. data is erased for good because someone makes a mistake while using rebase

Data isn't permanently removed from rebasing, you can still get at it with the reflog. If you discovered the problem months later, I suppose that could happen. checkout and reset --hard can irrecobably destroy uncomitted data (as can clean, but that's more obvious and has appropriate guardrails)

> 4. widespread but fine-grained cherry picking leaves the team unclear about what’s been merged and what has not been merged

Overuse of cherry-picking feels like a workflow smell to me, but even still, you shouldn't need to care. Merging the entire branch will work fine if some of the contents have been cherry-picked previously.

> 5. a developer makes a change in the wrong branch because they forgot what branch they were in

True of any VCS with branching.

> 6. a developer is unable to recover a stash because they forgot where they were when they created the stash, or they simply forget that they have work in a stash

I'll agree with you here, I think stash is a bit of a footgun and almost never makes sense to use if the stashed contents are going to live for longer than a minute or two. But this is easily addressable with workflow, don't use stash for anything other than an extremely temporary holding place.

> 7. developers confused by working in an unattached commit, after a botched attempt to revert

Git is generally not great about warning you about dangerous operations or things you shouldn't be doing. Running against a detached HEAD is not one of those things, you'd have to be willfully ignoring what it's telling you to not realize that this isn't a safe thing to do.

> 8. a developer feels the need to delete the repo from their harddrive and clone it again, because the whole repo got into a state that they seemed unable to resolve

I will agree that git could make it easier to say "just get me back to origin, i give up"

> 9. the “blame” command is nearly useless — maybe its because we never know in which branch a given change was made, finding who made a mistake is very difficult

Blame isn't great, but it sucks in precisely the same way it sucks in any VCS. Just seeing who last committed a line often isn't enough information. It would be cool to see an alternative here, but I don't see many better alternatives around.

It sounds like in general, this is more a concern with dealing with branches and branching, which is totally fair. If simple trunk-based development works for you, branches are an unnecessary addition. But there's no reason you have to use all these git features. You can happily have everyone just work against master and never need to think about branches (in that mode the various Git GUIs will probably be more than adequate as well).

I do agree that git tutorials tend to focus on a more advanced branching / rebase focused workflow, and that simpler resources for less technically minded folks would be handy.

Re: Oh shit, git: Getting myself out of bad situations

#89

Adopted a git GUI years ago and haven't looked back. I get looks sometimes, but I can't help but gloat when I can stage and unstage individual lines in less than a second. I think anyone who uses the CLI is either trying too hard or hasn't realized the beauty of a git GUI. Takeaways: - My commit time is usually much faster than coworkers, with higher accuracy (less frequent accidental commits, etc.) - I don't remembe…

What GUI do you recommend? My experience has been that GUIs are the easiest and fastest way to make a mess that can't be corrected without dropping to CLI or re-cloning. (I'm looking at you SourceTree). I've long recommended that everyone who uses git know how to use the CLI even if they don't use it regularly.

Magit in emacs. Probably one of the killer applications for emacs.

Re: Oh shit, git: Getting myself out of bad situations

#90
post #14
post #9

Is it just me or did http://ohshitgit.com/#accidental-commit-master forget to checkout master before calling reset?

It's still on master because `git branch` just creates and doesn't do checkout. (Which is why I've almost exclusively switched to `git checkout -b ` for creating. How often do I want to create but not switch to the branch?)

Git branch is helpful if you just want to make a snapshot before doing something potentially harmful. If you hose up, just checkout that branch again.
Post reply on HN