ブログタイトル募集中の新人SE記

ネットの片隅で非力を嘆く

とりあえずコレだけはやっておきたいGit初期設定

Git初期設定

ssh接続

cd ~/.ssh
ssh-keygen -t rsa

下記みたくなんか聞かれるが3回Enterでよい
(1つ目が鍵の名前,2つ目、3つ目はパスフレーズの設定)

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

鍵が生成されたら公開鍵をクリップボードにコピー
例) vim id_rsa.pubで開いてssh-rsa含めて全てコピー

https://github.com/settings/ssh にアクセス

画面右上の「Add SSH key」からコピーした公開鍵を登録

git config系

ユーザ名/メアド登録

git config --global user.name "<user_name>"
git config --global user.email "address@example.co.jp"

色付け

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

push方式

git config --global push.default simple

改行コードをCRLFに変更しようとするのを阻止

git config --global core.autoCRLF false

git関連パッケージ

git-completion

gitコマンドを補完
- 下記コマンド実行

cd ~/
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
mv git-completion.bash .git-complation.bash
source .git-complation.bash
  • ~/.bash_profile に下記を追加
if [ -f ~/.git-completion.bash ] ; then
  . ~/.git-completion.bash
fi

上記でブランチ名がうまく補完されなかった場合↓(動くとは言ってない)

rm .git-complation.bash
find / -name git-completion.bash
cp <findコマンドで見つかったパス> ~/.git-completion.bash
source .git-complation.bash

git-prompt

プロンプトに現在のブランチを表示

cd ~
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
mv git-prompt.sh .git-prompt.sh
vim .bashrc

末尾に下記を追加

if [ -f ~/.git-prompt.sh ]; then
    source ~/.git-prompt.sh
    PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
fi

gitコマンド追加(エイリアス)

logをいい感じのグラフで表示

cd ~/
vim .gitconfig

末尾に下記を追加(既にエイリアス登録している場合はそこに)

[alias]
lol = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
lola = log --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

個人環境依存の設定ファイルをgit管理から外す

  • IDEの設定ファイル等、Git管理下から除外したいファイルを以下に記述
vim ~/.gitignore_global

例)

# .gitignore_global
# ここに個人環境依存の設定ファイル等を記述
.ftpconfig
  • 上記ファイルをgitが見てくれるように設定
git config --global core.excludesfile $HOME/.gitignore_global

設定されると.gitconfigに下記が追加される

[core]
    excludesfile = /home/${USERNAME}/.gitignore_global