-
Notifications
You must be signed in to change notification settings - Fork 181
Make "accidentally committed to master" just two commands #176
Copy link
Copy link
Open
Description
A way of achieving
ohshitgit/en/swears/tips/04-accidental-commit-master.md
Lines 9 to 13 in 6631834
| # 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 |
# 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:
- Shorter by avoiding splitting the creation of the new branch and switching to it
- Is an alternative solution to Please DO NOT tell people to run
git reset HEAD~ --hardon their master because they might have yet untracked files #144 to avoid thereset --hardpotentially deleting uncommitted changes. While--keepwould 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:
- update-ref is more niche and another command to learn
- Since update-ref needs the full "/refs/heads/…" qualifier, but will happily "succeed" if you accidentally only type "master" by creating the file
.git/masterinstead of updating.git/refs/heads/masterit would probably lead to some strange behaviour of your git in the future that is hard to trace. I would consider this a footgun.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels