aormsbyEIT
aormsbyEIT

Reputation: 51

Can I change the default git branch in Android Studio?

The default git branch in Android Studio is 'master'. However, Github has updated its default to 'main', and I'd like to keep consistent with that. Is there any way to change the default git branch when using version control within Android Studio?

Clarification: I know that I can switch branches after the repo is created, but let's say there is no repo yet. I go to VCS > Import into Version Control > Share Project on Github, and it creates a repo with default settings, 'master' branch. I want to change the settings so that the default branch on creation is 'main' or whatever I want it to be.

I don't want to have to create a new branch after the repo is initialized. If I'm going to take extra steps, I'll just use the CLI. Just not what I'm looking for.

Upvotes: 5

Views: 4024

Answers (4)

Synth
Synth

Reputation: 95

I stumbled upon this problem as well and after searching all over this worked for me. Open the terminal tab in the bottom left of Android Studio, then enter

git config --global init.defaultBranch main

After enabling VCS for a new project, the branch is automatically set to main instead of master. You will still need to change the branch name for existing repos.

To confirm your default branch at anytime (before or after the previous command), simply omit the branch name

git config --global init.defaultBranch

Credit goes to VonC's answer. Instructions to change existing repo branch also in his answer.

EDIT: So it didn't end up working for me. According to further research,

"Git has always created an initial first branch with the name master ... Starting in Git 2.28, git init will instead look to the value of init.defaultBranch when creating the first branch in a new repository."

Since I am using Linux Mint 20.2, which is based on Ubuntu Focal, my version of Git is stuck at 2.25. This might explain why it doesn't work for me.

Looking through all the Settings in the IDE, I was unable to find an option to change the default branch. However, someone in Jetbrain's YouTrack confirmed that

"the IDE does not instruct git to use any specific name, so there is no need to wait for any specific support to rename the branch. Git 2.28 introduced the setting to control the default branch name ... So you can set it globally to have main (or any other name) in new repositories".

In summary, try installing a version of Git that is 2.28 or above, and with the mentioned init.defaultBranch configuration variable, it should work.

FINAL EDIT: On my new workstation with Git 2.37 it works as promised above. If you want new projects to be set up with your default git branch, follow my instructions using git 2.28 or higher.

Upvotes: 1

Sidharth Mudgil
Sidharth Mudgil

Reputation: 1461

Make main default branch

  1. open git bash terminal
  2. type git config --global init.defaultBranch main

this will globally change the default branch to the main

Renaming Local branch

  1. Open git bash in the project directory
  2. Type $ git branch -m master main

Renaming Remote branch

  1. First push main branch using git push -u origin main
  2. Now delete the master branch git push origin --delete master

for detailed solution checkout

Upvotes: 3

Dharmik Thakkar
Dharmik Thakkar

Reputation: 2168

You can rename your local branch created to the name you want. For example master to main so that local branch matches to your remote branch. Follow the steps:

  1. In case of android studio go to the bottom right corner and tap on your current branch (in my case its master):enter image description here

  2. Select the local branch you want to rename: enter image description here

  3. Rename your local branch, In my case I am renaming my master branch to main. enter image description here

Upvotes: 2

Emon Hossain Munna
Emon Hossain Munna

Reputation: 755

Yes there is an option to change that thing, i have marked the option in image attachment. hope that would help you!

enter image description here

Upvotes: 4

Related Questions