Git工作原理

1. Git操作名词介绍

  • 仓库(Repository):存储代码及其版本历史的容器,分为本地仓库和远程仓库。
  • 工作区(Working Directory):本地电脑上的代码目录,包含未提交的修改。
  • 暂存区(Staging Area):存储待提交的代码改动,也称为索引(Index)。
  • 提交(Commit):将暂存区改动保存到本地仓库,形成版本快照。
  • 合并(Merge):将分支代码合并到当前分支
  • 推送(Push):将本地仓库代码上传到远程仓库。
  • 拉取(Pull):从远程仓库获取最新代码并合并到本地仓库。
  • 分支(Branch):用于隔离不同开发任务,可独立维护代码版本。
  • HEAD:指向当前操作的分支
git操作说明

2. Git常见工作流程详解

git工作流程
git工作流程
查看账号信息
yuzhenteng@yuzhentengdeMac-mini ~ % git config user.name
lixuyan
yuzhenteng@yuzhentengdeMac-mini ~ % git config user.email
123456789@qq.com
git 初始化
yuzhenteng@yuzhentengdeMac-mini git_day1 % git init
Initialized empty Git repository in /Users/yuzhenteng/Desktop/demo/local_repo/git_day1/.git/
查看当前状态
yuzhenteng@yuzhentengdeMac-mini git_day1 % git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    file.txt

nothing added to commit but untracked files present (use "git add" to track)
将工作目录修改添加到暂存区
yuzhenteng@yuzhentengdeMac-mini git_day1 % git add .
将暂存区内容提交到本地仓库
yuzhenteng@yuzhentengdeMac-mini git_day1 % git commit -m"add file to local repo"
[main (root-commit) 429cfd4] add file to local repo
 1 file changed, 5 insertions(+)
 create mode 100644 file.txt
查看提交日志
yuzhenteng@yuzhentengdeMac-mini git_day1 % git log --oneline --decorate --graph --stat
* 429cfd4 (HEAD -> main) add file to local repo
   file.txt | 5 +++++
   1 file changed, 5 insertions(+)

3. git reset 与git checkout命令讲解

git reset

  • HEAD: 用来指向分支的最后一次提交对象。
  • git reset :用于回退版本,可以指定退回某一次提交的版本。
    --soft:回退到暂存区。
    --hard: 回退到工作目录。

我们在file.txt中添加-----6----- 并提交到本地仓库 这段操作我就不展示了。接下来我将这次提交回退到工作区

yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset --hard HEAD~1
HEAD is now at 429cfd4 add file to local repo
或
yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset --hard 429cfd4
HEAD is now at 429cfd4 add file to local repo
  • HEAD~1:指HEAD指向当前操作分支的最后一次提交对象的前一个提交对象。或者直接用前一个commitId代替。

下面我们用--soft将提交对象回退到暂存区。

yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset --soft HEAD~1

此时工作区内容没有发生变化。我想回退到工作区内容。接下来就用到了checkout。

checkout

  • git checkout :
    A.切换分支,例如:git checkout master
    B.重新存储工作区文件 git checkout -- .
yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset HEAD .
Unstaged changes after reset:
M   file.txt
yuzhenteng@yuzhentengdeMac-mini git_day1 % git checkout -- .
yuzhenteng@yuzhentengdeMac-mini git_day1 % git status
On branch main
nothing to commit, working tree clean
  • git reset HEAD . 将暂存区内容移除, .:点代表当前目录。

4. Git key-value文件系统

git key-value文件系统
  • git cat-file -p commit-hash 用于查看提交信息
  • Git对象存储
    Git将存储对象的40位HASH分为两部分:
    A. 头两位作文件夹
    B.后38位 为对象文件名 git/objects/hash[:2]/hash[2:40]
  • 为什么要这么设计目录结构,而不直接使用49位hash作文件名?
    1.部分文件系统对目录下的文件数量有限制。例如,FAT32限制单目录下的最大文件数量是65535个
    2.部分文件系统查找文件属于线性查找,目录下的文件越多,访问越慢。
  • Git index文件
    Git在•git 文件夹下面存放了 index 文件,该文件表示Git stage 的内容。该文件是二进制文件,保存了被stage的文件的所有信息,像inode信息、hash值等等
  • git ls-files -s 查看暂存区文件
  • 为文件生成hash值且保存文件 git hash-object -w 文件路径
yuzhenteng@yuzhentengdeMac-mini git_day1 % git hash-object -w file.txt 
6fc3c2d99d427db79cd35a409043889f4dcaff5c
  • 保存到索引/暂存区 git update-index --add --cacheinfo 100644 hash 文件路径
yuzhenteng@yuzhentengdeMac-mini git_day1 % git update-index --add --cacheinfo 100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c file.txt
yuzhenteng@yuzhentengdeMac-mini git_day1 % git ls-files -s
100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c 0   file.txt

5. 手动构建git commit tree

  • 写树 git write-tree
yuzhenteng@yuzhentengdeMac-mini git_day1 % git write-tree
639282694c1a66e7936f273449bcfa25ff2ffc29
yuzhenteng@yuzhentengdeMac-mini git_day1 % git cat-file -p 639282694c1a66e7936f273449bcfa25ff2ffc29
100644 blob 6fc3c2d99d427db79cd35a409043889f4dcaff5c    file.txt
  • 在暂存区创建新树 git read-tree --prefix=文件夹/ hash
yuzhenteng@yuzhentengdeMac-mini git_day1 % git read-tree --prefix=hank/ 639282694c1a66e7936f273449bcfa25ff2ffc29
yuzhenteng@yuzhentengdeMac-mini git_day1 % git write-tree
0191adbc95c577c0b77c31bfc47e714306b60645
yuzhenteng@yuzhentengdeMac-mini git_day1 % git ls-files -s
100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c 0   file.txt
100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c 0   hank/file.txt
yuzhenteng@yuzhentengdeMac-mini git_day1 % git cat-file -p 0191adbc95c577c0b77c31bfc47e714306b60645
100644 blob 6fc3c2d99d427db79cd35a409043889f4dcaff5c    file.txt
040000 tree 639282694c1a66e7936f273449bcfa25ff2ffc29    hank
yuzhenteng@yuzhentengdeMac-mini git_day1 % echo "add 10" | git commit-tree bdd8135704656336dacde01b5bcdba81fedc3752
5200a9049b527ee704b99ee7030ac12203ae3d63
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容