Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think the steps under https://ohshitgit.com/#accidental-commit-master are wrong: it reverts the commit on the new branch -NOT on the master. This is because git branch auto checks out the new branch. You need to do

git branch NEWBRANCH

git checkout master

git revert —hard HEAD^

If you want to continue working on the new branch you do

git checkout NEWBRANCH



>The command’s second form creates a new branch head named <branchname> which points to the current HEAD, or <start-point> if given. [...] Note that this will create the new branch, but it will not switch the working tree to it; use "git switch <newbranch>" to switch to the new branch.

Source: git branch --help


The steps I see on the website are:

  git branch some-new-branch-name
  git reset HEAD~ --hard
  git checkout some-new-branch-name
Which is correct, assuming master is already checked out.

0. prior state is that we're on master and have committed something that should have been on a branch

1. create a new branch that is identical to master (i.e., contains the commit) -- note this does NOT checkout the new branch (`git checkout -b some-new-branch-name` would do that)

2. reset current branch (master) to point at the commit before (i.e., strips the commit from master)

3. checkout the new branch to continue work there

At the end, master doesn't contain the commit anymore, and the new branch does. It's all correct.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: