Gitコマンドのデフォルトブランチをmainにする

Gitのデフォルトブランチはmasterですが,masterからslaveという言葉を連想して一部に人が不愉快に感じるので使わないようにしようという動きが2年ほど前からあって各種サービスのデフォルトブランチもmainになっています.(witelist/blacklistなんかも)
でも手元のGitコマンドのデフォルトはmasterなので既定値を変更してみました.

まず普通にリポジトリを作るとmasterになります.ただ,ヒントは表示されます.
git branch -m <新しいブランチ名> でブランチ名の変更ができました.

$ cd /tmp/tmp.07sONCJp59
$ 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>
Initialized empty Git repository in /tmp/tmp.07sONCJp59/.git/
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
$ echo test > test
$ git add test
$ git commit -m 'test'
[master (root-commit) c27747d] test
 1 file changed, 1 insertion(+)
 create mode 100644 test
$ git branch
$ PAGER=cat git branch
* master
$ git branch -m master main
$ PAGER=cat git branch
* main
$ git log
$ tig
$ echo main > main
$ git add main
$ git commit -m 'mainにしてからの初めてのコミット'
[main dc3230d] mainにしてからの初めてのコミット
 1 file changed, 1 insertion(+)
 create mode 100644 main
$ git status
On branch main
nothing to commit, working tree clean

デフォルトブランチを変更してみます.

man git-config より init.defaultBranch で変更できそうです.

init.defaultBranch
    Allows overriding the default branch name e.g. when initializing a new repository.

実際に設定してみます.
git config --global init.defaultBranch main でデフォルトブランチを main に設定して ~/.gitconfig に設定されているのを確認,実際にリポジトリを作ると main になりました.

$ mktemp -d
/tmp/tmp.QBuop4f5Vn
$ cd /tmp/tmp.QBuop4f5Vn
$ git config --global init.defaultBranch main
$ grep Branch ~/.gitconfig
        defaultBranch = main
$ git init
Initialized empty Git repository in /tmp/tmp.QBuop4f5Vn/.git/
$ PAGER=cat git branch
$ echo test > test
$ git add test
$ git commit -m '1st commit'
[main (root-commit) dfe208a] 1st commit
 1 file changed, 1 insertion(+)
 create mode 100644 test
$ git status
On branch main
nothing to commit, working tree clean
$ PAGER=cat git branch
* main
環境
$ dpkg-query -W git
git     1:2.36.1-1
$ lsb_release -dr
Description:    Debian GNU/Linux bookworm/sid
Release:        unstable
$ arch
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)