Git使用手册

Git使用手册

anzai249 床主

简介

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.

基础用法

创建仓库

本地新建

1
git init

克隆仓库

1
git clone <repository>

工作区域

  • 工作区
    • .git所在目录
    • 用户实际操作的目录
  • 暂存区
    • .git/index
    • 存放add后没有commit的内容
  • 本地仓库
    • .git/objects
    • Git用于存储代码和版本信息的区域,commit后的内容存到此处

文件状态

flowchart LR
   A[未跟踪] --"git add"--> B[未修改] --修改文件--> C[已修改] --"git add"--> D[已暂存]
   A[未跟踪] --"git add"--> D[已暂存]
   D[已暂存] --"git commit"--> B[未修改] --"git rm"--> A[未跟踪]
   D[已暂存] --"git reset"--> C[已修改] --"git checkout"--> B[未修改]

提交

查看仓库状态

1
git status

添加到暂存区

1
git add

从暂存区中删除

1
git rm

提交

1
git commit

日志

1
2
3
4
# 显示hash id、提交者、时间和提交信息
git log
# 只显示hash和提交信息
git log --oneline

回退版本

1
2
3
4
5
6
# 不放弃工作区和暂存区的内容
git reset --soft
# 放弃工作区和暂存区的内容
git reset --hard
# 保留工作区,放弃暂存区的内容
git reset --mixed

对比文件

1
2
3
4
5
6
7
8
9
10
# 比较工作区和暂存区之间的差异
git diff
# 比较工作区和仓库之间的差异
git diff HEAD
# 比较暂存区和仓库之间的差异
git diff --cached

# 比较两个版本之间的差异
git diff <commit ID 1> <commit ID 2>

上一个版本用HEAD~HEAD^,上两个版本HEAD~2

此外本质零还可以比较分支之间的差异。

删除文件

1
2
3
4
# 从工作区和暂存区全部删除
git rm
# 只从暂存区中删除
git rm --cached

关联远程

1
2
3
4
# 查看对应远程仓库
git remote -v
# 关联
git remote add <shortname> <url>
1
2
3
4
5
6
# 获取远程仓库的修改,不合并到本地
git fetch
# 从远程拉取
git pull
# 推送到远程仓库
git push

分支

1
2
3
4
5
6
7
8
# 创建分支
git branch <branchname>
# 切换分支
git switch <branchname>
# 合并分支
git merge <branchname>
# 删除分支
git branch -d <branchname>

rebase

1
2
# 将当前分支的commit直接剪到目标分支上面
git rebase <branchname>

Gitflow模型

1
2
# 标记版本号
git tag

将仓库分为以下分支进行开发:

  • main
  • hotfix
  • release
  • develop
  • feature

下图是draw.io Apache License 2.0 )的一个模板,直接拿来用了,里面的Nightly分支是每晚自动构建。

Gitflow

  • 标题: Git使用手册
  • 作者: anzai249
  • 创建于 : 2023-07-20 10:29:52
  • 更新于 : 2023-12-16 22:02:41
  • 链接: https://anzai.sleepingbed.top/archives/posts/9f0af8b7.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论