tags:

views:

266

answers:

3

Using the MSYSGIT history browser, I checked-out a particular commit. This was a mistake - I was trying to view the files and thought I had to check them out to see them.

Since then, I have been unable to push any more commits to my remote repo (on GITHub). Which I somewhat understand, though not entirely. [I admit to not fully understand how checkout works, please be gentle on a noob.]

How do I undo that checkout and get things back to the state they were in before?

If I right click on the 'checked out' commit, I get the option to 'remove this branch'. Will that delete the files of that commit [bad] or just undo the check out? Am I barking up the wrong tree?

A: 

A branch is basically a pointer, in C++ terms.

You may want to try moving your branch to point to a different commit. Use git reset for that.

git checkout mybranch
git reset mypreviouscommit

This doesn't delete the commit, but it does make mybranch "point to" mypreviouscommit. So, whatever was there at mypreviouscommit will be there at master.

strager
To verify that I understand this correctly - where the checked out commit is 2e968f, and I have since added another commit 2a45b0. I now do "git reset 2e968f" and I should have, as before I messed up, no checked out commits, and 2a45b0 being the latest regular commit.
SamGoody
Trying to reset threw an error. fatal: Could not parse object '2e968fd7bf2018680856316e3133c610284d9619'. Please help.
SamGoody
+3  A: 

My understanding is that you accidentally checked out an earlier commit, and then made fresh commits on top of that which you'd like to keep. First, ensure you have a pointer to your current work:

git checkout -b newbranch

Then, checkout where you were before:

git checkout master

Hopefully this will put you back on your mainline branch and you can push to GitHub as normal. If you then want to pull in the changes you made, you can then merge and (once you're happy) delete the temporary branch:

git merge newbranch
git branch -d newbranch
Dial Z
If you don't want to merge the whole newbranch, you can also git cherry-pick single commits.
Benno
Also, you will be unable to push the master to the remote if someone else has added commits to the remote master. In that case, do 'git pull' and after that 'git push' should work.
Antti Tarvainen
Great job! That did it! [Even though the question wasn't so clear.] Am still curious though: What would've happened had I pressed "remove this branch" in the msysgit history browser? Would it have deleted the files? Or even all commits made after that file?
SamGoody
It would have presumably removed the branch you were on (master?), and therefore any commits that were only referred to by that branch. It'd probably leave HEAD pointing to the same commit, but once you did something which moved that pointer there would be no references to those commits and they'd eventually be pruned. You'd still be able to pull another copy from the remote repo, though.
Dial Z
A: 

If you don't mind loosing all the changes you've made since the last pull to GitHUB, you can simply delete your directory and

git clone ...

on a fresh, new one.

Bruno Reis
I do mind losing my other commits, but its an idea to keep in mind. BTW, I find this attitude very common in GIT (eg. I came across this comment today: "Interesting, never faced that issue because I periodically recreate my fork from the master. Right, it's a lot of work especially if you have a lot of contributions but hey..." [Isn't that a very sad reflection of the impracticability of using GIT?!]
SamGoody