Git Refspec
一句话解释
refspec 告诉 Git 在 push 与 fetch 期间哪些本地与远程引用对应。
用大白话说
refspec(引用规范)是在 git push 与 git fetch 期间把源引用映射到目标引用的字符串。格式为 +src:dst——可选 + 允许非快进更新,src 是源引用,dst 是目标。refspec 使 push 与 fetch 在仓库间移动引用。运行 git push origin main 时,Git 用 refspec main:main(来自配置的 remote.origin.push,或从 main 推断)。可写显式 refspec 做非常规事:git push origin feature:experimental 把你的 feature 分支推到远程的 experimental 分支。+ 前缀即使非快进也强制更新。gitconfig 中的 refspec([remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/*)定义默认 fetch 映射——所有远程分支镜像到 refs/remotes/origin/ 下。理解 refspec 解锁对哪些引用移到哪里的精确控制,适用于镜像、部分同步与 CI 工作流。
示例
bash
# Push local feature to remote experimental branch
git push origin feature:experimental
# Force-push (non-fast-forward allowed)
git push origin +feature:feature
# Fetch a remote tag into a local tag
git fetch origin refs/tags/v1.0:refs/tags/v1.0
# Delete a remote branch by pushing an empty source
git push origin :old-branch
# Default fetch refspec in .git/config
# [remote "origin"]
# fetch = +refs/heads/*:refs/remotes/origin/*src:dst 把源映射到目标引用。+ 允许强制。空 src 删除 dst。默认 fetch refspec 把所有远程分支本地镜像。
相关术语
相关资源
相关课程
- Git 内部
学习 refspec。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。