環境変数の設定
環境変数の設定場所は3箇所存在する
以下コマンドで確認できる
# Gitの全ての設定を確認する
git config --list
# システム全体
git config --system --list
# HOMEディレクトリ配下の.gitconfig cd ~/.gitconfig
git config --global --list
# ローカルリポジトリ内の.git/config
git config --local --list
最低限必要な初期設定は以下の通り。
git config --global user.name {username}
git config --global user.email {emailaddress}
git config --global --list
ちょっと便利な設定(Tab補完機能など)
Linuxシェル上での設定。
sudo mkdir -v /usr/local/etc/bash_completion.d
sudo wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O /usr/local/etc/bash_completion.d/git-completion.bash
sudo chmod 755 /usr/local/etc/bash_completion.d/git-completion.bash
sudo wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O /usr/local/etc/bash_completion.d/git-prompt.sh
sudo chmod 755 /usr/local/etc/bash_completion.d/git-prompt.sh
Linuxの設定ファイルに記載しておくと良い設定。
※Terminalの起動シェルが.bashrcであることを事前に確認しておく。
(.zshrcの設定されている場合は変更が必要)
# bashrcファイルの編集
vim ~/.bashrc
# bashrcファイルに追記
# git settings
source /usr/local/etc/bash_completion.d/git-prompt.sh
source /usr/local/etc/bash_completion.d/git-completion.bash
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_SHOWCOLORHINTS=true
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
# 環境をリロード
source ~/.bashrc
ちょっと便利な設定(エイリアスなど)
ローカルのgitプロジェクト配下に.gitconfigファイルを作成して設定。
# ファイルを作成
touch .gitconfig
# ファイルを編集
vim .gitconfig
# エイリアスの設定を追記
[alias]
st = status
co = checkout
ri = rebase -i
lv = log --graph --branches --tags --remotes --pretty=format:'%Cred%h%Creset - %s%C(yellow)%d%Creset %Cgreen(%cr:%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date=iso
b = branch
ba = branch -a
fap = fetch --all --prune
pff = pull --ff
※git lvの意味
HEAD -> (master, origin/master, origin/HEAD)
ローカルの現在地 -> (ローカルのmasterブランチ, 追跡中のローカルブランチ, gitHub上のデフォルトリモートブランチ)
プッシュ時のユーザー名・パスワードの自動化
### ファイルとして認証情報を保存
# cloneもしくはpushすると、~/.git-credentials配下にパスワードファイルが生成される
git config --global credential.helper store
### キャッシュとして一定期間、認証情報を保存
# .git > configに情報が保存される。(--localでも問題ない。その場合は対象のプロジェクトのみに有効)
git config --global credential.helper cache
git config credential.helper 'cache --timeout=300'
### 登録した情報の削除
# ~/.gitconfigで登録した情報を削除
git config --global --unset credential.helper
# .git > localで登録した情報を削除
git config --local --unset credential.helper