Skip to content
← 返回Git列表
Git任务与配置

Git 提交、历史与配置 · 导出代码与打开协作界面至检查对象、引用与底层提交结构 ​

返回分篇目录

导出代码与打开协作界面 ​

archive 导出代码,archive-file 保留扩展用法;浏览器入口、图形界面及批量仓库工具单独查阅。

git archive · 从代码树创建文件归档 ​

更多信息:https://git-scm.com/docs/git-archive。

  • 从当前 HEAD 内容创建 .tar 归档并输出到标准输出:
bash
git archive {{[-v|--verbose]}} HEAD
  • 使用 Zip 格式并显示详细进度:
bash
git archive {{[-v|--verbose]}} --format zip HEAD
  • 将 Zip 归档输出到指定文件:
bash
git archive {{[-v|--verbose]}} {{[-o|--output]}} {{路径/到/文件.zip}} HEAD
  • 从指定分支的最新提交内容创建 .tar 归档:
bash
git archive {{[-o|--output]}} {{路径/到/文件.tar}} {{指定分支}}
  • 使用特定目录的内容创建归档:
bash
git archive {{[-o|--output]}} {{路径/到/文件.tar}} HEAD:{{路径/到/目录}}
  • 为归档中的每个文件添加前缀路径:
bash
git archive {{[-o|--output]}} {{路径/到/文件.tar}} --prefix {{路径/到/前缀路径}}/ HEAD

git archive-file · 将当前 Git 分支的所有文件导出为 Zip 压缩包 ​

属于 git-extras的一部分。 更多信息:https://github.com/tj/git-extras/blob/main/Commands.md#git-archive-file。

  • 将当前检出的提交打包成 Zip 压缩包:
bash
git archive-file

git browse · 在默认浏览器中查看上游代码库 ​

属于 git-extras 工具集的一部分。 更多信息:https://github.com/tj/git-extras/blob/main/Commands.md#git-browse。

  • 在默认浏览器中打开第一个上游代码库:
bash
git browse
  • 在默认浏览器中打开指定的上游代码库:
bash
git browse {{上游名称}}

git browse-ci · 在默认浏览器中打开当前 git 代码库的 CI 网站 ​

属于 git-extras 工具集的一部分。 更多信息:https://github.com/tj/git-extras/blob/main/Commands.md#git-browse-ci。

  • 在默认浏览器中打开当前代码库的 CI 配置页面(基于上游网站):
bash
git browse-ci
  • 为指定远程仓库打开当前代码库的 CI 配置页面:
bash
git browse-ci {{远程名称}}

git brv · 按最后提交日期排序显示分支列表 ​

属于 git-extras的一部分。 更多信息:https://github.com/tj/git-extras/blob/main/Commands.md#git-brv。

  • 列出所有分支,显示日期、最新提交哈希和提交信息:
bash
git brv

git cola · 功能强大且界面直观的 Git 图形化界面工具 ​

更多信息:https://git-cola.readthedocs.io/en/latest/git-cola.html。

  • 启动图形界面:
bash
git cola
  • 以修订模式启动图形界面:
bash
git cola --amend
  • 提示选择 Git 仓库(默认为当前目录):
bash
git cola --prompt
  • 打开指定路径的 Git 仓库:
bash
git cola {{[-r|--repo]}} {{路径/到/git仓库}}
  • 在状态组件中应用路径过滤器:
bash
git cola {{[-s|--status-filter]}} {{过滤器}}

git bulk · 批量操作多个 Git 仓库 ​

属于 git-extras的一部分。 更多信息:https://github.com/tj/git-extras/blob/main/Commands.md#git-bulk。

  • 将当前目录注册为工作区:
bash
git bulk --addcurrent {{工作区名称}}
  • 注册一个工作区用于批量操作:
bash
git bulk --addworkspace {{工作区名称}} {{到仓库的绝对路径}}
  • 在指定目录克隆仓库并注册为工作区:
bash
git bulk --addworkspace {{工作区名称}} {{仓库父目录的绝对路径}} --from {{远程仓库地址}}
  • 从换行分隔的远程仓库列表克隆并注册为工作区:
bash
git bulk --addworkspace {{工作区名称}} {{/路径/到/根/目录}} --from {{/路径/到/文件}}
  • 列出所有已注册的工作区:
bash
git bulk --listall
  • 在当前工作区的所有仓库上执行 Git 命令:
bash
git bulk {{命令}} {{命令参数}}
  • 删除指定工作区:
bash
git bulk --removeworkspace {{工作区名称}}
  • 删除所有工作区:
bash
git bulk --purge

git blame-someone-else · 将你的糟糕代码归咎于其他人 ​

更多信息:https://github.com/jayphelps/git-blame-someone-else。

  • 更改提交的提交者和作者:
bash
git blame-someone-else "{{作者 <someone@example.com>}}" {{提交}}

检查对象、引用与底层提交结构 ​

日常提交之外的对象检查、引用验证、属性忽略规则和底层提交命令集中在此。

