简体   繁体   中英

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.

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

在此处输入图像描述

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 ): 在此处输入图像描述

  2. Select the local branch you want to rename: 在此处输入图像描述

  3. Rename your local branch, In my case I am renaming my master branch to main . 在此处输入图像描述

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.

I hope.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM