【Git 学习笔记】第二章 Git 的配置(上)

说明
本篇根据原书第二章前半部分的学习笔记整理,涉及 2.1 ~ 2.4 小节内容。具体实操代码库,需使用 GitHub 线上版本。

第二章 配置 Git

相关主题:

  • 配置目标
  • 检索已有配置
  • 配置模板
  • .git 目录模板
  • 几个配置示例
  • 设置 Git 别名
  • refspec 举例

2.1 配置目标

Git 配置分三个层次:

配置层次 说明 配置文件路径
SYSTEM 系统级配置 Linux: /etc/gitconfig
Windows: C:\Git\etc\gitconfig
GLOBAL 用户级配置 ~/.gitconfig
LOCAL 仓库级配置 .git/config

练习:

# Sync repo status
$ git clone https://git.eclipse.org/r/jgit/jgit demo
$ cd demo
# List the system config
$ git config --list --system
# List the global (user) config
$ git config --list --global
# List the repo config
$ git config --list --local

# Query single global key
$ git config --global user.email
# Set a key locally
$ git config --local user.email john@example.com
# Set default git editor
$ git config --global core.editor vim

2.2 检索已有配置

# View all the effective configurations for the current Git repository
$ git config --list
# Show single configuration item
$ git config user.name
# Set a new username
$ git config user.name "John Doe"
# Set your own configuration
$ git config my.own.config "Whatever I need"
$ git config my.his.config "Whatever He need"
$ git config my.own.config.item item
# Check the config file
$ cat .git/config
# ... sth irrelevant
[my "own"]
        config = Whatever I need
[my "his"]
        config = Whatever He need
[my "own.config"]
        item = item
# Show the value assigned
$ git config my.own.config
Whatever I need
$ git config my.own.config.item
item

2.3 配置模板

模板通常在用户级配置(GLOBAL),一般不在仓库级(LOCAL)配置。

# Demonstrated in Windows
> git clone https://github.com/PacktPublishing/Git-Version-Control-Cookbook-Second-Edition.git demo
> git demo
# Create template file, save as ~/gitcommitmsg.txt
> notepad
# Demo content:
###########################################################
Short description of commit 

Longer explanation of the motivation for the change 

Fixes-Bug: Enter bug-id or delete line 
Implements-Requirement: Enter requirement-id or delete line 
###########################################################

# Config commit template
> git config --global commit.template ~/gitcommitmsg.txt
# Create a new commit
> echo "test new content" >> abc.txt
> git status -s
?? abc.txt
> git add abc.txt; git status
A  abc.txt
> git commit
# This would display a Sublime Text 3 interface:

图2-1

# Edit the template content, save and exit sublime text
> git commit
[master a5c36d0] Test git template file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 abc.txt
> git log -1
commit a5c36d0bb6bcd5e3599de500e7628c296554aa93 (HEAD -> master)
Author: SafeWinter <zandong_19@aliyun.com>
Date:   Sat Nov 27 23:30:16 2021 +0800

    Test git template file

    Added a new file called abc.txt to test the functionality of git template

    Fixes-Bug: demo-bug-001
    Implements-Requirement: (skip)

相关注意事项

  1. 配置 Sublime Text 3 为默认编辑器时,需加 -w 参数,表示 wait,即等待提交内容保存完毕后才可退出编辑,否则会默认提交失败。相关的 Git 配置如下(Windows 环境下,路径分隔符 只能是 \\):
# List git default editor (Sublime Text 3)
$ git config --system core.editor
'C:\\ProgramFiles\\SublimeText3\\subl.exe' -w
# Config Sublime Text 3 as default git editor
$ git config --system core.editor "'C:\\ProgramFiles\\SublimeText3\\subl.exe' -w"
  1. Window 下的 Git 设置不可直接用于 WSL2,因为绝对路径在 Linux 环境下不存在 C:\,而是挂载在 /mnt/ 路径下。

  2. 虽然对 WSL2 可重新配置默认编辑器:

    $ git config --system core.editor
    '/mnt/c/ProgramFiles/SublimeText3/subl.exe' -w
    

    当在 WSL2 但运行 git commit 时,Sublime Text 3 会默认保存到 Win10 环境下的 .git 文件夹内,临时文件名为: .git/COMMIT_EDITMSG;然而此时使用的路径还是 WSL2 的 /mnt/c/...,因此首次默认保存提交注释会出错,需要手动指定到当前 .git 文件夹内,再次覆盖保存。

  3. 由于 LinuxWindows 的根路径不同,要尽量避免同时在 WindowsLinux 环境内提交新版本,否则将报错:

    # Under WSL2 environment
    $ git commit
    hint: Waiting for your editor to close the file... 'C:\\ProgramFiles\\SublimeText3\\subl.exe' -w: 1: C:\\ProgramFiles\\SublimeText3\\subl.exe: not found
    error: There was a problem with the editor ''C:\\ProgramFiles\\SublimeText3\\subl.exe' -w'.
    Please supply the message using either -m or -F option.
    
  4. 模板注释文件也可以是仓库级别的,只要将文件配置在 --local 级别即可,以满足更多样化的模板设置。

