Question

Background: I have a local repository that has a remote. All has gone well for quite some time. I'm doing development on a Mac and using GitX. Before the problem I'm about to describe, I had pushed to the remote 5 days ago, and had 10 local commits since then that are unpushed.

Problem: I staged a commit in GitX, wrote its commit message, and pushed the keyboard shortcut key for "commit," just like I've done many times. (At least, so I think--I did it quickly, so perhaps I did something different by accident--not sure.) Then the following situation occurred:

  1. GitX now shows master as completely disconnected from origin/HEAD and origin/master. (See screenshot of a portion of the GitX window here.)
  2. GitX also shows no record of the 10 commits since the last push. (The 10 most recent commits aren't in the list in that screenshot.)
  3. GitX seems to indicate that the current position of master has no history, as if all the files in the repo were added in that one (most recent) commit. (Not shown in screenshot.)
  4. git log from the command line lists one entry only, the last commit I just made.
  5. git log -g from the command line lists the full history, including the 10 missing commits, and the most recent one.

I have no idea what's going on, or why any of the above 5 things are happening. I do not understand the difference between git log and git log -g (aka git log --walk-reflogs) but it seems to be where the data I'm missing is residing.

Questions: How can I get back the 10 commits that only appear in git log -g? How can I reattach the master to the rest of the history? What on earth caused this in the first place?

Was it helpful?

Solution

I do not know gitX, but it looks a lot like you created a commit with no parents by accident. If that’s the case, you need to follow these steps:

  1. Copy your .git repository in case something goes wrong
  2. Note the SHA of your current commit on master (SHA1)
  3. Use a combination of git reflog master and maybe gitk --walk-reflogs or git log --walk-reflogs to find the SHA of your previous commit (SHA2).
  4. Run git checkout -B master **SHA2** to get back to that commit
  5. Run git checkout **SHA1** -- . to get your changes back in the working directory
  6. You are now back where you were before your mistake. So commit your changes and you are done.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top