Git 常用配置

来源:互联网 发布:unix编程 编辑:程序博客网 时间:2024/05/21 11:19

配置文件综述

关于配置文件的说明:Git-config-files-order官网说明,摘取一部分如下:

$(prefix)/etc/gitconfig
System-wide configuration file.

$XDG_CONFIG_HOME/git/config
Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will be used. Any single-valued variable set in this file will be overwritten by whatever is in ~/.gitconfig. It is a good idea not to create this file if you sometimes use older versions of Git, as support for this file was added fairly recently.

~/.gitconfig
User-specific configuration file. Also called “global” configuration file.

$GIT_DIR/config
Repository specific configuration file.

The files are read in the order given above, with last value found taking precedence over values read earlier.
上面4个地方的文件会被按顺序读取,越往下优先级越高;

一、全局ignore

有时候你需要对某类文件进行全局的忽略而不希望一个个repository的设置。显然这样的需求在配置文件中是被支持的,那么选择什么样的“全局”呢?
个人认为“操作系统用户级别”作为全局是最合适的,毕竟我就是希望“我”这个用户有一个自己的全局配置,事实上Git的global也是这个意思。

  1. 所以根据如上配置文件顺序,我们选择~/.gitconfig文件进行修改:

    [user]    name = xxx    email = xxx@xxx.xxx[core]    excludesFile = ~/.gitignore_global

    在这个文件中我希望全局设置nameemail,当然最重要的还是core.excludesFile(官网配置说明,搜core.excludesFile);这句配置我指定了“配置排除什么文件的”配置文件的位置,即用户目录下的.gitignore_global(名字任意,~代表用户目录):

    **/*.iml   # 任意目录下-后缀为.iml的文件被排除**/target  # 任意目录下-target目录(及其下所有文件)被排除 **/classes # 任意目录下-classes目录(及其下所有文件)被排除 

    这里面具体能怎么配置呢?其实就是一个简单的模式匹配,有官网文档说明,往下翻翻就能看到。

    这样,我们就做好了全局ignoreIDE 们也都会认识的。

  2. Eclipse
    2和3我准备说一下Eclipse和IDEA中怎么配置,一般来说无需配置所以我加了删除线,为什么讲呢,就是防止IDE中原先你做了不同的设置导致1的设置没生效;

    打开Preferences -> Team -> Git -> Configuration
    这里写图片描述

    这里能改变Git的配置文件位置,可以自行看看设置的时候正确。

  3. IDEA
    IDEA其实没啥讲,因为不能在Settings中设置Git配置文件地址,它使用的就是Git约定的位置。
    不过IDEA有另一个可以配置忽略文件的设置,在Settings -> Version Control -> Ignored Files

    这里写图片描述

    这里是IDEA自己的配置和规则和Git无关,不过如果用IDEA开发,这样配置当然也有用。

二、不同repository不同配置

不同repository不同配置,其实就是修改不同下的repository_dir/.git/config文件:

[user]    name = xxx    email = xxx@xxx.xxx[core]    repositoryformatversion = 0    filemode = false    bare = false    logallrefupdates = true    symlinks = false    ignorecase = true[remote "origin"]    url = git@xxx.xxx.xxx:xxx.git    fetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]    remote = origin    merge = refs/heads/master

这有什么用呢?其实目前我用到的最大用处就是公司Git库个人Git库使用的[user]配置不一样,这样的情况就可以通过配置不同的[user.name][user.email]来解决

三、多个SSH key

Git使用SSH时,默认会在~/.ssh目录中找id_rsaid_rsa.pub这个密钥对。不过很多时候,我们在公司使用公司的GitLab和自己使用GitHub是不同的密钥,这时就需要知道怎么设置多对Key的情况。

  1. 首先两对不同的密钥对

    ssh-keygen -t rsa -C "邮箱@公司.com" -f ~/.ssh/<公司名>-id-rsassh-keygen -t rsa -C "邮箱@个人.com" -f ~/.ssh/<个人标识>-id-rsa
  2. 添加配置文件~/.ssh/config

    # 公司Host gitlab.公司.com    PreferredAuthentications publickey    RSAAuthentication yes    IdentityFile ~/.ssh/<公司名>-id-rsa# 个人Host github.com    PreferredAuthentications publickey    RSAAuthentication yes    IdentityFile ~/.ssh/<个人标识>-id-rsa

    Host : 指定匹配的host,全部使用*,多个中间用空格隔开
    PreferredAuthentications:指定使用什么验证方式
    RSAAuthentication:是否使用RSA验证,其实不配也行,默认就是yes
    IdentityFile:验证文件在哪,写上私钥的地址

  3. 最后,别忘了把公钥放到对应git网站上


……待遇见情况后补充

1 0
原创粉丝点击