Skip to content

Git Alias

一句话解释

git alias 是在 git config 中定义的自定义快捷方式,展开为更长的 Git 命令或序列。

用大白话说

git alias 让你为更长的 Git 命令定义一个短名。你用 git config 配置它,然后像内置命令一样使用。别名省去输入并减少你频繁运行命令的错误。例如把 "co" 别名为 "checkout"、"br" 为 "branch"、"ci" 为 "commit -s"、"st" 为 "status -sb"。于是 git co main 运行 git checkout main。你还可通过加 ! 前缀别名为 shell 命令,实现强力组合。别名存在 gitconfig 的 [alias] 段。它们不改变 Git 行为——纯粹是快捷方式。许多开发者在 dotfiles 中共享一组通用别名,以在多机保持一致工作流。

示例

bash
# Define common shortcuts
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci "commit -s"
git config --global alias.st "status -sb"

# Use them like built-in commands
git co main          # git checkout main
git br -d feature    # git branch -d feature
git ci -m "fix"      # git commit -s -m "fix"

# Alias to a shell command (prefix with !)
git config --global alias.lg "!git log --oneline --graph --all"

别名经 git config alias.NAME VALUE 设置。用 git NAME 调用。加 ! 前缀运行 shell 命令。

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表