Git 中的核心术语

举报
黄生 发表于 2026/09/03 11:16:18 2026/09/03
【摘要】 1. Origin英文定义:The default name Git gives to the remote repository you cloned from. It’s like an alias (nickname) for the URL of your fork.中文理解:“origin” 就是你克隆的那个远程仓库的默认别名。当你 git clone <某个URL> 时,Git...

1. Origin

英文定义:
The default name Git gives to the remote repository you cloned from. It’s like an alias (nickname) for the URL of your fork.

中文理解:
“origin” 就是你克隆的那个远程仓库的默认别名

  • 当你 git clone <某个URL> 时,Git 自动把这个 URL 命名为 origin
  • 它不是一个固定的东西,只是一个指向远程地址的快捷方式

例句:

“I pushed my changes to origin main.”
→ 我把修改推送到 我克隆来源的那个远程仓库 的 main 分支。

2. Main

英文定义:
The default branch name in modern Git repositories. It represents the primary development line.

中文理解:
main分支(branch)的名字,是项目的主干分支。

  • 以前叫 master,现在逐渐改为 main
  • 它就像一棵树的树干,其他分支(feature、dev)从它分出去

例句:

“Please merge your feature branch into main.”
→ 请把你的功能分支合并到主分支

类比:
一本书的主线剧情(main storyline)——其他支线剧情都从这里分叉。

3. Lease /liːs/(名词,在 Git 中是 --force-with-lease

英文定义:
A safety mechanism. It means “force push only if the remote branch hasn’t been updated by someone else since your last fetch.”

中文理解:
lease = 租约、契约。在 Git 里,--force-with-lease带条件地强制推送

命令 含义
git push --force 无条件覆盖远程(危险!像"砸门而入")
git push --force-with-lease “我保证我看到的远程状态是最新的,如果不是,就拒绝推送”(像"用钥匙开门,但先确认锁没被换过")

例句:

“Use --force-with-lease instead of --force to avoid destroying others’ work.”
→ 用 --force-with-lease 代替 --force,避免摧毁别人的工作。

4. Unshallow /ʌnˈʃæləʊ/(动词)

英文定义:
To convert a “shallow” clone (with truncated history) into a full clone by fetching all missing commits.

中文理解:
shallow = 浅的(只有表层)。unshallow = “去浅层化”,把浅克隆补全为完整仓库。

拆解:

  • shallow clone(浅克隆):git clone --depth 1 只拿最新的 1 次提交
  • unshallow:把缺失的历史记录全部下载回来

例句:

“Your clone is shallow. Run git fetch --unshallow to get the full history.”
→ 你的克隆是浅的。运行 git fetch --unshallow 来获取完整历史。

类比:
你只看了电影的最后 5 分钟(浅克隆),现在想看全片(unshallow)——你把前面所有的剧情都下载下来。


5. Upstream

英文定义:
The original repository that your fork came from. It’s “upstream” because code flows from it to your fork (like a river).

中文理解:
upstream = 上游(河流的上游)。

  • 你的 fork 是从别人的仓库复制来的,那个原始仓库就是你的 upstream
  • 代码流向:upstream(上游)→ 你的 fork(下游)

例句:

“Add the original repo as upstream so you can sync with it.”
→ 把原始仓库添加为 upstream,这样你就能和它同步了。

类比:
长江是上游(upstream),你所在的湖泊是下游(downstream)——水从上游流下来。

配置方式:(注意:不是默认的而是需要配置的)

git remote add upstream https://github.com/原作者/仓库.git

6. Remote

英文定义:
A version of your repository that is hosted on a server (like GitHub). It’s “remote” because it’s not on your local machine.

中文理解:
remote = 远程仓库

  • 相对于你电脑上的本地仓库(local),GitHub 上的仓库就是 remote
  • origin 是 remote 的一种(默认的),upstream 也是 remote 的一种

例句:

“Use git remote -v to see all your remote repositories.”
→ 用 git remote -v 查看你所有的远程仓库。

类比:
你的电脑是"家"(本地),GitHub 服务器是"公司"(远程)。remote 就是公司的地址。

串起来的完整场景

假设你 fork 了一个开源项目:

# 1. 克隆你自己的 fork(origin = 你的fork)
git clone https://github.com/你的用户名/项目.git

# 2. 添加上游仓库(upstream = 原作者仓库)
git remote add upstream https://github.com/原作者/项目.git

# 3. 如果之前用了浅克隆,补全历史
git fetch --unshallow

# 4. 从上游拉取最新代码
git fetch upstream
git merge upstream/main

# 5. 推送到你的 fork(origin)
git push origin main

# 6. 如果遇到冲突,用安全的强制推送
git push --force-with-lease origin main

记忆口诀

术语 口诀
origin 你 clone 的那个远程仓库的"小名"
main 主干分支(像树的树干)
lease 强制推送时的"安全带"(检查后再覆盖)
unshallow 把"只看结局"变成"看全片"
upstream 你 fork 来源的"上游"仓库
remote 所有不在你电脑上的仓库统称

小测验

  1. originupstream 都是 ______。(答案:remote)
  2. 如果你想看完整历史,应该用 git fetch ______。(答案:–unshallow)
  3. 安全地强制推送用 git push ______。(答案:–force-with-lease)
  4. main 是 ______ 的名字。(答案:分支 branch)
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。