Git(二)- 升级、自动提示、SSH免密登陆

1 git 版本过低,升级

在 CentOS 中,有通过 Yum 安装了低版本的 git,需要做特定的升级处理,相关步骤如下:

  • 先移除旧版本的 git: yum erase git
  • Git 安装中,会对其他包有相关依赖,比如curl-devel、expat-devel、openssl-devel等包:
1
sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
  • Git 为了能够添加更多格式的文档(如 doc, html, info),你需要安装以下的依赖包:
1
2
yum install asciidoc docbook2X xmlto
ln -s /usr/bin/db2x_docbook2texi /usr/bin/docbook2x-texi
  • 下载 Git 安装包, 直接下载:
1
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.18.0.tar.xz
  • 解压后安装:
./configure --prefix=/usr/local
make install install-doc install-html install-info

2 自动提示

2.1 命令行友好提示

1
2
$ wget https://github.com/git/git/blob/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
$ echo "source ~/.git-completion.bash" >> ~/.bashrc

2.2 目录颜色、分支、修改提示

  • 参考 1,基于 bash-git-prompt: git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1
  • 参考 2,git-prompt.sh:https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

3 git ssh 账号配置(免密码输入)

Git 有时需要用户的凭证才能执行操作; 例如,它可能需要输入用户名和密码通过 HTTP 访问远程存储库。

本节描述了 Git 用于请求这些凭证的机制,以及一些需要避免的特性重复输入这些凭证

3.1 基于 ssh 操作流程

参考:https://help.github.com/en/articles/connecting-to-github-with-ssh

  • gitlab 仓库:gitlab/github 仓库;
  • 利用ssh-keygen生成本地 ssh key: ssh-keygen -C "github.com" -f ~/.ssh/id_rsa_github
  • 添加公钥到 gitlab 或者 github 服务器,通常是在设置中心可以找到添加入口;
  • 针对 ssh 配置 config(重要)特别注意 User 用户名是(git),host 是小写
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Terry@home-mac app-soa]$ ll ~/.ssh/config
-rw-r--r--  1 Terry  staff  196  9 25 17:32 /Users/Terry/.ssh/config
[Terry@home-mac app-soa]$ cat ~/.ssh/config
host github.com
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_github
host gitlab.egomsl.com
    Hostname gitlab.egomsl.com
    User git
    IdentityFile ~/.ssh/id_rsa_egomsl
  • 测试
1
2
3
4
[Terry@home-mac app-soa]$ ssh -T git@github.com
Hi tkstorm! You've successfully authenticated, but GitHub does not provide shell access.
[Terry@home-mac app-soa]$ ssh -T gitlab.egomsl.com
Welcome to GitLab, guoluping!
  • 坑点问题,在没有~/.ssh/config 时候,手动创建的话,需要设置好文件权限
1
2
3
4
//Bad owner or permissions on ~/.ssh/config
chmod 600 ~/.ssh/config
或者
chown $USER ~/.ssh/config

3.2 基于 credential.helper

gitcredentials - providing usernames and passwords to Git

通过git config --help 搜索关键字credential,另外可以通过man 7 gitcredentials查看更多内容。避免重复反复输入相同的凭证可能很麻烦。Git 提供了两种方法来减少这种麻烦:

  1. 给定身份验证上下文的用户名的静态配置。
  2. 用于缓存或存储密码,或与系统密码钱包或密钥链进行交互的凭据帮助程序。

3.2.1 凭证上下文

Git 认为每个凭证都有一个由 URL 定义的上下文。此上下文用于查找特定于上下文的配置,并传递给任何助手,它可以将其用作安全存储的索引。

例如,假设我们正在访问https://example.com/foo.git。当 Git 查看配置文件(~/.gitconfig)以查看某个部分是否匹配此上下文(两个协议是相同的,两个主机是相同的,不关心路径组件),会采用第一个证书凭证:

1
2
3
4
5
[credential "https://example.com"]
    username = foo
    helper = cache --timeout 7200
[credential "https://kernel.org"]
    username = foo

上述https://example.com等价于通过命令行:git config credential.https://example.com.helper 'cache --timeout 7200'

3.2.2 相关命令行设置参考

通过git config --help 搜索关键字credential

  • credential.helper
  • credential.useHttpPath
  • credential.username
  • credential..*
  • credentialCache.ignoreSIGHUP

3.2.3 凭证助手(cache、store)

凭证助手是 Git 可以同时请求用户名和密码的外部程序;它们通常与 secure 接口操作系统或其他程序提供的存储。 要使用助手,您必须首先选择要使用的一个。Git 目前包括以下帮助:

  • cache: 将凭证在内存中缓存一小段时间。有关详细信息,请参见 git-credential-cache(1)。
  • store: 将凭证无限期地存储在磁盘上。有关详细信息,请参见 git-credential-store(1)。

您也可以安装第三方帮助程序;在git help -a的输出中搜索credential-*,从中可以选择了一个 helper,然后就可以通过将其名称放入凭证中来告诉 Git 使用它作为辅助变量:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 1. Find a helper.
$ git help -a | grep credential-
  credential-cache          remote-ext
  credential-cache--daemon  remote-fd
  credential-netrc          remote-ftp
  credential-osxkeychain    remote-ftps
  credential-store          remote-http

// 2. Read its description.
$ git help credential-store
$ git help credential-cache

git-credential-store - Helper to store credentials on disk
git-credential-cache - Helper to temporarily store passwords in memory
....

// 3. Tell Git to use it.
$ git config --global credential.helper store

// 4. Push a repos and input pass
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

// 5. [several days later]
$ git push http://example.com/repo.git
[your credentials are used automatically]

4 git config 初始化

4.1 层次关系:库级别 > global 级别 > 系统级别

1
2
3
4
5
6
7
8
9
// 命令行配置
git config --system
git config —global
git config

// 存储位置
/etc/gitconfig
~/.gitconfig
.git/config

4.2 相关 git 的全局设置:

1
2
3
4
git config --global user.email "tkstorm1988@gmail.com”
git config --global user.name "TkWeber”
git config --global push.default simple
git config --global core.editor vim

配置模板:如果你有特定的策略要运用在提交信息上,在系统上创建一个模板文件,设置 Git 默认使用它,这样当提交时,你的策略每次都会被运用:git config --global commit.template $HOME/.gitmessage.txt

5 参考