当我试图推动原点时,为什么Git告诉我“没有这样的远程'起源'?

时间:2014-08-26 09:59:37

标签: git github push git-remote

我对Git很新;我最近才创建了一个GitHub帐户。

我只是试图推送我的第一个存储库(示例项目),但我收到以下错误:

No such remote 'origin'

我运行了以下命令:

git init
git commit -m "first commit"
git remote add origin https://github.com/VijayNew/NewExample.git
git push -u origin master

但是,当我运行git commit -m"首次提交"时,我收到以下消息:

nothing added to commit but untracked files present (use "git add" to track)

然后我尝试使用

设置origin
git remote set-url origin https://github.com/VijayNew/NewExample.git

但是我收到了以下错误:

No such remote 'origin'

我做错了什么,我该怎么办?

4 个答案:

答案 0 :(得分:126)

两个问题:

1 - 你从未告诉Git开始跟踪任何文件

你写的是你跑了

git init
git commit -m "first commit"

那个,在那个阶段,你得到了

nothing added to commit but untracked files present (use "git add" to track).

Git告诉你,你从来没有告诉它开始跟踪任何文件,它没有什么可以拍摄快照。因此,Git不会创建任何提交。在尝试提交之前,您应该告诉Git(例如):

  

嘿Git,你看到README.md文件闲置在我的工作目录中吗?你能把它置于版本控制之下吗?我希望它能够进入我的第一个提交/快照/修订版......

为此,您需要使用

stage感兴趣的文件
git add README.md
在运行之前

git commit -m "some descriptive message"

2 - 您尚未设置远程存储库

然后你跑了

git remote add origin https://github.com/VijayNew/NewExample.git

之后,您的本地存储库应该能够与驻留在指定URL(https://github.com/VijayNew/NewExample.git)的远程存储库进行通信...前提是远程存储实际存在! 但是,您似乎从未在GitHub上创建过远程仓库:在撰写此答案时,如果我尝试访问相应的URL,我会

enter image description here

在尝试推送到该远程存储库之前,您需要确保后者实际存在。所以去GitHub并创建有问题的远程仓库。然后,只有这样你才能成功推进

git push -u origin master

答案 1 :(得分:32)

我猜你在提交失败后没有运行此命令,所以实际运行它来创建遥控器:

 git remote add origin https://github.com/VijayNew/NewExample.git

提交失败,因为您需要git add要跟踪的某些文件。

答案 2 :(得分:11)

当我试图将本地创建的存储库与github上的空白存储库链接时,我遇到了这个问题。 最初,我尝试使用git remote set-url,但是我不得不做git remote add

git remote add origin https://github.com/VijayNew/NewExample.git

答案 3 :(得分:1)

以下步骤对我有用:

初始化

首先,初始化存储库以使用 Git,以便跟踪任何文件更改:

git init

创建别名 origin

然后,检查您要与别名 origin 关联的远程存储库是否存在,如果不存在,请先在 git 中创建它。

$ git ls-remote https://github.com/repo-owner/repo-name.git/

如果存在,将其与远程别名“来源”相关联:

git remote add origin https://github.com:/repo-owner/repo-name.git

并使用git remote -v检查远程别名“来源”属于哪个网址:

$ git remote -v
origin  https://github.com:/repo-owner/repo-name.git (fetch)
origin  https://github.com:/repo-owner/repo-name.git (push)

验证别名来源

接下来,验证您的别名 origin 是否正确别名,如下所示:

$ cat ./.git/config
:
[remote "origin"]
        url = https://github.com:/repo-owner/repo-name.git
        fetch = +refs/heads/*:refs/remotes/origin/*
:

必须查看此部分 [remote "origin"]。您可以考虑使用适用于 Windows 和 MacOS 的 GitHub Desktop,它可以帮助我自动填充 ~./git/config 文件中缺少的部分,或者您可以手动添加它,虽然不是很好,但它有效!

从远程主分支拉取任何内容

$ git pull origin main

这会将您刚刚别名为 origin 的存储库中的任何内容提取到本地存储库,包括 .gitignore,在此过程中创建分支 main

检查主分支

$ git branch
* main

这将向您显示 main 分支已创建,您现在已在该分支上。

可选

您可能还想更改 origin 别名 以使其更直观,尤其是在使用多个 origin 时:

git remote rename origin my-super-git-repo

终于

git add .
git status //If you want to check what's going to be committed
git commit -m 'First commit' //-m is for message
git push origin main

你会看到一串如下的行:

<块引用>

枚举对象:22,完成。
计数对象:100% (22/22),完成。
使用多达 8 个线程的 Delta 压缩
压缩对象:100% (13/13),完成。
写入对象:100% (21/21), 4.29 KiB | 292.00 KiB/s,完成。
总计 21 (delta 2),重用 0 (delta 0),pack-reused 0
远程:解决增量:100% (2/2),完成。
https://github.com/repo-owner/repo-name.git

948279c..1f3b0b8 主 -> 主