Git | 配置别名操作

基本概念

  • 作用 ------ 通过简短的别名替代复杂的 Git 命令,提高效率

  • 配置级别

    • 命令加 --global / 修改 ~/.gitconfig 文件 ------ 对 当前用户的所有仓库 生效
    • 命令不加 --global / 修改 .git/config 文件 ------ 对 当前仓库 生效

命令行配置别名

  • 用 st 表示 status ,查看 Git 仓库状态

    shell 复制代码
    $ git config --global alias.st status
    $ git st
  • 用 ci 表示 commit,提交代码

    shell 复制代码
    $ git config --global alias.ci commit
    $ git ci -m "bala bala bala..."
  • 用 unstage 表示 reset HEAD,撤销暂存区的修改

    shell 复制代码
    $ git config --global alias.unstage 'reset HEAD'
    $ git unstage test.py
  • 用 last 表示 log -1,显示最后一次提交信息

    shell 复制代码
    $ git config --global alias.last 'log -1'
    $ git last
    commit adca45d317e6d8a4b23f9811c3d7b7f0f180bfe2
    Merge: bd6ae48 291bea8
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Thu Aug 22 22:49:22 2013 +0800
    
        merge & fix hello.py
  • 用 lg 表示 log --color --graph ...,自定义的彩色日志格式

    shell 复制代码
    $ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
    $ git lg

文件配置别名

  • 配置当前仓库 ------ 修改项目根目录 .git/config 文件

    ini 复制代码
    $ cat .git/config 
    [core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
    [remote "origin"]
        url = git@github.com:michaelliao/learngit.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
        remote = origin
        merge = refs/heads/master
    [alias]
        last = log -1
  • 配置当前用户的所有仓库 ------ 修改用户主目录 .gitconfig 文件

    ini 复制代码
    $ cat .gitconfig
    [alias]
        co = checkout
        ci = commit
        br = branch
        st = status
    [user]
        name = Your Name
        email = your@email.com

相关推荐
wflynn8 小时前
GitHub 日榜趋势速报 | 2026-11-18
开源·github
Hvitur9 小时前
Git - 多分支合并
git·github
考虑考虑11 小时前
git刷新远程分支列表
git·后端·gitlab
laoyeye83113 小时前
手写 vLLM 分页 KV Cache(三):引用计数、写时复制(CoW)与前缀缓存
人工智能·github
一条Go16 小时前
边缘 Agent:为什么是它连出来,而不是平台连进去
后端·架构·github
一条Go16 小时前
开源新生代雷达 · 第 15 期
github
AI大模型-小华16 小时前
Codex 自动测试怎么用?从生成测试用例到修复失败用例
git·测试用例·ai编程·软件开发·codex·chatgpt pro·codex cli
OpenTiny社区17 小时前
我用Vue 100%复制DeepSeek开源的网页App,代码更少,功能还更多!
前端·vue.js·github
Bigger17 小时前
我扒了 GitHub Trending 前 20,8 个在做同一件事:给 Coding Agent 造基础设施
开源·github·ai编程
徐小夕18 小时前
MedDoc AI:把临床书写、协作、质控、知识问答装进一个工作台
前端·vue.js·github