0

The code I have been writing for 5 days disappered after tapping wrong button. I have been working in HEAD. And made 3 commits in 5 days. In BitBucket desktop app I have twice tapped a different branch and now its gone. I can't find commits that were not sent to the server.

The branch doesn't come up, but commits are visible in history:

Macintosh:repo admin$ git branch -a
* HEAD
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Macintosh:repo admin$ git branch
* HEAD
  master

Here is the history of commits:

Macintosh:repo admin$ git log --oneline --all --graph --decorate  $(git reflog | awk '{print $1}')
warning: refname 'HEAD' is ambiguous.
* 1d2bc57 THIRD
* d2a0291 SECOND
* 40c2d66 FIRST
| * 7100271 (HEAD -> master, origin/master, origin/HEAD, HEAD) CURRENT
|/  
* bc35e2f OLDCOMMINT

The Desktop app shows empty HEAD branch, thats why I can't understand what is going on here. How do I switch from "7100271" (CURRENT) to "1d2bc57" (THIRD) ?

Thanks in advance.

ashvardanian
  • 424
  • 1
  • 6
  • 17
  • I'm glad that you found a solution below, but it's worth pointing out that a "Bitbucket desktop app" doesn't currently exist. Perhaps you mean [SourceTree](https://www.atlassian.com/software/sourcetree)? – ChrisGPT was on strike Jul 10 '16 at 01:39
  • Yes, certainly. Isn't it the official app? – ashvardanian Jul 10 '16 at 01:42
  • Both products come from the same company, but I wouldn't call SourceTree official. Many people use Bitbucket with other clients, and many people use SourceTree with other repository hosts. – ChrisGPT was on strike Jul 10 '16 at 02:03

3 Answers3

2

You should be able to access the data by simply using git checkout on the commit sha

git checkout 1d2bc57

After this you may not be on any branch (a 'detached head state') anymore, in which case you can just create a new branch.

git checkout -b newBranch

I hope this helps!

icesin
  • 128
  • 1
  • 3
  • Thanks for fast response. How can I then make my current branch the main one. So obviously, make GIT think that master is "THIRD", rather than "CURRENT"? – ashvardanian Jul 10 '16 at 01:26
  • And when I try to create a new branch I receive following errors: warning: refname 'HEAD' is ambiguous. fatal: Ambiguous object name: 'HEAD'. – ashvardanian Jul 10 '16 at 01:30
  • I think the error might be caused by you having another branch named 'HEAD'. Try renaming it to something else using `git branch -m HEAD newBranch` . You then should be able to use `git rebase master` while in the new branch to transfer all of the changes into master. – icesin Jul 10 '16 at 02:37
2

Use git reflog to obtain a history of all commits you checked out or committed.

Once you find your most recent commit, use git checkout <hash> to check out that commit.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1

What has happened here is that you have created a branch (regular, ordinary, local branch) named HEAD, whose name conflicts with the special name HEAD that Git uses to know which branch you're on.

When you check out a branch like master, Git writes the branch name into the HEAD file. If you check out the branch named HEAD, Git will write HEAD into the HEAD file. This entire situation is highly confusing: it's like going to a party, or a classroom, and finding out that everyone is named Ashot. :-)

The best thing to do here is to rename or remove the branch that is named HEAD (rename or kill off the extra Ashot, as it were). To rename it from the command line:

$ git branch -m HEAD new-name

I have no idea how to do this from some fancy GUI (there may not be any way to fix it from there, GUIs tend to be limited).

torek
  • 448,244
  • 59
  • 642
  • 775
  • Is there a way to make GIT write temporary commits in something named not HEAD? – ashvardanian Jul 10 '16 at 01:47
  • No: you can get onto *no* branch (using `git checkout --detach` or `git checkout `), after which `HEAD` contains the raw SHA-1 of the current commit, rather than the name of the branch from which to get that commit ID. But either way, the underlying name here is `HEAD`: it's just a question of whether `.git/HEAD` contains the *raw ID* of a commit, or the *name of a branch* that gives you the ID of the commit. (You can use a named branch for a while, then switch away from it and *delete* the name, at which point all the commits are forgotten and are up for garbage-collection.) – torek Jul 10 '16 at 01:58