Git 里面的 origin 到底代表啥意思?

你的代码库(repository)可以存放在你的电脑里,同时你也可以把代码库托管到Github的服务器上。

在默认情况下,origin指向的就是你本地的代码库托管在Github上的版本。

我们假设你首先在github上创建了一个Repository,叫做repository,假设你的Github ID是user1,这个时候指向你的代码库的链接是

https://github.com/user1/repository

如果你在terminal里输入

git clone https://github.com/user1/repository

那么git就会在本地拷贝一份托管在github上的代码库

这个时候你cd到repository

然后输入

git remote -v

你会看到控制台输出

origin https://github.com/user1/repository.git (fetch)
origin https://github.com/user1/repository.git (push)

也就是说git为你默认创建了一个指向远端代码库的origin(因为你是从这个地址clone下来的)

再假设现在有一个用户user2 fork了你个repository,那么他的代码库链接就是这个样子

https://github.com/user2/repository

如果他也照着这个clone一把,然后在他的控制台里输入

git remote -v

他会看的的就是

origin https://github.com/user2/repository.git (fetch)
origin https://github.com/user2/repository.git (push)

可以看的origin指向的位置是user2的的远程代码库

这个时候,如果user2想加一个远程指向你的代码库,他可以在控制台输入

git remote add upstream https://github.com/user1/repository.git

然后再输入一遍 git remote -v

输出结果就会变为

origin https://github.com/user2/repository.git (fetch)
origin https://github.com/user2/repository.git (push)
upstream https://github.com/user1/repository.git (push)
upstream https://github.com/user1/repository.git (push)

增加了指向user1代码库的upstream,也就是之前对指向位置的命名。

总结来讲,顾名思义,origin就是一个名字,它是在你clone一个托管在Github上代码库时,git为你默认创建的指向这个远程代码库的标签

转载:https://www.zhihu.com/question/27712995/answer/39946123




posted @   迷走神经  阅读(932)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示