When I checkout my master branch, it is not actually checked out in git

998 views Asked by At

I ran into a situation today where, after pulling from an upstream branch and rebasing, recent commits to the upstream branch were acting like they hadn't taken effect.

Symptoms in the best order I can remember:

  1. While coding in a local branch, a Visual Studio solution file kept appearing to be modified without my intervention--its new contents exactly undoing just one file in the most recent commit (but not undoing the other 3 .cs and .prj files). It seemed like Visual Studio was doing it, but I don't know for sure. (That is, git status showed a new modified .sln file but my changes shouldn't have affected the project).

  2. I couldn't build the solution due to this file having been modified and the solution not being aware of important new dependencies (references added to two projects). Discarding the modified file (on the theory that I wanted the changes added in the most recent commit from upstream) did not help.

  3. I have been making changes in a local development branch, but couldn't see how my single local commit could be causing the weirdness--I just hadn't made those kind of changes.

  4. I closed and reopened VS, and cleaned and rebuilt the solution, more than once.

  5. The SmartGit log showed no additional commits or changes besides the one that I had made to a local branch, and was expecting.

  6. Attempting to remove any confounding factors, I switched from my local development branch to my local master branch (which I generally keep unmodified without any new commits) and rebuilt the project, all with the same results.

  7. I finally noticed that the SmartGit log was providing visual evidence that master wasn't really master. Take a look at this screen shot from SmartGit/Hg 5.0.3 (build #2073):

    SmartGit Log Screen Shot

    This is the result after issuing in the git console git checkout master. But look at the blue lines connecting the commit nodes: it shows that I really have the 11th commit backward in history checked out!

    If I issue git checkout master^ then it properly checks out the 2nd commit down, and adjusts the blue line accordingly.

    If I checkout the most recent commit by SHA, git checkout a07df46 then the blue line extends all the way up to the very top commit.

  8. When I do git show-ref it indicates that refs/heads/master is pointing to the SHA of the most recent commit from upstream--as expected.

  9. I tried the following series of commands, which netted no improvement whatsoever in the state of things (the SHA is the one for the most recent commit from upstream):

    git checkout HEAD^
    git branch -f master master^
    git branch -f master a07df46
    git checkout master
    

All I want to do is get my repository back in sync so I can continue developing!

If I can't figure it out soon, I'm going to push all my local branches to a personal remote fork (different from the upstream main project), delete my local repository, re-clone the upstream repository locally, and then pull any branches I care about back from my fork. But, I would really love to know how git got into this strange master-but-not-master state.

How do I recover from this strange state without re-cloning the entire repository?

0

There are 0 answers