2.4 .git 目录模板

除了全局配置,有时进行版本控制时还需要触发一些预处理脚本的执行(这些 script 脚本即 Git hooks、Git 钩子),或者是模板化地剔除一些文件等。此时可以对 git init 命令设置统一的预处理操作。为配置项 init.templatedir 指定一个存放脚本的文件夹路径即可。

该文件夹路径的配置方式:

  1. 作为 git clonegit init 命令的参数项运行;
  2. 作为 $GIT_TEMPLATE_DIR 环境变量运行;
  3. 作为 Git 的配置项运行(配置 init.templatedir 实现,默认路径为 /usr/share/git-core/templates )。

模板化操作的工作原理

将指定模板文件夹内的文件复制到 .git (即 $GIT_DIR )文件夹。

本节演示两个模板操作:

  1. git hook 示例
  2. 以模板的方式设置忽略清单,忽略所有 *.txt 文件加入版本控制

示例1:Git 钩子

# Make customed template directory
$ mkdir ~/.git_template
$ mkdir ~/.git_template/{hooks,info}
# Copy git default hook files
$ cd ~/.git_template/hooks
$ cp /usr/share/git-core/templates/hooks/* .
# Rename commit-msg.sample file
$ cp commit-msg.sample commit-msg
# Edit with vim in commit-msg:

图2-2

# Make the hook executable
$ chmod +x ~/.git_template/hooks/commit-msg
# Config git template directory
$ git config --global init.templatedir ~/.git_template
# Testing and validating
$ cd ~ && git init template-example && cd template-example
$ echo "something to commit" > somefile
$ git add somefile
$ git commit -m "Committed something"
$ git log -1

图2-3

可以看到,提交的内容后面有一空行,以及一行由 git hook 中的 commit-msg 指定的文本。


示例2:剔除 *.txt 文件

本例将设置忽略所有 *.txt 文件,相当于将 *.txt 加入 .gitignore 文件。

# Make a new exclude file into template folder
$ echo "*.txt" > ~/.git_template/info/exclude
# testing
$ cd ~ && git init template-example2 && cd template-example2
$ echo "this is the readme file" > README.txt
$ git status

图2-4

小结

  1. 每当进行 initclone 操作,创建文件夹时 git 会自动将模板文件夹内的文件复制到新版本库 .git 中。此外,模板路径还可以在 命令行环境变量 中进行指定;

  2. 示例中使用了 --global,意味着对该用户的所有相关操作生效。但也有不足:对配置生效前同样需要应用相同模板规则的旧仓库,只能手动再执行一遍 git init

  3. 示例仅对 Linux 系统生效,Windows 系统的默认模板路径在 {GIT_INSTALL_DIR}\mingw64\share\git-core\templates,且 \nWindows 系统不能输出一个回车,可以追加一个空串:($1 指代的是版本提交时的注释内容)

    #!/bin/sh
    MSG_FILE="$1"
    echo "" >> $MSG_FILE
    echo "Hi from template commit-msg hook on Win10" >> $MSG_FILE
    

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 18:02:07       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 18:02:07       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 18:02:07       58 阅读
  4. Python语言-面向对象

    2024-07-09 18:02:07       69 阅读

热门阅读

  1. 部署LVS-DR群集

    2024-07-09 18:02:07       26 阅读
  2. Vue项目使用mockjs模拟后端接口

    2024-07-09 18:02:07       23 阅读
  3. python的魔法方法

    2024-07-09 18:02:07       30 阅读
  4. virtualbox和docker的区别和优缺点以及如何选择

    2024-07-09 18:02:07       37 阅读
  5. 56、Flink DataStream 的管理执行配置详解

    2024-07-09 18:02:07       20 阅读
  6. 守望数据边界:sklearn中的离群点检测技术

    2024-07-09 18:02:07       33 阅读
  7. 57、Flink 的项目配置概述

    2024-07-09 18:02:07       30 阅读
  8. git杂记

    git杂记

    2024-07-09 18:02:07      27 阅读
  9. [Python学习篇] Python面向对象——继承

    2024-07-09 18:02:07       29 阅读