Dzhao
Dzhao

Reputation: 693

Missing commits after switching back and forth between branches

I am new to git so please bear with me. On my local repo, I've been working on this branch for a while with many commits. I haven't really pushed in a while because I am the only one working on this project. Today I finally decided it might be time to merge the development branch into the master branch so I did a git checkout master. (I did a git status before and nothing needed to be added).

After switching to master, I decided to go back to my development branch with a git checkout check_pburst (check_pburst is my development branch's name) but now I can only see the commits up to the time I pushed. I have a feeling I'm on a remote tracking branch (maybe?). git log shows that a lot of my commits aren't shown. Is there a way I can see the commits past the time I pushed? I wish I could give you guys more info but I accidentally closed git bash and now the messages I got aren't view-able anymore.

How do I recover my latest commits?

Update: After looking into this issue more, I did a git branch and saw only two branches (which makes sense). I then did a git checkout tab tab and it listed more branches but none of those branches had my latest commits. Then I did a git reflog which showed my latest commit. I did a git checkout 25edfe8. I then did a git branch and noticed that my latest commit was on the branch Detached HEAD at 25edfe8

Upvotes: 1

Views: 929

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521168

From our discussions, it became evident that you had been working in a detached head state rather than in your feature branch check_pburst. The standard solution to bring commits from a detached state into an actual branch is first create a branch. Do the following from your detached state:

git checkout -b check_pburst_detached

At this point, you have created a bona-fide Git branch containing all the stray commits which you made. It would be prudent at this point to backup your work by pushing this to the remote:

git push origin check_pburst_detached

If at any subsequent point you make a mistake or get lost, you can always "reset" back to this branch, which you know contains all your commits.

Since you want to actually retain all the detached commits in your feature branch, you best option might be to rebase check_pburst_detached on check_pburst and then fast-forward the latter with the former:

git checkout check_pburst_detached
git rebase check_pburst

Complete the rebase and then fast-forward merge check_pburst_detached onto check_pburst:

git checkout check_pburst
git merge check_pburst_detached

This merge should have zero conflicts, as the check_pburst_detached branch should be ahead of check_pburst. You can verify that your feature branch check_pburst indeed has the commits which went missing.

Upvotes: 1

Related Questions