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

Git 提交、历史与配置 · 初始化仓库与查阅命令帮助至配置凭据和缓存方式 ​

返回分篇目录

Git 提交、历史与配置 ​

初始化仓库与查阅命令帮助 ​

先初始化仓库并设置配置,再查帮助或扩展工具;git annex 与 git bug 是扩展工具,各自示例保留。

git · 分布式版本控制系统 ​

类似如 commit、add、branch、checkout、push 等子命令都有自己的使用文档。 更多信息:https://git-scm.com/docs/git。

  • 执行 git 的子命令:
bash
git {{子命令}}
  • 在自定义的 git 仓库根路径下执行子命令:
bash
git -C {{仓库地址}} {{子命令}}
  • 在给定参数条件下,执行 git 的子命令:
bash
git -c '{{配置项}}={{值}}' {{子命令}}
  • 显示帮助文档:
bash
git --help
  • 显示 git 子命令的详细帮助文档(如 clone, add, push, log 等子命令):
bash
git help {{子命令}}
  • 检查 git 的版本号:
bash
git --version

git help · 显示 Git 的帮助信息 ​

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

  • 显示指定 Git 子命令的帮助信息:
bash
git help {{subcommand}}
  • 在网络浏览器中显示指定 Git 子命令的帮助信息:
bash
git help {{[-w|--web]}} {{subcommand}}
  • 列出所有可用的 Git 子命令:
bash
git help {{[-a|--all]}}
  • 列出可用的指南:
bash
git help {{[-g|--guides]}}
  • 列出所有配置变量的名称:
bash
git help {{[-c|--config]}}

git init · 初始化新的本地 Git 仓库 ​

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

  • 初始化新的本地仓库:
bash
git init
  • 初始化仓库,并指定初始分支的名字(通常默认为 master 或 main):
bash
git init {{[-b|--initial-branch]}} {{分支名}}
  • 初始化仓库,使用 SHA256 算法生成对象哈希(需 Git 2.29+ 版本支持):
bash
git init --object-format sha256
  • 创建裸仓库,适合用作 SSH 远程仓库:
bash
git init --bare

git config · 管理 Git 仓库的自定义设置项 ​

这些设置可以分为局部设置(只对当前仓库生效)和全局设置(对当前用户生效)。 更多信息:https://git-scm.com/docs/git-config。

  • 列出局部设置项(存储在当前仓库的 .git/config):
bash
git config {{[-l|--list]}} --local
  • 列出全局配置项(存储在 ~/.gitconfig):
bash
git config {{[-l|--list]}} --global
  • 列出系统配置项(存储在 /etc/gitconfig),并且展示文件的位置:
bash
git config {{[-l|--list]}} --system --show-origin
  • 获取某个配置项的值:
bash
git config alias.unstage
  • 设置某个全局配置项:
bash
git config --global alias.unstage "reset HEAD --"
  • 将某个全局配置项恢复为默认值:
bash
git config --global --unset alias.unstage
  • 使用默认编辑器修改本地设置:
bash
git config {{[-e|--edit]}}
  • 使用默认编辑器修改全局设置:
bash
git config --global {{[-e|--edit]}}

git alias · 为 Git 命令创建快捷方式 ​

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

  • 列出所有别名:
bash
git alias
  • 为命令创建一个别名:
bash
git alias "{{别名}}" "{{命令}}"
  • 搜索一个现有的别名:
bash
git alias ^{{别名}}

git annex · 用 Git 管理文件,但不将其内容提交到版本库中 ​

当文件被附属时,其内容会被移至键值存储中,并创建一个指向该内容的符号链接。 更多信息:https://git-annex.branchable.com/git-annex/。

  • 初始化一个带有 Git annex 的仓库:
bash
git annex init
  • 添加文件:
bash
git annex add {{路径/到/文件或目录}}
  • 显示文件或目录的当前状态:
bash
git annex status {{路径/到/文件或目录}}
  • 同步本地仓库与远程仓库:
bash
git annex sync {{远程仓库名}}
  • 获取文件或目录(从键值存储中恢复内容):
bash
git annex get {{路径/到/文件或目录}}
  • 显示帮助:
bash
git annex help

git bug · 一个使用 Git 内部存储的分布式问题追踪器,不会在项目中添加额外文件 ​

您可以像提交和分支一样,将问题提交到与他人交互的同一个 Git 远程仓库。 更多信息:https://github.com/git-bug/git-bug/blob/master/doc/md/git-bug.md。

  • 创建新身份:
bash
git bug user create
  • 创建新问题:
bash
git bug add
  • 推送新问题条目到远程仓库:
bash
git bug push
  • 拉取更新:
bash
git bug pull
  • 列出已有问题:
bash
git bug ls
  • 使用查询条件筛选和排序问题:
bash
git bug ls "{{状态}}:{{open}} {{排序}}:{{edit}}"
  • 按文本内容搜索问题:
bash
git bug ls "{{搜索查询}}" baz

git bugreport · 收集系统和用户的调试信息,生成文本文件以帮助报告 Git 中的问题 ​

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

  • 在当前目录创建新的问题报告文件:
bash
git bugreport
  • 在指定目录创建新的问题报告文件(如目录不存在则自动创建):
bash
git bugreport {{[-o|--output-directory]}} {{path/to/directory}}
  • 使用指定格式的时间戳后缀创建问题报告文件( strftime 格式):
bash
git bugreport {{[-s|--suffix]}} {{%m%d%y}}

配置凭据和缓存方式 ​

先看凭据接口,再按需要查阅内存缓存或文件存储示例。

git credential · 用于管理和存储 Git 用户凭证(如用户名和密码) ​

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

  • 获取凭证信息(从配置文件中读取用户名和密码):
bash
echo "{{url=http://example.com}}" | git credential fill
  • 存储凭证信息(保存到配置的凭证助手中):
bash
echo "{{url=http://example.com}}" | git credential approve
  • 删除凭证信息(从所有配置的凭证助手中清除):
bash
echo "{{url=http://example.com}}" | git credential reject

git credential-cache · Git 凭证缓存助手,用于在内存中临时存储密码 ​

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

  • Git 凭证缓存助手,用于在内存中临时存储密码:
bash
git config credential.helper 'cache --timeout={{秒数}}'

git credential-store · Git 凭证存储助手,用于将密码存储在磁盘上 ​

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

  • 指定凭证存储文件路径:
bash
git config credential.helper 'store --file={{路径/到/文件}}'

继续阅读