Search code examples
gitsmartgit

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


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?


Solution

  • This was a long time ago, but my best guess is that I did exactly as suggested:

    1. Clone into a new directory.
    2. Add to the new git repo a "local remote" pointing to the old directory by directory name.
    3. Do a git fetch on that remote.
    4. Create new branches from all desired remote branches.
    5. Remove the remote
    6. Delete the old directory.

    Also, consider:

    git checkout master
    git reset origin/master --hard