Git newbie here. I made some changes that I wanted to abandon. So I did a checkout to an earlier point, and started working again. I don't recall exactly what else I did, but here is my current situation (as displayed by PyCharm):
HEAD is at the top (comment: "Typo"). origin/master are on the version whose comment is "Notes on redirection.
I pushed to github and HEAD didn't show up there. I understand that I need to made the Typo version my current origin/master. How do I do that?
I am the only developer, nobody is going to be affected by any changes. I don't care if the two versions on the dead end (ending in "Notes on redirection") are permanently deleted.
CodePudding user response:
You are currently in a "detached HEAD" state, meaning there is no active branch. You can have your master
branch point to you current revision ("Typo") using this command:
git switch --force-create master
After that you can push using the --force-with-lease
option. This is required because the push will abandon the two revisions "out and store ..." and "Notes on ...":
git push --force-with-lease
So I did a checkout to an earlier point, and started working again.
The right command to use would be git reset --hard <commit-ID>
. This avoids the "detached HEAD" state. Note that for the first push after that operation you still need to use the --force-with-lease
option.
CodePudding user response:
A branch in Git is just a string, a name pointing to a single commit. The way to get yourself back to "More reversion", when you decided you were going down the wrong road, was not to use checkout
but rather to use reset --hard
, which simply picks up the current branch name and repoints it to wherever you specify.
And you can use that same technique now to fix the problem! First make a branch to hold your place. Then jump back to the master pointer that you accidentally abandoned, and repoint it to where you are now:
git switch -c temp
git switch master
git reset --hard temp
You no longer need the placeholder, so delete it:
git branch -D temp
By the way, this sort of thing is why you should not develop on master — even when you are the only user of the repo. If you had been on an experimental branch when you decided your strategy was a failure, you could simply have abandoned the branch, switched back to master, and started a new branch with a different line of experimentation.