Sublime Forum

Are my commits lost forever?

#1

I performed 2 commits at 11:01 and 11:02. When I tried to push to master I got an error saying I couldn’t push because I wasn’t on any branch, and I had to be on a branch before pushing.

So I checked out master, but now my two commits are gone! All the code has reverted to the last master push. Is there a way to retrieve those 2 commits from somewhere? As you can see, the latest commits are from August, there’s nothing in Stashes, and nothing in any of the other branches. Please advise.
branches

0 Likes

#2

Hi @marquizzo,

Sorry to hear you’ve lost visibility of the commits. Whilst they might not be visible, it’s likely the commits are still around in what’s called the reflog. You can run git reflog via the command line to view previously created commits.

Once you run this, it should pop up a list of commit IDs along with some metadata. You can find the commit ID before you checked out master, and check that commit out using git checkout COMMIT_ID_HERE. Once you’ve checked out the commit, confirm it is the correct one, and create a branch (easiest way is to create one in sublime merge). Once the branch is created, you won’t lose visibility to that commit anymore.

Let me know if I can assist you further.

Thanks,
- Dylan

1 Like

#3

Whew, thank you so much! This really gave me a scare, you saved me about 5 hours of lost work!

1 Like

#4

See this enhancement request for improvements & warnings on losing commits when in detached head state.

Also, this enhancement request for a first-class reflog view:

2 Likes

#5

Definitely good enhancement requests. As it stands right now, SublimeMerge just makes detached commits vanish from the user interface… really gave me a scare!

0 Likes

#6

Just so you know, git will let commits vanish as well. The only difference is that git will tell you you’re leaving commits behind (see example below), whereas Sublime Merge does not (nor can they be retrieved in the command history).

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

  6be1b0470 commit-message

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 6be1b0470

Switched to branch 'master'
Your branch is up to date with 'origin/master'.
2 Likes

#7

To get out of a detached head without losing my commits, I used to create a temporary branch at my commit, checkout the correct branch, merge with fast-forward, then delete the temporary branch but I found a better way and created a Sublime Merge command for it (that you can add to your Default.sublime-commands file in your {installation_folder}\Data\Packages\User directory):

{
	"caption": "• Set branch to head",
    "command": "git",
    "args": { "argv": ["checkout", "-B", "$select_local_branch"] }
},

This command effectively places the local branch that you select at the HEAD where you are at (note: the bullet point in the name of the command is to distinguish my own commands from the default ones included in SM). So when I try to push and get the error that I’m in a detached HEAD, I run this command to get out of it and push on the correct branch.

1 Like

#8

Saved my life, many thanks

0 Likes