git check-attr · 检查每个路径的文件属性状态(未指定/已设置/未设置) ​

更多信息:https://git-scm.com/docs/git-check-attr。

  • 检查文件的所有属性状态:
bash
git check-attr {{[-a|--all]}} {{路径/到/文件}}
  • 查看文件的某个特定属性值:
bash
git check-attr {{属性}} {{路径/到/文件}}
  • 查看指定文件的所有属性值:
bash
git check-attr {{[-a|--all]}} {{路径/到/文件1 路径/到/文件2 ...}}
  • 在一或多个文件上查看特定的属性值:
bash
git check-attr {{属性}} {{路径/到/文件1 路径/到/文件2 ...}}

git check-ignore · 分析和调试 Git 忽略规则(.gitignore 文件) ​

更多信息:https://git-scm.com/docs/git-check-ignore。

  • 检查文件或目录是否被忽略:
bash
git check-ignore {{路径/到/文件或目录}}
  • 检查多个文件或目录是否被忽略:
bash
git check-ignore {{路径/到/文件或目录1 路径/到/文件或目录2 ...}}
  • 从标准输入读取路径列表(每行一个路径):
bash
git < {{路径/到/文件列表}} check-ignore --stdin
  • 不检查索引(用于调试为何某些路径被跟踪而非忽略):
bash
git check-ignore --no-index {{路径/到/文件或目录1 路径/到/文件或目录2 ...}}
  • 显示每个路径匹配的具体忽略规则:
bash
git check-ignore {{[-v|--verbose]}} {{路径/到/文件或目录1 路径/到/文件或目录2 ...}}

git check-ref-format · 检查引用名称格式是否合法,不合法时返回非零状态码 ​

更多信息:https://git-scm.com/docs/git-check-ref-format。

  • 检查指定引用名称的格式是否合法:
bash
git check-ref-format {{refs/head/refname}}
  • 打印最近检出的分支名称:
bash
git check-ref-format --branch @{-1}
  • 规范化引用名称格式:
bash
git check-ref-format --normalize {{refs/head/refname}}

git cat-file · 查看 Git 仓库对象的内容或类型和大小信息 ​

更多信息:https://git-scm.com/docs/git-cat-file。

  • 获取 HEAD 提交的字节大小:
bash
git cat-file -s HEAD
  • 获取指定 Git 对象的类型(blob、tree、commit、tag):
bash
git cat-file -t {{8c442dc3}}
  • 根据对象类型打印指定 Git 对象的内容:
bash
git cat-file -p {{HEAD~2}}

git count-objects · 统计未打包对象数量及其磁盘占用情况 ​

更多信息:https://git-scm.com/docs/git-count-objects。

  • 统计未打包对象的数量和磁盘占用:
bash
git count-objects
  • 以易读单位显示未打包对象的数量和磁盘占用(如KB/MB):
bash
git count-objects {{[-H|--human-readable]}}
  • 显示未打包对象、包文件及打包对象等统计:
bash
git count-objects {{[-v|--verbose]}}
  • 以易读格式显示详细统计信息:
bash
git count-objects {{[-H|--human-readable]}} {{[-v|--verbose]}}

git commit-graph · 生成和验证 Git 提交图文件 ​

更多信息:https://git-scm.com/docs/git-commit-graph。

  • 为仓库本地 .git 目录中的打包提交生成提交图文件:
bash
git commit-graph write
  • 生成包含所有可达提交的提交图文件:
bash
git show-ref {{[-s|--hash]}} | git commit-graph write --stdin-commits
  • 生成包含当前提交图文件中所有提交以及从 HEAD 可达提交的提交图文件(追加模式):
bash
git rev-parse {{HEAD}} | git commit-graph write --stdin-commits --append

git commit-tree · 用于创建提交对象的底层工具 ​

另请参阅:git commit。 更多信息:https://git-scm.com/docs/git-commit-tree。

  • 创建带有指定提交信息的提交对象:
bash
git commit-tree {{树对象哈希}} -m "{{提交信息}}"
  • 从文件读取提交信息创建提交对象(使用 - 表示从标准输入读取):
bash
git commit-tree {{树对象哈希}} -F {{路径/到/文件}}
  • 创建 GPG 签名过的提交对象:
bash
git commit-tree {{树对象哈希}} -m "{{提交信息}}" {{[-S|--gpg-sign]}}
  • 创建指定父提交的提交对象:
bash
git commit-tree {{树对象哈希}} -m "{{提交信息}}" -p {{父提交哈希}}

git column · 以列格式显示数据 ​

更多信息:https://git-scm.com/docs/git-column。

  • 将标准输入内容格式化为多列显示:
bash
ls | git column --mode={{column}}
  • 以最大宽度 100 的列格式显示标准输入内容:
bash
ls | git column --mode=column --width={{100}}
  • 以最大边距 30 的列格式显示标准输入内容:
bash
ls | git column --mode=column --padding={{30}}

示例整理自 tldr-pages,作者为 tldr-pages team and contributors,遵循 CC BY 4.0。

继续阅读