Skip to content

Make "accidentally committed to master" just two commands #176

@miallo

Description

@miallo

A way of achieving

# create a new branch from the current state of master
git branch some-new-branch-name
# remove the last commit from the master branch
git reset HEAD~ --hard
git checkout some-new-branch-name
with just two commands that falls into the category of "now we are playing code golf" could be

# 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 Please DO NOT tell people to run git reset HEAD~ --hard on their master because they might have yet untracked files #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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions