Skip to main content

《Git 权威指南》第二篇 Git独奏 第4章 Git初始化

5 min read

[toc]

前言

本系列文章是针对计算机经典书籍《Git 权威指南》的精简版学习笔记,方便大家快速了解内容以及速记,欢迎订阅

4.1 创建版本库及第一次提交

  1. 首先查看自己的git版本
  • git --version
  • 比如我的是 git version 2.24.3 (Apple Git-128)
  1. 建立新工作目录
  • 如果git版本是1.6.5+
  • git init demo
  • 否则就一步一步来
  • mkdir demo
  • cd demo
  • git init
  1. 查看隐藏的git目录
  • ls -aF
  • 会显示 ./ ../ .git/
  • 其中 .git 就是Git版本库(又叫仓库,repository)
  • 版本库所在目录称为工作区,比如我的 ~/coding/git/demo
  1. 为工作区加内容
  • echo "Hello." > welcome.txt
  • 需要运行命令 git add welcome.txt将新建立的文件添加到版本库
  • 提交:git commit -m "initialized."
  • -m 为提交说明
  • [master (root-commit) 1a557fb] initialized. 从这可以看出此次提交在名为master分支上,且是该分支的第一个提交(root-commit),提交ID为1a557fb
  • 1 file changed, 1 insertion(+)可以看出此次提交修改了一个文件,包含一行的插入
  • create mode 100644 welcome.txt 可以看出此次提交创建了一个新文件

4.2 思考:为什么工作区根目录下有一个.git目录

  • Git及其他分布式版本控制系统(如Mercurial/Hg, Bazaar)的一个共同特点是版本库位于工作区的根目录下
  • 传统的集中式版本控制系统需要建立版本库和工作区的对应
  • Git的设计是的所有的版本控制操作都在本地即可完成
  • Git提供了一条命令用来搜索工作区的文件内容
  • git grep "Hell"
  • 结果是 welcome.txt:Hello.
  • 如果在工作区的子目录中执行Git命令
  • 会在工作区目录中依此向上递归查找git目录,找到的.git目录就是工作区对应的版本库,.git所在的目录就是工作区的根目录,文件.git/index记录了工作区文件的状态(实际上是暂存区的状态)
  • 在非工作区执行git命令时会因为找不到.git目录而报错
  • 如果想知道工作区的根目录的位置
  • 先创建多级目录并进入
  • mkdir -p a/b/c
  • cd a/b/c
  • 当前目录:~/coding/git/demo/a/b/c
  • 显示版本库.git目录所在的位置
  • git rev-parse --git-dir
  • /Users/xujie/coding/git/demo/.git
  • 显示工作区根目录
  • git rev-parse --show-toplevel
  • /Users/xujie/coding/git/demo
  • 显示工作区根目录的相对目录
  • git rev-parse --show-prefix
  • a/b/c/
  • 显示从当前目录后退到工作区的根的深度
  • git rev-parse --show-cdup
  • ../../../

4.3 git config命令的各参数有何区别

  • 使用不同参数的git config命令时机操作了不同的文件
  • git config -e
  • 实际上操作 ./demo/.git/config (版本库级别,优先级最高)
  • git config -e --global
  • 实际上操作: ~/.gitconfig (用户全局配置)
  • git config -e --system
  • 实际上操作 /etc/gitconfig (系统级别)
  • git配置文件采用的是INI 文件格式
  • cat .git/config

[core]

repositoryformatversion = 0

filemode = true

bare = false

logallrefupdates = true

ignorecase = true

precomposeunicode = true

  • 读取配置文件的属性
  • git config <section>.<key>
  • git config core.bare
  • false
  • 更改或者设置INI文件中某个属性的值
  • git config a.b something
  • git config x.y.z something

$ cat .git/config

[core]

repositoryformatversion = 0

filemode = false

bare = false

logallrefupdates = true

symlinks = false

ignorecase = true

[a]

b = something

[x "y"]

z = something

  • 操作任何其他的INI文件
  • GIT_CONFIG=test.ini git config a.b.c.d "hello, world"

$ cat test.ini

[a "b.c"]

d = hello, world

  • 从配置文件test.ini中读取配置
  • GIT_CONFIG=test.ini git config a.b.c.d
  • hello, world

4.4 思考:是谁完成的提交

  • 可以通过执行下面的命令删除user.name 和 user.email的配置
  • git config --unset --global user.name
  • git config --unset --global user.email
  • 然后提交
  • git commit --allow-empty -m "who does commit?"

[master df2b623] who does commit?

Committer: xujie <xujie@xujies-MacBook-Pro.local>

Your name and email address were configured automatically based

on your username and hostname. Please check that they are accurate.

You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"

git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

1 file changed, 2 insertions(+)

create mode 100644 test.ini

  • git log --pretty=fuller

commit df2b62367ca80056d248113aed8f3dd4d10dcdca (HEAD -> master)

Author: xujie <xujie@xujies-MacBook-Pro.local>

AuthorDate: Wed Sep 28 22:37:49 2022 +0800

Commit: xujie <xujie@xujies-MacBook-Pro.local>

CommitDate: Wed Sep 28 22:37:49 2022 +0800

who does commit?

commit 1a557fb84b2ed006bca7395729b1efb71baf96f1

Author: jiexu <admin@example.com>

AuthorDate: Wed Sep 28 21:39:06 2022 +0800

Commit: jiexu <admin@example.com>

CommitDate: Wed Sep 28 21:39:06 2022 +0800

initialized.

  • 重新配置提交者信息
  • git config --global user.name "jiexu"
  • git config --global user.email jiexu@example.com
  • 重新提交修改
  • git commit --amend --allow-empty --reset-author

4.5 思考:随意设置提交者姓名,是否不太安全

  • 这是分布式版本控制系统的特性使然,每个人都是自己的版本库的主人,很难也没有必要进行身份认证,所以无法使用经过认证的用户名作为提交的用户名

备份本章的工作

  • 备份内容
  • git clone demo demo-step-1
Loading Comments...