いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

git init したらメッセージが表示された (init.defaultBranch, 2.31.1)

概要

ちょこちょこ周りの人に、「git init したら英語でメッセージが表示された!」って聞かれるので、ついでにここにメモメモ。。

Git 2.28 から 初期ブランチ に関しての変更が入っています。

github.blog

元々、master という名前のブランチが初期ブランチの名前だったのですが、諸々でその名前を変更できるように調整できるようになりました。

なので、それ以降のバージョンを利用している状態で初期ブランチの名前設定していないと git init したら以下のメッセージが出ます。

$ git version
git version 2.31.1

$ 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 さんは分かりやすいメッセージで教えてくれているのですが、英語でいきなりババーって表示されると焦る人が多いみたいで、どうしたらいいのか?とよく聞かれます。

メッセージをちゃんと読むと丁寧にやり方書いてくれていますので、以下拙訳です。

$ git version
git version 2.31.1

$ git init
hint: 初期ブランチの名前として 'master' を使っていますー. 
hint: このデフォルトブランチ名は今後変更される可能性があるですよー.
hint: この警告を出さないようにして、今後作る全部のリポジトリに対しての初期ブランチ名を設定する場合は
hint: 以下のように設定してくださいね:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: ’main'とか'trunk'とか'development' とかって名前が 'master' の代わりによく利用されますねー.
hint: 今作ったばかりのブランチの名前を変えたい場合は、このコマンドで変更できますぜよ:
hint: 
hint:   git branch -m <name>

試してみる

init.defaultBranch を設定していない状態でリポジトリつくってみます。

$ 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 /home/devlights/tmp/.git/

$ echo 'hello' > hello.txt

$ git add hello.txt
$ git commit -m "Add hello.txt"
[master (root-commit) e1c38a7] Add hello.txt
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

$ git branch --show-current 
master

master って表示されてますね。てことで、今度は初期ブランチ名の設定をしてやってみます。

まず、元に戻したいので .git ディレクトリさんをバイバイします。

$ rm -rf ./.git

で、おもむろに以下のコマンドを実行。

$ git config --global init.defaultBranch main

で、再度 git init をやり直し。

$ git init
Initialized empty Git repository in /home/devlights/tmp/.git/

今度はメッセージが表示されません。つづいてコミットしてみます。

$ git add hello.txt
$ git commit -m "Add hello.txt"
[main (root-commit) 92a162c] Add hello.txt
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

$ git branch --show-current 
main

今度は main ってなってますね。

参考情報

github.blog

www.publickey1.jp

github.com


過去の記事については、以下のページからご参照下さい。

  • いろいろ備忘録日記まとめ

devlights.github.io

サンプルコードは、以下の場所で公開しています。

  • いろいろ備忘録日記サンプルソース置き場

github.com

github.com

github.com