Now as I'm making commits to my project it shows the SHA-1 on the command line as the working branch (instead of master)
This probably means you have a “detached HEAD”. A detached HEAD points directly to a commit instead of pointing to a branch (which then points to a commit). A detached head is like an unnamed branch.
This state was caused by your git checkout HEAD^
command as HEAD^
refers to a commit, not to a branch name. You probably wanted to do git reset --hard HEAD^
—while master was still the active branch—to drop the most recent commit from master (it would still exist on disk and be reachable through the reflog until its entry in the reflog expired).
How can I point HEAD to the latest commit I'm working from?
HEAD
is always the commit from which you are working, whether it is detached or not.
If you mean “How can I point master to the latest commit I'm working from?”, then that is the same as asking “How can I get master to point to HEAD
?”. The answer is
git branch -f master HEAD
(actually, you can leave off HEAD
, since it is the default). This forcibly resets master to the commit currently at HEAD
. Any commits on master that are not reachable via another branch or the current HEAD
will henceforth only be reachable via the reflog and will eventually be garbage collected (this throws away, from master, anything in master that is not in HEAD
). You also probably want to reattach your HEAD
to this updated master after this.
git checkout master
Instead of the two commands above, you could reattach HEAD
first, then reset master with these two consecutive commands:
git checkout master # reattach, commit at HEAD is now the unwanted commit
git reset --hard HEAD@{1} # reset master to the commit at HEAD before the prior command
The HEAD@{1}
notation is used to access entries in the reflog. This example just means “the previous HEAD
” (i.e. “the commit at HEAD
before the most recent operation that affected HEAD
”).