这是我参与「第五届青训营 」伴学笔记创作活动的第 8 天
一、Git的基本命令
- 配置
- git config
- 用户名配置
- git config --global user.name "你的名字"
- git config --global user.email 你的邮箱(此处不需要双引号)
- Instead of配置
- git config --global url.git@github.com:.insteadOf github.com/
- Git命令别名配置
- git config --global alias.cin "commit --amend --no-edit"
- HTTP Remote
- 免密配置
- URL: github.com/git/git.git
- 内存:git config --global credential.helper 'cache --timeout==3600'
- 硬盘:git config --global credential.helper "store --file /path/to/credential-file"
- 不指定目录的情况默认是 ~/.git-credentials
- 将密钥信息存在指定文件中, 具体格式:{user}:${password}@github.com
- 免密配置
- 用户名配置
- git remote
- 查看Remote
- git remote -v
- 添加Remote
- git remote add origin ssh git@github.com:git/git.git
- git remote add origin http github.com/git/git.git
- SSH Remote
- 免密配置
- URL: git@github.com:git/git.git
- SSH可以通过公私钥的机制,将生成公钥存放在服务端,从而实现免密访问。目前的Key的类型四种,分别是dsa、rsa、ecdsa、ed25519。默认使用的是rsa,由于一些安全问题,现在已经不推荐使用dsa和rsa了,优先推荐使用ed25519。
- 免密配置
- 查看Remote
- git config
- 提交代码
- git commit
- git add
- 远端同步
- 拉取代码
- clone
- pull
- fetch
- 推送代码
- push
- 拉取代码
二、Git目录介绍
- 项目初始化: mkdir study → cd study → git init
- 其他参数
- initial-branch 初始化的分支
- bare 创建一个裸仓库(纯Git目录,没有工作目录)
- template 可以通过模板来创建预先构建好的自定义git目录
三、Objects
commit/tree/blob在git里面都统一称为Object, 除此之外还有个tag的object
- Blob: 存储文件的内容
- Tree: 存储文件的目录信息
- Commit: 存储提交信息,一个Commit可以对应唯一版本的代码
把这三个信息串联在一起:
- 通过Commit寻找到Tree信息,每个Commit都会存储对应的Tree ID
- 通过Tree存储的信息,获取到对应的目录树信息。
- 从tree中获得blob的ID,通过Blob ID获取对应的文件内容。