「Git」- 分布式版本控制系统 | 学习笔记

认识

官网:https://git-scm.com/
文档:https://git-scm.com/doc/
仓库:https://github.com/git/git/

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. 简而言之,Git 是个版本管理工具。

组成

对象库:原始文件,日志消息,作者信息,日期,以及其他的版本或者分支的信息。

索引

对象类型

# 块:BLOB
文件的每一个版本表示为一个「块」。二进制大对象,可以包含任意数据的变量或者文件。

# 目录树:TREE
一个「目录树」对象代表一层目录信息。它记录 BLOB 标识符,路径名,和在一个目录里所有文件的一些元数据。它可以递归引用其他「目录树」,从而建立一个包含目录和子目录的完整的目录层次结构。

# 提交:COMMIT
一个「提交」对象保存版本库中每次变化的元数据,包括作者、提交者、提交日期、日志消息。每个提交都指向一个「目录树」对象。这个目录树对象在一张完整的快照中捕获提交时版本库的状态。

# 标签:TAG
为特定对象分配可读的名称。通常是一个「提交」对象。

索引

索引是一个临时的、动态的二进制文件,它描述整个版本库的目录结构。索引捕获项目的某以时刻的整体结构的一个版本。

性质

safe.directory git config –global –add safe.directory

DeepSeek / 如何禁用该特性
DeepSeek / 解释 git config –global –add safe.directory

该命令用于向 Git 的全局配置中添加一个被标记为”安全”的目录。这是 Git 2.35.1 及更高版本中引入的安全特性。

当 Git 检测到仓库目录被其他用户拥有时(例如用 sudo 克隆了仓库),它会拒绝工作以防止潜在的恶意脚本执行。使用此命令可以将特定目录标记为安全,绕过这个安全检查。

Git 2.35.1 引入的 safe.directory 安全检查可以通过以下几种方式禁用:

  • 标记所有目录为安全(不推荐):git config –global –add safe.directory ‘*’
  • 完全禁用该特性(需要 Git 2.36+):git config –global safe.directory none
  • 通过环境变量临时禁用:export GIT_TEST_ASSUME_DIFFERENT_OWNER=1

要查看当前的安全目录设置:git config –global –get-all safe.directory —— 如果返回空,则表示没有目录被特别标记为安全,Git 会执行默认的所有权检查。

删除所有安全目录配置:git config –global –unset-all safe.directory

在命令提示符号中,显示当前 GIT 分支

Display git branch in bash prompt

# ----------------------------------------------------------------------------- # 方法一
# 该命令存在于 /usr/lib/git-core/git-sh-prompt 中,

export PS1="\\w\$(__git_ps1 '(%s)') \$ "

# ----------------------------------------------------------------------------- # 方法二

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\n$ "

# 注意事项:
#   1. 不管使用那种方法都要注意“\$”的存在,这用于防止命令被提前执行。否则,当切换到其他目
#      时,分支名不会发生改变;

构造

容器镜像

[I] SRC: docker.io/alpine/git:v2.49.1
[I] DST: ccr.ccs.tencentyun.com/d3rm-3rd/docker.io_alpine_git:v2.49.1

on Ubuntu 18.04.5 LTS

How to Install the Latest Git Version on Ubuntu
Git/Download for Linux and Unix

# 仓库版本的安装
apt-get install git

# 最新版本的安装,for Ubuntu
add-apt-repository ppa:git-core/ppa
apt update
apt install git

on CentOS 7.x(安装新版本)

IUS/Setup
Update Git to the latest (help)
How to install latest version of git on CentOS 7.x/6.x

# 安装新版本 Git
yum install -y \
    https://repo.ius.io/ius-release-el7.rpm \
    https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum makecache
yum install git2u-all.noarch # 全家桶(11/20/2019 git version 2.16.5)

# 安装更新的版本
# yum install http://opensource.wandisco.com/centos/6/git/x86_64/wandisco-git-release-6-1.noarch.rpm
# yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-1.noarch.rpm
yum install -y http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
yum makecache
yum install -y git #(11/20/2019 git version 2.22.0)

on Windows

Git – Downloading Package/Download for Windows

安装下载即可

通过源码安装

针对 Git 的编译安装,建议参考 BLFS 文档:

应用

权限管理

Gitosis | 权限管理工具,通过一个特殊的仓库(gitosis-admin.git)对 Git 权限进行管理;
Git on the Server – Gitosis
gitosis 使用笔记

管理多个仓库

如何同时管理本地的多个 git 代码仓库,例如,同时创建分支、同时同送远程、批量修改后提交所有仓库、……

通过 Shell Python 脚本

通过 Git 子模块 (Submodules):git submodule foreach ‘git checkout -b feature/new-feature’

通过专门的多仓库管理工具

通过 IDE 插件:许多现代 IDE 和编辑器(例如 VS Code、IntelliJ IDEA、……)都有插件可以同时管理多个仓库。

撤销修改

DeepSeek / git 代码回滚到之前的提交

git reset –soft <commit-hash> # 将 HEAD 指针移动到指定提交;保留所有更改在工作目录中;不会修改暂存区或工作目录;

git reset –mixed <commit-hash> # 将 HEAD 指针移动到指定提交;更改保留在工作目录但未暂存;重置暂存区,但不影响工作目录;

git reset –hard <commit-hash> # 将 HEAD 指针移动到指定提交;所有更改都将丢失;

git revert <commit-hash> #

保留空目录 Keep a empty folder

How can I add an empty directory to a Git repository? – Stack Overflow

在我们项目中,存在数据目录,在程序运行之前,这些目录为空目录。当提交到 Git 仓库时,这些空目录会被忽略。但是,我们希望保存这些空目录,以体现项目目录结构。该笔记将记录:在 git 中,如何保留空目录。

或者,在空目录中,创建 .gitignore 文件,并填入如下内容:

# Ignore everything in this directory
*
# Except this file
!.gitignore

或者,在空目录中,创建 .placeholder 文件,在根目录的 .gitignore 填入如下内容:

# Ignore everything in this directory
data/*
# Except this file
!.placeholder

# 如果是多级的子目录,使用
**/data/*

参考

Home Page:https://git-scm.com/
Installion: https://git-scm.com/download/linux
Souece Code: https://github.com/git/git

  • 在 Man 手册页面中,使用子命令的形式查找。比如 man git-add,用于查看 git add 命令的手册;
  • 执行命令 git help <cmd> 用于查看帮助手册,比如 git help add 用于查看 git add 命令的手册;
  • 当然,常用的做法还是通过搜索引擎,按需查询;
  • 《Git 版本控制管理(第 2 版)》| 《Pro Git》| ……