ITADN

Make "accidentally committed to master" just two commands

#176Openmiallo 创建于 2025-10-04
M
miallocommented
A way of achieving https://github.com/ksylor/ohshitgit/blob/66318347fdfbfdc9e09b55291576a0751aa14dad/en/swears/tips/04-accidental-commit-master.md?plain=1#L9-L13 with just two commands that falls into the category of "now we are playing code golf" could be ```sh # switch to a new branch from the current state of master git switch -c some-new-branch-name # update your master branch - CAREFUL: You need the "/refs/heads/"! git update-ref /refs/heads/master HEAD~ ``` Pros: 1. Shorter by avoiding splitting the creation of the new branch and switching to it 2. Is an alternative solution to #144 to avoid the `reset --hard` potentially deleting uncommitted changes. While `--keep` would at least error out instead of deleting changes, this solution will always work though, no matter if you have (un)staged changes and it will keep them exactly (un)staged as they were. Why this _shouldn't_ be done: 1. update-ref is more niche and another command to learn 2. Since update-ref needs the full "/refs/heads/…" qualifier, but will happily "succeed" if you accidentally only type "master" by creating the file `.git/master` instead of updating `.git/refs/heads/master` it would probably lead to some strange behaviour of your git in the future that is hard to trace. I would consider this a footgun.
0 条评论