如何开始使用Git

总结

原本初加入的那帮人在2021年对于Git的部分有太多的增加,因此进行了分离。

由于个人访问令牌存在一些微妙的问题,所以使用SSH进行访问。
本地用户名是hoge。
远程用户名是huga。
远程主要使用GitHub。
如果在GitHub上有多个帐户,则可以使用扩展功能的账户切换器来方便切换。

以下是”.gitignore_global”的设置。

把与操作系统和编辑器相关的部分放入~/.gitignore_global中。
如果是Mac,

gibo dump macOS VisualStudioCode VisualStudio >> ~/.gitignore_global

如果是Windows,

gibo dump Windows VisualStudioCode VisualStudio >> ~/.gitignore_global

中国国家调查局

[ghq]
  root = /path/to/src

/path/to/src被设为/Users/USERNAME/src或C:\\Users\\USERNAME\\src。请注意需要将反斜杠转义为\\。~和$HOME不会被展开。

Mac 上的 fzf

# 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' }

在Windows上使用fzf

# 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"
            }
        }
    }
}

公钥验证

在本地生成密钥对

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注册

设置 > SSH和GPG密钥以配置公钥。

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

別名

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.

登录时需要输入密码。

将git与ssh进行关联。

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

确认远程状态。

git remote -v

使用多个账户轮流使用

准备多个别名

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

切换用户

[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-#中。

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

在~/.gitconfig-1上,可写入用户和ssh信息。

参考资料

    • ローカルに点在するgitリポジトリを一元管理したいアナタに教えたいghq-migrator | Developers.IO

 

    • 6歳娘「パパ、プロジェクトフォルダを見つけるのに何時間かけるの?」【ghq+fzf+zsh】 – Qiita

 

    • さいつよのターミナル環境を構築しよう – Qiita

 

    • GitHubでssh接続する手順~公開鍵・秘密鍵の生成から~ – Qiita

 

    • [備忘] 複数Githubアカウントでssh接続設定(config)を使い分ける手順

 

    • GitHub 接続時の ~/.ssh/config の書き方

 

    git configをプロジェクトによって使い分ける – Qiita
bannerAds