LoginSignup
0
0

More than 1 year has passed since last update.

Gitの始め方

Last updated at Posted at 2021-10-11

Summary

最初に入れるやつら2021に付け足すgitのパートが長いので分離。

Personal access tokensは微妙なのでsshでアクセス。
ローカルのユーザー名はhoge
リモートのユーザー名はhuga
リモートは、基本的にGitHubを使用。
GitHubで複数アカウントがある場合は、拡張機能のAccount Switcherが便利。

~/.gitignore_globalの設定

OSとエディタに由来する部分は、~/.gitignore_globalに入れとく。
Macなら、

gibo dump macOS VisualStudioCode VisualStudio >> ~/.gitignore_global

Windowsなら、

gibo dump Windows VisualStudioCode VisualStudio >> ~/.gitignore_global

ghq

~/.gitconfig
[ghq]
  root = /path/to/src

/path/to/srcは、/Users/USERNAME/srcC:\\Users\\USERNAME\\srcにしている。バックスラッシュをエスケープして\\にする必要があることに注意。~$HOMEは展開されない。

fzf on Mac

~/.zshrc
# git
# Exit with esc while choosing directory
compdef _g g
function g(){
  case "$1" in
    root )
      git config ghq.root;;
    * )
      target="$(ghq list -p | sort -fV | fzf)"
      echo $target
      if [ -n "$target" ]; then 
        cd "$target"
      fi;;
  esac
}
function _g() { _values '' 'root' }

fzf on Windows

~\Documents\PowerShell\profile.ps1
# git
# Exit with esc while choosing directory
function g () {
    switch ($Args[0]) {
        'root' {
            git config ghq.root
        }
        default {
            $target="$(ghq list -p | sort | fzf)"
            # `ghq list -p`が動かないとき
            # $target = "$((Get-ChildItem -Path $(ghq root) -Depth 3 -Hidden -Filter .git).parent.FullName | sort | fzf)"
            echo $target
            if ($target) {
                cd "$target"
            }
        }
    }
}

Public key authentication

Key pairをローカルで作成

ssh-keygen
> Enter file in which to save the key (/path/to/.ssh/id_rsa): /path/to/.ssh/github
> Enter passphrase (empty for no passphrase):

/path/to/.ssh は、/Users/hoge/.sshとか。
passphraseは、くれぐれも忘れないように。

GitHubに登録

Settings > SSH and GPG keysに公開鍵の設定する。

pbcopy < /path/to/.ssh/github.pub

alias

~/.ssh/config
Host github
    HostName github.com
    User git
    Port 22
    IdentityFile /path/to/.ssh/github
    UseKeychain yes
    AddKeysToAgent no

疎通確認

ssh -T github
> Enter passphrase for /path/to/.ssh/github:
> Hi huga! You've successfully authenticated, but GitHub does not provide shell access.

passphraseがログイン時に必要。

gitsshをリンク

~/.gitconfig
[url "github:"]
    InsteadOf = https://github.com/
    InsteadOf = git@github.com:

リモートを確認する。

git remote -v

複数のアカウントを使い分け

複数のアライアスを準備

~/.ssh/config
Host github-1
    HostName github.com
    User git
    Port 22
    IdentityFile /path/to/.ssh/github-1
    UseKeychain yes
    AddKeysToAgent no

Host github-2
    HostName github.com
    User git
    Port 22
    IdentityFile /path/to/.ssh/github-2
    UseKeychain yes
    AddKeysToAgent no

疎通確認

ssh -T github-1
ssh -T github-2

ユーザーの切り替え

~/.gitconfig
[user]
	name = 
	email = 
[includeIf "gitdir:/path/to/src/github.com/huga-1/"]
	path = ~/.gitconfig-1
[includeIf "gitdir:/path/to/src/github.com/huga-2/"]
	path = ~/.gitconfig-2

ディレクトリごとに~/.gitconfig-#へ振り分ける。

~/.gitconfig-#
[user]
	name = huga-#
	email = xxxxxxxx+huga-#@users.noreply.github.com
[url "github-#:"]
	InsteadOf = https://github.com/
	InsteadOf = git@github.com:

# = 1, 2
~/.gitconfig-#に、ユーザーやsshの情報を書き込む。

References

0
0
0

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
0
0