🔧 Gitのデフォルトブランチ名を変える

なぜ変える?

Githubのデフォルトブランチ名である「master」が色々と問題になったそうで... 現在のデフォルトブランチ名は「main」となっている。

ローカルでgit initした時のデフォルトブランチ名に出てくる下記のコードが表示されいちいちブランチ名の変更をするのがめんどくさかったというのもあり、デフォルトブランチ名を「main」に変えてしまおうということ。

git init時に表示されたヒント

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>

デフォルトブランチ名を変更するコードたち

Gitのバージョンを2.28.0以降に

まず、ブランチ名を変更するにはGitのバージョンが2.28.0以降でなければいけないようです。

確認方法

git --version

アップデート方法

Mac OSの方はこちら Windowsの方はこちら

Mac OSの場合

brew install git

Windowsの場合

git update-git-for-windows

いざ変更

git config --global init.defaultBranch main