tags:

views:

149

answers:

3

Okay so I made some changes in my project that resulted in a huge mess. I had already committed the changes so I could go back to it later, and then used git checkout HEAD^ to checkout the previous commit. 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)

I don't know everything there is to know about git but I'm guessing HEAD is still pointing to my broken copy as I'm going off in a tangent and have resolved the problem. How can I point HEAD to the latest commit I'm working from?

I'm guessing it has to do with rebase but I'm not 100% sure.

Thanks.

+3  A: 
git checkout HEAD
git reset HEAD^

This will erase the most recent commit. Confirm that it worked with svn log.

I noticed you tagged this git-svn. This will only work if you haven't pushed the messed up commit out to svn yet. If you have, you'll need to apply a reverse merge of the messed up commit like this:

$ git log
commit 30480f327040f812cb2afffdd1cdd374bf26fe83
Author: you
Date: today

    messed up commit

$ git revert 30480f327040f812cb2afffdd1cdd374bf26fe83

where 30480f327040f812cb2afffdd1cdd374bf26fe83 is the hash of the messed up commit.

nilbus
+1  A: 

I think what you're looking for is git reset:

git checkout HEAD
git reset --hard HEAD^

This will take "master" back to one revision prior to the current HEAD. What you're working with at the moment is called a "detached head", since it doesn't have a symbolic name associated with it.

Greg Hewgill
+3  A: 

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”).

Chris Johnsen
Thanks for going in-depth.
Ryan Kearney