Michael Borgwardt
Michael Borgwardt

Reputation: 346397

How to retrieve accidental commits to a remote tracking branch?

According to GitGuys

Remote Tracking Branches should not be modified by users (don’t set your git branch to a remote tracking branch via git checkout and then try to modify the remote tracking branch).

Unfortunately, that's exactly what I did by accident, and committed a day's work. And now after switching to a different branch, those commits have disappeard and cannot be found in the log of any existing branch, local or remote.

Fortunately, I still see them in .git/logs/HEAD and using git show with the hashes there gives me the code diffs, so they're not lost completely. My questions:

Upvotes: 1

Views: 76

Answers (1)

Andy
Andy

Reputation: 46444

The commits are likely still there. You just need to find a reference that points to them.

Use git reflog to find your last commit that you lost. Once you find that commit, use git checkout -b <branch name> <lost commit hash> to create and checkout a branch that points to that tip.

As far as the error, you should have received a message saying you were working in a headless state.

Upvotes: 1

Related Questions