←back to thread

Oh Shit, Git?

(ohshitgit.com)
464 points Anon84 | 1 comments | | HN request time: 0s | source
Show context
pitaj ◴[] No.42729155[source]
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
replies(11): >>42729838 #>>42729902 #>>42730974 #>>42731510 #>>42731713 #>>42731798 #>>42731885 #>>42732130 #>>42734933 #>>42737820 #>>42740404 #
baobun ◴[] No.42731510[source]
5. Teaching `git add .` as default to add changes to the staging area is not ideal. Show adding specific files instead has less room for subsequent "oh shit" and better.
replies(2): >>42732162 #>>42733716 #
ajross ◴[] No.42732162[source]
True enough, but it does make for good practice with the index and splitting workflows later on when you need to clean it up.

I think there's space for "git add ." as a didactic step. It maps cleanly to the most obvious way to understand a commit, as "here's what I've done". Bootstrapping from that to an understanding of "commits as communication with other developers" will naturally happen over time.

replies(1): >>42732679 #
baobun ◴[] No.42732679[source]
Is not very compatible with printlog-debugging. I'd rather encourage devs to prod around as they go if it benefits them, which causes grief for either them or reviewers in the end if they've internalized what you just said.

Explicitly adding internalizes a personal review process as inherent part of the push process, instead of something you attempt to force on top later.

It's better with a collaboration workflow that limits the span of time with expected discipline, imo.

replies(1): >>42732823 #
recursive ◴[] No.42732823[source]
You can have both. Make sure the whole diff is what you want it to be before invoking `add .`
replies(1): >>42743104 #
baobun ◴[] No.42743104[source]
Sure. I hear the pull-out method is also an effective contraceptive.
replies(1): >>42744366 #
1. recursive ◴[] No.42744366{3}[source]
If it works for you, I'm not going to try to talk you out of it.