1 git 版本过低,升级
在 CentOS 中,有通过 Yum 安装了低版本的 git,需要做特定的升级处理,相关步骤如下:
- 先移除旧版本的 git:
yum erase git
- Git 安装中,会对其他包有相关依赖,比如
curl-devel、expat-devel、openssl-devel
等包:
|
|
- Git 为了能够添加更多格式的文档(如 doc, html, info),你需要安装以下的依赖包:
|
|
- 下载 Git 安装包, 直接下载:
|
|
- 解压后安装:
./configure --prefix=/usr/local
make install install-doc install-html install-info
2 自动提示
2.1 命令行友好提示
|
|
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 是小写
|
|
- 测试
|
|
- 坑点问题,在没有~/.ssh/config 时候,手动创建的话,需要设置好文件权限
|
|
3.2 基于 credential.helper
gitcredentials - providing usernames and passwords to Git
通过git config --help
搜索关键字credential
,另外可以通过man 7 gitcredentials
查看更多内容。避免重复反复输入相同的凭证可能很麻烦。Git 提供了两种方法来减少这种麻烦:
- 给定身份验证上下文的用户名的静态配置。
- 用于缓存或存储密码,或与系统密码钱包或密钥链进行交互的凭据帮助程序。
3.2.1 凭证上下文
Git 认为每个凭证都有一个由 URL 定义的上下文。此上下文用于查找特定于上下文的配置,并传递给任何助手,它可以将其用作安全存储的索引。
例如,假设我们正在访问https://example.com/foo.git
。当 Git 查看配置文件(~/.gitconfig
)以查看某个部分是否匹配此上下文(两个协议是相同的,两个主机是相同的,不关心路径组件),会采用第一个证书凭证:
|
|
上述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 使用它作为辅助变量:
|
|
4 git config 初始化
4.1 层次关系:库级别 > global 级别 > 系统级别
|
|
4.2 相关 git 的全局设置:
|
|
配置模板:如果你有特定的策略要运用在提交信息上,在系统上创建一个模板文件,设置 Git 默认使用它,这样当提交时,你的策略每次都会被运用:git config --global commit.template $HOME/.gitmessage.txt
5 参考
- Git 官档:https://git-scm.com/downloads
- git-completion:https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
- git-git-prompt.sh: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
- bash-git-prompt: https://github.com/magicmonty/bash-git-prompt.git
- connecting-to-github-with-ssh:https://help.github.com/en/articles/connecting-to-github-with-ssh