Main/Master mismatch between local and remote repositories
Asked by Josh Soong:

I initialized a new repository in Bitbucket, which created a 'main' branch. Then, using the Salesforce DX plugins in VS Code, I created a local copy of our Salesforce test environment in VS Code.

In VS code, my repository's primary branch is called 'master'. When I push from VS code to Bitbucket, instead of pushing onto the 'main' branch, it creates a new 'master' branch. So now I have 'main' and 'master'.

I eventually figured out how to merge 'master' into 'main', but the very next time I tried to push again from VS code into bitbucket, it created another 'master' branch instead of adding to 'main'.

Additionally, bitbucket says I have no repositories found in this workspace, although I have authorized bitbucket.

How do I change either Bitbucket or my local git repository so that the names of the branches match each other?


Answers:
author: Theodora Boudale
body: Hi Josh and welcome to the community.

First, allow me to say that I am not very familiar with VS Code. Most Git GUI applications offer the ability to set which remote branch a local branch is tracking. If you can see a list of branches in VS Code, perhaps right-clicking the 'master' branch will show you and give you the ability to change the remote tracking branch to 'main' instead of 'master'.

From the description of your issue, it sounds like your local branch 'master' is tracking a remote branch named 'master', which would explain why pushing 'master' branch creates a 'master' branch in the Bitbucket repo.

If you don't see an option to change this from VS Code, I can let you know how to change this from command line:

If your want your local branch 'master' to track the remote branch 'main', you can open a command line tool/Git Bash, navigate to the directory of the repo, and then run the command:

```
git branch master -u origin/main
```

If you want, you can also rename your local branch to main, to avoid any confusion. First, make sure that 'master' branch is checked out locally. Then, rename it to 'main' with the command

```
git branch -m main
```

and set origin/main as the remote tracking branch with

```
git branch main -u origin/main
```

The commands above assume that the repo's remote is named 'origin', which is the default name created for a remote.  
You can confirm if this is the case for your repo by running the command

```
git remote -v
```

If you see in the output

```
origin <bitbucket_repo_url> (fetch)
origin <Bitbucket_repo_url> (push)
```

then this confirms the remote's name is indeed origin. If you see a different name, you'll need to use that one instead of 'origin' in the commands above.

Please feel free to let me know if this helps and if you have any questions.

Kind regards,  
Theodora

comments: author: Josh Soong
body: This was a wonderfully detailed reply, thank you so much. I'm new to git so the commands are very helpful.

I'm still a little confused about which url should be considered the "correct" url for a Bitbucket repository.

In the web browser, the url to the repository looks like: Bitbucket.org/ourgroup/ourproject

however if I click the "clone" button it pulls up a url that looks like MyUsername@bitbucket.org/ourgroup/ourproject.git

I had set the one with .git as the origin, so now I have a remote "main" that looks like the first url and a remote origin that looks like the second. I don't know if I can or should change the url associated with origin or if it doesn't really matter.


author: Theodora Boudale
body: Hi Josh,

When you open a repository in your browser, the URL is indeed <https://bitbucket.org/workspace-id/repo-slug/>

If you click the **Clone** button, then the clone command for HTTPS authentication (username+password) will look like this indeed

https://Bitbucket-username@bitbucket.org/workspace-id/repo-slug.git

It includes your own Bitbucket username, and that URL is set as a remote in your local clone. This way, you will not be asked to enter your Bitbucket username every time you pull from or push to the Bitbucket repo from your local one.

There is no need to change the origin if the URL looks like https://Bitbucket-username@bitbucket.org/workspace-id/repo-slug.git, this is correct.

I am a bit confused also about the remote 'main' that you mentioned. If you execute the command

```
git remote -v
```

do you see a remote named 'main' listed in there?

There is no need to set a second remote named 'main'. You just need to set your local branch named 'master' (or 'main', if you have renamed your local branch), to track the remote branch origin/main.

If there's still confusion/issues and you haven't managed to resolve this, could you let me know what is the output of the following commands in your local clone?

```
git remote -v
git branch -a
git branch -vv
```

Please feel free to sanitize the URLs in the output of the first command prior to sharing it, but leave the branches names in the output of the last two commands if that's ok, so we can check what the current status of the branches are.

Kind regards,  
Theodora


author: Josh Soong
body: Thank you again for your help! I did rename my local branch to 'main'. When I run the git remote -v command, this is what I see:

```
main https://bitbucket.org/workspace-id/repo-slug (fetch)
main https://bitbucket.org/workspace-id/repo-slug (push)
origin https://Bitbucket-username@bitbucket.org/workspace-id/repo-slug.git (fetch)
origin https://Bitbucket-username@bitbucket.org/workspace-id/repo-slug.git (push)
origin https://Bitbucket-username@bitbucket.org/workspace-id/repo-slug.git (push)
```

So it looks like I have a main and an origin. The other weird thing is it looks like my last 2 lines are identical, I'm not sure if it's supposed to be that way or not.

git branch -a produces

```
* main
```

and git branch -vv produces

```
* main b4409b8 more formatting of class getPOPtest
```

Also, when I run "git push main" I can see in Bitbucket that my push succeeded.


author: Theodora Boudale
body: Hi Josh,

Thanks for the info!

I don't believe that you need the remote 'main', mentioned in the output of 'git remote -v'.  
What is configured there is the remote repository (and not the remote branches), so just 'origin' should be ok.  
You may remove the remote 'main' with the following command inside the repo directory:

```
git remote rm main
```

I am not sure why there are two lines with

```
origin https://Bitbucket-username@bitbucket.org/workspace-id/repo-slug.git (push)
```

Normally there should be only one. You can try removing and re-adding the remote 'origin', to see if this helps fix the issue:

```
git remote rm origin
git remote add origin https://Bitbucket-username@bitbucket.org/workspace-id/repo-slug.git
```

It's also strange that git branch -a doesn't list the remote branches as well, normally you'd see something like

```
* main
? remotes/origin/main
```

You can try running the following command again, after you sort out the remotes:

```
git branch main -u origin/main
```

It looks that there are no issues now since the push succeeds, but I'd still suggest sorting out the above, just to make sure that the set up is correct.

Please feel free to let me know if you have any questions!

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud