The basic idea
We should learn git from the underlevel.How is the git designed?The basic datastructure of git: blob, tree, commit
type blob array<byte>
type tree map<string, tree | blob>
type commit struct {
parents: array<commit>
author: string
message: string
snapshot: tree
}
type object = blob | tree | commit
def store(object):
id = sha1(object)
objects[id] = object
def load(id):
return objects[id]
In fact, when you create a file, there is a blob object appear.the blob object has a hash code with it.You can get the specfic contents by visiting its object[hash_code].when you create a folder, then there is a tree object appear.As the same, there is a hash code with it.when you create a commit, as before, there is a hash code with it, and its contents contain all this trees and blobs.You should know each snapshot only corresponds one commit.just one by one.hash code is just like "指纹".
# a readable name for hashcode.while the references can change, the hash code is stable.
references = map<string, string>
def update_reference(name, id):
references[name] = id
def read_reference(name):
return references[name]
def load_reference(name_or_id):
if name_or_id in references:
return load(references[name_or_id])
else:
return load(name_or_id)
when you feel confuesd about the corresponding between git command and the datastructure you can use the git cat-file -p <hash_code> to watch the details.
The basic command(coming from the missing-semseter about git)
基础
-
git help <command>: 获取 git 命令的帮助信息 -
git init: 创建一个新的 git 仓库,其数据会存放在一个名为.git的目录下 -
git status: 显示当前的仓库状态 -
git add <filename>: 添加文件到暂存区 -
git commit: 创建一个新的提交 -
git log --all --graph --decorate: 可视化历史记录(有向无环图) -
git diff <filename>: 显示与暂存区文件的差异 -
git diff <revision> <filename>: 显示某个文件两个版本之间的差异 -
git checkout <revision>: 更新 HEAD 和目前的分支
分支和合并
-
git branch: 显示分支 -
git branch <name>: 创建分支 -
git checkout -b <name>: 创建分支并切换到该分支- 相当于
git branch <name>; git checkout <name>
- 相当于
-
git merge <revision>: 合并到当前分支 -
git mergetool: 使用工具来处理合并冲突 -
git rebase: 将一系列补丁变基(rebase)为新的基线
远端操作
git remote: 列出远端git remote add <name> <url>: 添加一个远端git push <remote> <local branch>:<remote branch>: 将对象传送至远端并更新远端引用git branch --set-upstream-to=<remote>/<remote branch>: 创建本地和远端分支的关联关系git fetch: 从远端获取对象/索引git pull: 相当于git fetch; git mergegit clone: 从远端下载仓库
撤销
git commit --amend: 编辑提交的内容或信息git reset HEAD <file>: 恢复暂存的文件git checkout -- <file>: 丢弃修改git restore: git2.32 版本后取代 git reset 进行许多撤销操作