错误:源参考规范master与任何内容都不匹配。

85

我尝试按照这篇帖子中提出的解决方法,但它没有起作用,我仍然收到错误信息:src refspec master does not match any。

这是我所做的: 按照这个解决方案。

// adding the file I created
$ git add .
$ git commit -m 'initial commit'
$ git push origin master
error: src refspec master does not match any.

当做:

$ git push origin HEAD:master
b40ffdf..a0d1423  HEAD -> master // looks promising

// adding a remote
$ git remote add devstage -f <another git>
$ git merge devstage/master -s recursive -X ours
$ git push -u devstage master
error: src refspec master does not match any.

更多信息:

$ git branch 
* origin

$ git show-ref
refs/heads/origin
refs/remotes/devstage/master
refs/remotes/origin/HEAD
refs/remotes/origin/devstage
refs/remotes/origin/master
refs/remotes/origin/origin

所以我肯定缺少refs/heads/master,但不知道如何创建它。

谢谢


你是如何创建你的代码库的?你创建了哪个文件? - Michael Ver
@MichaelVer:git克隆 https://<用户名称>@bitbucket.org/<我的代码库> - special0ne
2
最近Github将git push origin master更改为git push origin main - Vintage Coder
这是一个重复的问题 Git: Message https://dev59.com/vm855IYBdhLWcg3wuG0M - Vintage Coder
27个回答

85

这应该会对你有所帮助

git init
git add .
git commit -m 'Initial Commit'
git push -u origin master

1
在执行 git add . 命令后,git push -u origin master 就可以正常工作了。 - Kyung Hwan Min
3
没有将本地 Git 存储库添加到远程存储库,如何能帮助任何人开始?你漏掉了主要步骤 git remote add... - pixel
运行得非常顺利 - GhostRider

48

git branch 看来,你的本地分支名称为 "origin"。

你可以使用 -mv 标志重命名该分支, 就像这样:

git branch -mv origin master

之后,git branch 应该显示 master :-)

为了确保这个名称确实是唯一偏差的事情,你可以运行 git log 并查看最近的几个提交 - 并将它们与 bitbucket 网站上的最近几个提交进行比较。


1
开始工作时,我执行了以下操作:
git clone https://<User>@bitbucket.org/<RepoPath>
这将创建以下.git/config文件: [core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true hideDotFiles = dotGitOnly [remote "origin"] url = https://<User>@bitbucket.org/<RepoPath> fetch = +refs/heads/:refs/remotes/origin/ [branch "origin"] remote = origin merge = refs/heads/origin请问我做错了什么吗?
- special0ne
嗯,只需要执行 git clone repo_address 就可以了。由于某种奇怪的原因,本地分支被命名为“origin”(从 git/config 的“branch [origin]”部分可见)。它为什么这样做?我不知道。你试过我的重命名建议了吗?如果不起作用,也可以手动更改 gitconfig 的 [branch] 部分,如下所示:[branch 'master'] remote = origin merge = refs/heads/master - apprenticeDev
太棒了!我也遇到了这个问题——因为我克隆了一个非主分支检出的存储库(这正是我想要的),但忘记在新存储库中将分支重命名为master - skwidbreth
我在推送到Heroku时遇到了错误,然后发现这个答案非常有用,因为在发布此帖子时,GitHub的主分支从“master”更名为“main”。希望它能帮助未来的某个人,也可能是我自己。 - emppeak
在 GitHub 上创建了一个全新的仓库,并将其设置为主要仓库。克隆后,我们可以看到“main”而不是“master”。因此,在这种情况下,推送应该是 git push -u origin main - Junior Mayhé

43

尝试去做:

git push origin HEAD:master

它起作用了,但如果你必须推送到自己的存储库,它会首先在我的存储库上创建一个拉取请求,然后我必须合并自己的拉取请求。 - Vansh Bhardwaj

38

我有相同的问题,要解决它,请按照以下步骤进行

 git init
 git add .
 git commit -m 'message'
 git push -u origin master    

如果您仍然遇到这个错误,请再按照这些步骤操作一遍。

 git add .
 git commit -m 'message'
 git push -u origin master 

那对我有用,希望它能帮助任何人


这里有一个简单而优雅的代码示例,请接收。 - bibliophilsagar
很好,这帮助我解决了我的问题。 - Sodmond

15

只需添加一个空提交即可通过以下方式解决该问题

$ git commit -m "empty commit" --allow-empty
$ git push

在进行空提交但不编辑任何内容后进行推送


14

尝试以下命令:

git push origin HEAD:master

当我尝试简单地执行git push时,Git抛出了下面的错误。很明显,这是因为Git在推送提交时匹配本地和远程分支。这是push.default的行为,您可以在此处找到更多详细信息。

fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:<Branch_Name>

To push to the branch of the same name on the remote, use

    git push origin <Branch_Name>

To choose either option permanently, see push.default in 'git help config'.

13

我一直遇到同样的错误。

我在本地仓库中添加了文件,尝试使用命令

"git push origin master"

显示了同样的错误。

我漏掉的是没有提交。

" git commit -m 'message' "

运行这个命令之后它就起作用了。


这适用于您在新的存储库上并且从未提交过的情况。 - r3wt
是的!我在我的第一个代码库中遇到了这个问题。 - arslan ahmed mir

7

答案就在错误信息里

错误:src refspec master does not match any。

Github 最近将默认分支更改为 main,详情请访问这里

在您的本地设置中,您可以按照下面的方式重命名本地分支

git branch -m master main

或者你可以从主分支(master)推送到主分支(main)

git push origin master:main

5
运行命令git show-ref,结果为refs/heads/YOURBRANCHNAME。如果你的分支不在其中,则需要通过以下方式切换分支:
git checkout -b "YOURBRANCHNAME"

git show-ref现在会显示您的分支引用。

现在您可以在您的分支上执行操作。


2

错误演示:

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git add --all

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git status
On branch dev
Initial commit
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   index.html
    new file:   photo.jpg
    new file:   style.css

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git push origin dev
error: src refspec dev does not match any.
error: failed to push some refs to 'git@github.com:yourRepo.git'

你可能不需要执行$ git commit -m "描述"
解决方案:
007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git commit -m "discription"
[dev (root-commit) 0950617] discription
 3 files changed, 148 insertions(+)
 create mode 100644 index.html
 create mode 100644 photo.jpg
 create mode 100644 style.css

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git push origin dev
To git@github.com:Tom007Cheung/Rookie-s-Resume.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:yourRepo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

1
请直接将图片中的文本信息复制到您的回答中。然后请解释一下这实际上是对问题的回答。我有印象它并不是一个完整的答案。至少缺乏解释为什么不可能以及如何实际解决问题。 - Yunnosch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接