Live data from Hacker News

Oh Shit, Git?

ohshitgit.com

1–10 of 275 posts

Re: Oh Shit, Git?

#3
Some changes I would make:

1. Always use `git switch` instead of `git checkout`

2. Avoid `reset --hard` at all costs. So for the "accidentally committed something to master that should have been on a brand new branch" issue, I would do this instead:

    # create a new branch from the current state of master
    git branch some-new-branch-name
    # switch to the previous commit
    git switch -d HEAD~
    # overwrite master branch to that commit instead
    git switch -C master
    # switch to the work branch you created
    git switch some-new-branch-name
    # your commit lives in this branch now :)
3. I'd apply the same to the `cherry-pick` version of "accidentally committed to the wrong branch":

    git switch name-of-the-correct-branch
    # grab the last commit to master
    git cherry-pick master
    # delete it from master
    git switch -d master~
    git switch -C master
4. And also to the "git-approved" way for "Fuck this noise, I give up.":

    # get the lastest state of origin
    git fetch origin
    # reset tracked files
    git restore -WS .
    # delete untracked files and directories
    git clean -d --force
    # reset master to remote version
    git switch -d origin/master
    git switch -C master
    # repeat for each borked branch

Re: Oh Shit, Git?

#4
Git is one of those technologies that I never got to wrap my head around of, because in so many ways it doesn't follow intuition and unless you have been using it for a long time, for literally every action you would probably have to Google or use the man page of the command.

Re: Oh Shit, Git?

#5
Yeah, please don't create sites like this. Just... don't.

Any, and I mean any "in case of a Git problem, just do this" recipe is wrong, often in very subtle ways. So, my advice: in case of a Git problem, contact the help channel provided by the organization hosting your Git repository. They'll help you out! And if it's your personal-I-am-truly-the-only-human-using-this repository? Just recreate it, and save yourself the pain.

Source: I'm part of the team behind the #githelp channel in many $DAYJOBs, and we know how hard things are. You committed an unencrypted password file, or worse, your entire 'secret' MP4 collection to our monorepo? Sure, just let us know! Pushed your experimental branch to master/main/head/whatever? We'll fix it!

Just don't ever, for whatever reason, run that-chain-of-commands you found on the Internet, without understanding what they do! In most cases, your initial mistake can be undone pretty quickly (despite requiring nonstandard tooling), but once you're three levels deep and four days later, not so much...

Re: Oh Shit, Git?

#6
Previous discussions:

Oh Shit, Git - https://news.ycombinator.com/item?id=31874308 - June 2022 (232 comments)

Oh Shit Git? - https://news.ycombinator.com/item?id=24173238 - Aug 2020 (156 comments)

Oh shit, git (2016) - https://news.ycombinator.com/item?id=19906972 - May 2019 (277 comments)

Oh shit, git: Getting myself out of bad situations - https://news.ycombinator.com/item?id=15951825 - Dec 2017 (509 comments)

Re: Oh Shit, Git?

#7

Yeah, please don't create sites like this. Just... don't. Any, and I mean any "in case of a Git problem, just do this " recipe is wrong , often in very subtle ways. So, my advice: in case of a Git problem, contact the help channel provided by the organization hosting your Git repository. They'll help you out! And if it's your personal-I-am-truly-the-only-human-using-this repository? Just recreate it, and save yoursel…

We’re not all working at $bigcorp with dedicated help teams. Sites like this are great and have helped me many times!

Re: Oh Shit, Git?

#8
post #4

Git is one of those technologies that I never got to wrap my head around of, because in so many ways it doesn't follow intuition and unless you have been using it for a long time, for literally every action you would probably have to Google or use the man page of the command.

I was trying to delete a file from history yesterday.

The built in way (filter-branch) pops up a warning, with a long delay, saying you should hit Control+C and go download a third party python script called filter-repo...

Re: Oh Shit, Git?

#10
post #7

Yeah, please don't create sites like this. Just... don't. Any, and I mean any "in case of a Git problem, just do this " recipe is wrong , often in very subtle ways. So, my advice: in case of a Git problem, contact the help channel provided by the organization hosting your Git repository. They'll help you out! And if it's your personal-I-am-truly-the-only-human-using-this repository? Just recreate it, and save yoursel…

We’re not all working at $bigcorp with dedicated help teams. Sites like this are great and have helped me many times!

What happens when you are the help team and it's the first time something goes wrong?
Post reply on HN