0

I'm have a problem with git resulting from an attempt at fixing another problem. I have lost some recent commits I have made. There was a merge commit and another that I had not yet pushed to the remote repository.

I did git push. It output fatal: You are not currently on a branch.. This is strange, as my local branch has the same name as a remote one. I did a git push just a few hours ago successfully.

I found a page on Stack Overflow that said I should do git checkout origin:remote-branch-name in such a situation. So I did. Here was the output:

Warning: you are leaving 1 commit behind, not connected to
any of your branches:

This is certainly not what I wanted to do.

I can still see my latest commits by doing git reflog. After the last time I successfully pushed, I did git pull origin --rebase because there were diverging branches at that time which I then resolved. I only made one more commit since then.

How do I get my local branch to the state it was in before I did git checkout origin:remote-branch-name? The only thing that should be different is that the head should be attached.

I much prefer a process that doesn't make any new commits. I want the commit history to be as it was.

  • `You are not currently on a branch` implies [detached head](https://stackoverflow.com/q/10228760/23118), i.e. no branch is current. So check out (your local) `remote-branch-name`. You might need to adjust that branch to include the "lost" commits (possibly via cherry-pick or interactive rebase). – hlovdal Jun 28 '23 at 22:58
  • For viewing commits I highly recommend using gitk: `gitk --all &` or `gitk --reflog &`. – hlovdal Jun 28 '23 at 22:59

1 Answers1

2

You have not actually lost any commits (yet). As you rightly say, all previous commit states you've been in are in your git reflog, and each of those commits still comes with its chain of parentage. So you can return to any of those states by saying

git checkout HEAD@{n}

where n is the number shown in the reflog in designating this commit state, the one you regard as the last "good" place where you were:

How do I get my local branch to the state it was in before I did...

In other words, find that state in the reflog, and check it out.

You will now be in detached head mode, so the next thing to do is to get on a branch. The way to do that is simply to say

git switch -c branchname

where branchname is the name of the branch you wish to assign to this commit state. If branchname already exists, Git will refuse that order, with a fatal error; to override Git, change that command to

git switch -C branchname

Note that if you do that, you lose the commit (and possibly some its parent chain) that was previously pointed to by branchname, so try to be sure of your ground before doing something so drastic. A command such as

git log --all --graph --oneline --decorate

can give you a very complete picture (perhaps too complete) of the current state of play of your existing branches; and of course

git log --graph --oneline --decorate HEAD@{n}

will show you the parent chain of the commit you are proposing to check out. So that information should allow you to make more of an informed judgment about where you want to go from here.

matt
  • 515,959
  • 87
  • 875
  • 1,141