LoginSignup
1068
1070

More than 3 years have passed since last update.

Gitをインストールしたら真っ先にやっておくべき初期設定

Last updated at Posted at 2013-10-25

概要

Git 初期設定の鉄板です。
何回やっても忘れるのでメモ。
気がついたら追記していきます。

2018-06-06 もう Git 2.17 ですよ。この記事は 2013 年代ぐらいに書かれた記事です。

ユーザー情報を設定する

最近の Git はこれを設定しないとエラーを吐くようになりました。いい機会です、是非設定しましょう。

git config --global user.name "First-name Family-name"
git config --global user.email "username@example.com"

エディタを Vim に設定する

コミットログを書くとき、 Ubuntu のデフォルトだと nano に設定されてしまうのでこれをvimに修正する。

git config --global core.editor 'vim -c "set fenc=utf-8"'

そもそも nano なんて使わねーよという時

ブコメや、 @fumiyas さんからも指摘がありましたが、 nano 自体を全体的にそもそも使いたくないときは

  • nano は削除する
sudo dpkg -P nano
  • ほかのエディタをデフォルトにする
export GIT_EDITOR=

or

export EDITOR=

or 私のやった方法は以下です。

$ sudo update-alternatives --config editor
alternative editor (/usr/bin/editor を提供) には 4 個の選択肢があります。

  選択肢    パス              優先度  状態
------------------------------------------------------------
* 0            /bin/nano            40        自動モード
  1            /bin/ed             -100       手動モード
  2            /bin/nano            40        手動モード
  3            /usr/bin/vim.basic   30        手動モード
  4            /usr/bin/vim.tiny    10        手動モード

現在の選択 [*] を保持するには <Enter>、さもなければ選択肢の番号のキーを押してください: 3
update-alternatives: /usr/bin/editor (editor) を提供するためにマニュアルモードで /usr/bin/vim.basic を使います

その他

git diff に色付けしたい

git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

push 方式を指定

Git 1.8系 以降で使える。というか警告が表示される。
新しい Git だと警告が表示されるので push 方式を明示する。

git config --global push.default simple

Git 1.8系以前 だとこれを設定すると Malform だって言われて以下の様なエラーになっちゃう。ひい。

error: Malformed value for push.default: simple
error: Must be one of nothing, matching, tracking or current.
fatal: bad config file line 8 in /home/wnoguchi/.gitconfig

以下のようにして削除する。

git config --global --unset push.default

詳しくは以下を参照。

Macユーザーに贈るUTF-8問題解決設定(macOS Sierraまで)

Git 1.7.12以降で使える。
Mac ユーザーの中では有名ですが、濁点つきのディレクトリ・ファイルが分けて表示されてしまう UTF8-MAC 問題の解決方法。
忘れがちなので注意。

2018-01-13 追記
以下の手順は macOS High Sierra 以降については標準のファイルシステムが APFS になったので未来の若者には無問題なのかな。

  • 今後クローンするリポジトリに対して有効になる
git config --global core.precomposeunicode true
  • 既存リポジトリの .git/config に対してすること
git config --local core.precomposeunicode true

git status とかで表示される日本語ファイル名がエスケープされてうざい

git config --global core.quotepath false

コミットに PGP/GPG で署名したい

git config --global gpg.program gpg
git config --global user.signingkey <<your key id>>
git config --global commit.gpgsign true
wnoguchi@lasthope:~/repos/dotfiles$ git log -n1 --show-signature
commit 88f8c02ddae9253ef28faf6f00ca862ee424ffde (HEAD -> master, origin/master)
gpg: Signature made Sat 06 Feb 2021 07:15:00 PM JST
gpg:                using RSA key EB46F8D643EF3A7CD686C002B4A5CEBBF13A8F59
gpg: Good signature from "Wataru Noguchi <wnoguchi@pg1x.com>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: EB46 F8D6 43EF 3A7C D686  C002 B4A5 CEBB F13A 8F59
Author: Wataru Noguchi <wnoguchi@pg1x.com>
Date:   Sat Feb 6 19:14:26 2021 +0900

    Initial commit.

詳細はこちら

  1. git(GitHub)でGPGを使った署名をおこなう - Qiita
  2. GitHubにgpg署名付きのコミットをする - Qiita

まとめ

以下のスクリプトを流し込むとそれっぽく初期設定してくれる。

git-bootstrap.sh
#!/bin/bash
git config --global user.name "First-name Family-name"
git config --global user.email "username@example.com"
git config --global core.editor 'vim -c "set fenc=utf-8"'
git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto
git config --global push.default simple
git config --global core.precomposeunicode true
git config --global core.quotepath false

# PGP/GPG 使いたいとき
git config --global gpg.program gpg
git config --global user.signingkey <<your key id>>
git config --global commit.gpgsign true

現在の設定を確認する

% git config --list
user.name=Wataru Noguchi
user.email=username@example.com
core.quotepath=false
core.excludesfile=/Users/noguchiwataru/.gitignore_global
core.precomposeunicode=true
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
push.default=simple

参考サイト

  1. git 1.7.12でUTF8-MAC問題が解決 | Butaman-kun Project
  2. git push時に表示されるwarning: push.default is unset...の意味と解決方法 - Qiita キータ
  3. 引数なしのgit pushは危険なので気をつけましょう - DQNEO起業日記
  4. gitのコミットログ編集用エディタをvimにする - Sticker@Something
  5. git git diff で色付け - mat_akiの日記
  6. git config --global で追加した設置値を削除したい場合 - knt45の日記
  7. git(GitHub)でGPGを使った署名をおこなう - Qiita
  8. GitHubにgpg署名付きのコミットをする - Qiita
1068
1070
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1068
1070