Home > database >  Unable to find remote git branch in remote -- BitBucket
Unable to find remote git branch in remote -- BitBucket

Time:04-21

Switched to a new machine. I need to pull the working branches which was pushed from my old machine to remote. But I am unable to find the remote branches via git command. I could see the branch in BitBucket UI.

This search all is not showing the branches

git branch -all

However I could see my branch in grep search

git ls-remote | grep "my_branch_name"

From ssh://git@my_org.com:xxxx/xxxx/xxx.git
ed18a***************************113aa4ac        HEAD
5b9a1***************************f4aa4f3a        refs/heads/feature/mybranch-10XX
704e2***************************2a4586d5        refs/heads/feature/mybranch-5XX

Is there anyway to fetch these to my new local ?

CodePudding user response:

You already have those branches. They're named origin/feature/mybranch-10XX and origin/feature/mybranch-5XX.

Those aren't branches, though. Instead, they're branches. If you want to work on those branches, you'll need to make branches from those branches. If this makes no sense, you're reading it right: Git overuses the word branch so much that it loses all meaning. (See What exactly do we mean by "branch"?)

To make sense out of this, stop calling things "branch" (or at least, not just branch). Use these words:

  • remote-tracking name: a name like origin/main or origin/feature/mybranch-10XX;
  • branch name (or "local branch", if you prefer): a name like main or feature/mybranch-10XX; and
  • commit: the thing that is identified by a big ugly hash ID.

Git will find the hash ID(s) of commit(s) using the names—the branch names and the remote-tracking names—and will then use those commits to find earlier commits. The set of commits found by using these names ("branches") is also a "branch". So depending on what one means when one says "branch", you either do have the branches (the set of commits), or you do have the branches (the remote-tracking names), or you don't have the branches (you lack the local branch names).

Fortunately, if you have a remote-tracking name such as origin/feature/mybranch-10XX and you call for Git to switch to a (local) branch name:

git switch feature/mybranch-10XX

and that branch name does not exist yet, this invokes the --guess option, which is on by default: git switch or git checkout means git switch --guess or git checkout --guess.1 The guessing code intercepts the error that Git would have given, had you used --no-guess. Git was about to say something to the effect that there is no local feature/mybranch-10XX, but then the guessing code intercepted the error. The guessing code scans through your remote-tracking names looking for one that matches up (after stripping off the origin/ part in this case). If it finds one, it creates a new (local) branch name from the remote-tracking name, identifying the same commit. That commit is now "on" the (local) branch and "on" the remote-tracking name. It may also be on many other branches: commits are often on many branches at the same time, in Git.

In any case, now that git switch or git checkout has used the guessing code to create the branch name, Git can switch to that branch. You will now have that particular commit checked out and can begin making changes and using those to make new commits.


1If you don't want Git to guess, turn this off with --no-guess. In modern versions of Git you can also disable it by default as a configuration option.

  • Related