• 『现学现忘』Git基础 — 11、配置Git用户签名的方式


    1、配置Git签名

    (1)语法

    python
    $ git config 配置文件作用域 user.name '用户名'
    $ git config 配置文件作用域 user.email '邮箱地址'

    示例如下:

    配置 user.name和user.email
    $ git config --global user.name ‘your_name'
    $ git config --global user.email ‘your_email@domain.com'

    注意:这个email一定是有效的,是你能够收得到邮件的email

    (2)配置系统用户签名

    可在任意目录下运行创建命令:git config --system

    bash
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ git config --system user.name 'tang_s'
    
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ git config --system user.email 'tang_s@126.com'

    提示:在Git中,没有提示就是最好的提示。

    系统用户注册信息会写在本地Git的安装目录下,...\etc\gitconfig文件中。

    bash
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ cat /f/DevInstall/Git/GitforWindows/etc/gitconfig
    [diff "astextplain"]
            textconv = astextplain
    [filter "lfs"]
            clean = git-lfs clean -- %f
            smudge = git-lfs smudge -- %f
            process = git-lfs filter-process
            required = true
    [http]
            sslBackend = openssl
            sslCAInfo = F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
    [core]
            autocrlf = true
            fscache = true
            symlinks = false
    [credential]
            helper = manager
    [user]
            name = tang_s
            email = tang_s@126.com

    提示:之前的Git版本中,gitconfig文件是在,Git安装目录下mingw64目录中的etc/gitconfig

    (3)配置全局用户签名

    可在任意目录下运行创建命令:git config --global

    bash
    # 在任何位置执行都可以
    # 执行这个配置表示你这台机器上所有的Git仓库都会使用这个配置,
    # 当然也可以对某个仓库指定不同的用户名和Email地址(本地用户)。
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ git config --global user.name 'sun_wk'
    
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ git config --global user.email 'sun_wk@126.com'

    全局用户注册的信息,会写到当前用户目录下.gitconfig文件中。

    bash
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ cat /c/Users/L/.gitconfig
    [user]
            name = sun_wk
            email = sun_wk@126.com

    (4)配置本地用户签名

    本地库用户只能在当前的本地库目录下运行该命令:

    python
    # 注意如果是配置local配置文件签名,可以省略--local参数
    $ git config --local user.name 'sha_hs'
    $ git config --local user.email 'sha_hs@126.com'

    注意:

    执行上边命令,要在一个仓库中执行,否则会提示你:

    fatal: --local can only be used inside a git repository

    还有--local选项只能在Git仓库中使用。

    演示:

    bash
    # 此时learngit目录不是一个本地Git仓库
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ git config --local user.name ''
    fatal: --local can only be used inside a git repository
    
    # 初始化learngit目录为Git本地仓库
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit
    $ git init
    Initialized empty Git repository in J:/git-repository/learngit/.git/
    
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    
    # 配置本地用户签名
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ git config --local user.name 'sha_hs'
    
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ git config --local user.email 'sha_hs@126.com'

    本地用户注册信息,会写到当前版本库目录下的.git目录中的config文件中。

    bash
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ cat .git/config
    [core]
            repositoryformatversion = 0
            filemode = false
            bare = false
            logallrefupdates = true
            symlinks = false
            ignorecase = true
    [user]
            name = sha_hs
            email = sha_hs@126.com

    注意:

    • 签名设置的用户名和邮箱,主要作用就是区分不同开发人员的身份。
    • 这里设置的签名和登录远程库,也就是代码托管中心的账号、密码没有任何关系。
    • Email地址没有要求和用户名一致,甚至Email地址不存在都没事。
    • 但是在实际工作中,这个Email一定是有效的,是你能够收得到邮件的Email。

    2、查看三个配置文件的用户签名

    通过命令的方式查看三个配置文件的用户签名。

    (1)语法

    • 执行git config命令,来查看各作用域配置文件中的配置信息。(这个命令在任何路径下都能执行)
    • 只执行git config命令,会显示git config命令所有的可用参数。
    • 执行git config --list命令,查看当前系统中Git的所有配置,三个配置文件所有的配置都显示出来。
    • 查看指定指定配置文件的内容:
      执行语句 说明
      $ git config --list --local 查看项目/仓库级别的配置文件信息
      $ git config --list --global 查看用户级别配置文件信息
      $ git config --list --system 查看系统级别配置文件信息

    (2)查看项目/仓库级别的配置文件信息(local)

    需要进入到一个仓库中,执行$ git config --list --local命令,才能显示该仓库的配置信息。否则会出现提示fatal: --local can only be used inside a git repository--local选项只能在Git仓库中使用。

    bash
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository
    $ git config --list --local
    fatal: --local can only be used inside a git repository
    
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository
    $ cd learngit/
    
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ git config --list --local
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    user.name=sha_hs
    user.email=sha_hs@126.com
    

    提示:

    执行$ git config --list --local命令时, Git会读取仓库中.git目录下的.git/config配置文件,该文件含有当前仓库的配置信息。

    bash
    # 查看.git目录
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ ll .git/
    total 7
    -rw-r--r-- 1 L 197121 176  4月  2 22:43 config
    -rw-r--r-- 1 L 197121  73  4月  2 22:41 description
    -rw-r--r-- 1 L 197121  23  4月  2 22:41 HEAD
    drwxr-xr-x 1 L 197121   0  4月  2 22:41 hooks/
    drwxr-xr-x 1 L 197121   0  4月  2 22:41 info/
    drwxr-xr-x 1 L 197121   0  4月  2 22:41 objects/
    drwxr-xr-x 1 L 197121   0  4月  2 22:41 refs/
    
    # 查看.git/config文件中的内容
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ cat .git/config
    [core]
         repositoryformatversion = 0
         filemode = false
         bare = false
         logallrefupdates = true
         symlinks = false
         ignorecase = true
    [user]
         name = sha_hs
         email = sha_hs@126.com
    

    (3)查看用户/全局级别的配置文件信息(global)

    在任何位置执行$ git config --list --global命令即可。

    bash
    # 任何目录下执行都可以
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ git config --list --global
    user.name=sun_wk
    user.email=sun_wk@126.com

    注意:

    如果我们是新安装的Git,还没有配置过global作用域内的配置信息,global级别的配置文件是没有的,只有我们配置一次global级别的配置信息,配置文件才会生成。

    image

    fatal: unable to read config file 'C:/Users/L/.gitconfig': No such file or directory:提示你无法读取配置文件'C:/Users/L/.gitconfig':没有此类文件或目录。

    提示:

    当我们配置过global作用域中的信息后,C:/Users/L/中的.gitconfig文件出现了。

    image

    执行git config --list --global命令后,查看的就是C:/Users/L/.gitconfig文件中的内容。

    (4)查看系统级别的配置文件信息(system)

    在任何位置执行$ git config --list --system命令即可。

    演示:

    bash
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository
    $ git config --list --system
    diff.astextplain.textconv=astextplain
    filter.lfs.clean=git-lfs clean -- %f
    filter.lfs.smudge=git-lfs smudge -- %f
    filter.lfs.process=git-lfs filter-process
    filter.lfs.required=true
    http.sslbackend=openssl
    http.sslcainfo=F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
    core.autocrlf=true
    core.fscache=true
    core.symlinks=false
    credential.helper=manager
    user.name=tang_s
    user.email=tang_s@126.com
    

    提示:

    该命令读取的配置文件所在的位置是,Git安装目录下的etc目录中的gitconfig文件。

    查看gitconfig文件中的内容,与上边显示的内容是对应的。

    (5)查看当前系统中Git的所有配置信息

    执行git config --list命令,查看当前系统中Git的所有配置,上面三个配置文件所有的配置都显示出来。

    示例:

    bash
    # 如果没有再本地仓库中执行该命令,只显示系统用户和全局用户配置文件中的信息
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository
    $ git config --list
    diff.astextplain.textconv=astextplain
    filter.lfs.clean=git-lfs clean -- %f
    filter.lfs.smudge=git-lfs smudge -- %f
    filter.lfs.process=git-lfs filter-process
    filter.lfs.required=true
    http.sslbackend=openssl
    http.sslcainfo=F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
    core.autocrlf=true
    core.fscache=true
    core.symlinks=false
    credential.helper=manager
    user.name=tang_s    # 系统用户签名
    user.email=tang_s@126.com
    user.name=sun_wk	# 全局用户签名
    user.email=sun_wk@126.com
    
    # 如果在本地仓库中执行该命令,三种用户配置文件的信息都会显示出来
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository
    $ cd learngit/
    
    L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
    $ git config --list
    diff.astextplain.textconv=astextplain
    filter.lfs.clean=git-lfs clean -- %f
    filter.lfs.smudge=git-lfs smudge -- %f
    filter.lfs.process=git-lfs filter-process
    filter.lfs.required=true
    http.sslbackend=openssl
    http.sslcainfo=F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
    core.autocrlf=true
    core.fscache=true
    core.symlinks=false
    credential.helper=manager
    user.name=tang_s    # 系统用户签名
    user.email=tang_s@126.com
    user.name=sun_wk  	# 全局用户签名
    user.email=sun_wk@126.com
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    user.name=sha_hs	# 本地用户签名
    user.email=sha_hs@126.com
    

    3、总结

    • 在本地Git的安装目录下,etc\gitconfig文件:是对登陆该操作系统的所有用户都普遍适用的配置。若使用git config命令时加上--system选项,读写的就是这个文件中的内容。
    • 当前操作系统用户目录下.gitconfig文件:该配置文件只适用于该用户,该用户可以配置Git用户签名等信息到这个配置文件中,是对这台计算机上所有的Git仓库适用。若使用git config命令时加上--global选项,读写的就是这个文件中的内容。
    • Git本地仓库中.git/config文件:当前项目的Git本地仓库中的配置文件,文件中的配置仅仅针对当前项目仓库有效。

    提示:每一个级别的配置都会覆盖上层的相同配置。(local覆盖global覆盖system


    __EOF__

  • 本文作者: 繁华似锦的博客
  • 本文链接: https://www.cnblogs.com/liuyuelinfighting/p/16164312.html
  • 关于博主: 评论和私信会在第一时间回复。或者直接私信我。
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
  • 声援博主: 如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。
  • 相关阅读:
    Tabby All configured authentication methods failed
    C# OpenVino Yolov8 Pose 姿态识别
    新手学习STM32还是ESP32
    算法复杂度介绍
    electron 基础
    第七章 总结及作业【编译原理】
    JavaScript正则表达式:正则表达式中的特殊字符
    华为云Stack首席架构师:打造“称手”的数字化工具,答好政企IT数字化转型这道必选题
    React之render
    【无标题】
  • 原文地址:https://www.cnblogs.com/liuyuelinfighting/p/16164312.html