消息“src refspec master 与任何内容都不匹配”在 Git 中推送提交时

发布于 2024-10-02 15:13:46 字数 422 浏览 8 评论 0原文

我克隆我的存储库:

git clone ssh://xxxxx/xx.git 

但是在更改一些文件并添加提交它们之后,我想将它们推送到服务器:

git add xxx.php
git commit -m "TEST"
git push origin master

但是我得到的错误是:

error: src refspec master does not match any.  
error: failed to push some refs to 'ssh://xxxxx.com/project.git'

I clone my repository with:

git clone ssh://xxxxx/xx.git 

But after I change some files and add and commit them, I want to push them to the server:

git add xxx.php
git commit -m "TEST"
git push origin master

But the error I get back is:

error: src refspec master does not match any.  
error: failed to push some refs to 'ssh://xxxxx.com/project.git'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

剧终人散尽 2024-10-09 15:13:46

也许你只需要承诺。当我这样做时,我遇到了这个:

mkdir repo && cd repo
git init
git remote add origin /path/to/origin.git
git add .

哎呀!从来没有犯过!

git push -u origin master
error: src refspec master does not match any.

我所要做的就是:

git commit -m "initial commit"
git push origin main

成功!

Maybe you just need to commit. I ran into this when I did:

mkdir repo && cd repo
git init
git remote add origin /path/to/origin.git
git add .

Oops! Never committed!

git push -u origin master
error: src refspec master does not match any.

All I had to do was:

git commit -m "initial commit"
git push origin main

Success!

北风几吹夏 2024-10-09 15:13:46
  1. 尝试使用 git show-ref 来查看您有哪些参考文献。有refs/heads/master吗?

由于最近的“在 GitHub 中用 main 替换 master”操作,您可能会注意到有一个 refs/heads/main。因此,以下命令可能会从 git push origin HEAD:master 更改为 git push origin HEAD:main

  1. 您可以尝试 git push origin HEAD:master< /code> 作为一个更独立于本地引用的解决方案。这明确表明您想要将本地引用 HEAD 推送到远程引用 master (请参阅 git-push refspec 文档)。
  1. Try git show-ref to see what refs you have. Is there a refs/heads/master?

Due to the recent "Replacing master with main in GitHub" action, you may notice that there is a refs/heads/main. As a result, the following command may change from git push origin HEAD:master to git push origin HEAD:main

  1. You can try git push origin HEAD:master as a more local-reference-independent solution. This explicitly states that you want to push the local ref HEAD to the remote ref master (see the git-push refspec documentation).
我不吻晚风 2024-10-09 15:13:46

删除本地计算机上的所有文件后,我也遇到了类似的错误,我必须清理存储库中的所有文件。

我的错误消息是这样的:

error: src refspec master does not match any.
error: failed to push some refs to 'git@github ... .git'

通过执行以下命令解决了这个问题:

touch README
git add README

git add (all other files)
git commit -m 'reinitialized files'
git push origin master --force  # <- caution, --force can delete others work.

I also had a similar error after deleting all files on my local computer, and I have to clean up all files in the repository.

My error message was something like this:

error: src refspec master does not match any.
error: failed to push some refs to 'git@github ... .git'

And it was solved by executing the following commands:

touch README
git add README

git add (all other files)
git commit -m 'reinitialized files'
git push origin master --force  # <- caution, --force can delete others work.
稚然 2024-10-09 15:13:46
git push -u origin master
error: src refspec master does not match any.

为此,您需要输入如下提交消息,然后推送代码:

git commit -m "initial commit"

git push origin master

成功推送到master。

git push -u origin master
error: src refspec master does not match any.

For that you need to enter the commit message as follows and then push the code:

git commit -m "initial commit"

git push origin master

Successfully pushed to master.

〆凄凉。 2024-10-09 15:13:46

对我来说,我必须确保公钥在服务器上正确配置(附加在〜/.ssh/authorized_keys中)和GitHub/Bitbucket (添加到我的GitHub 或 Bitbucket 上的 SSH 密钥) - 它们需要匹配。然后:

git add --all :/
git commit -am 'message'
git push -u origin master

For me I had to make sure the public key is properly configured on the server (appended in ~/.ssh/authorized_keys) and in GitHub/Bitbucket (added to my SSH keys on GitHub or Bitbucket) - they need to match. Then:

git add --all :/
git commit -am 'message'
git push -u origin master
标点 2024-10-09 15:13:46

在我仅使用一个空目录运行 git add 后,在一个全新的存储库中发生了这种情况。

一旦我添加了一个文件(例如 git add README.md),那么 git push 就工作得很好。

This happened to me in a brand new repository after I ran git add with only an empty directory.

As soon as I added a file (e.g. a git add README.md), then git push worked great.

缺少或跳过 git add .git commit 可能会导致此错误:

git push -u origin master
Username for 'https://github.com': yourusername
Password for 'https://[email protected]': 
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/yourusername/foobar.git'

要修复它,请重新初始化并遵循正确的顺序:

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

Missing or skipping git add . or git commit may cause this error:

git push -u origin master
Username for 'https://github.com': yourusername
Password for 'https://[email protected]': 
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/yourusername/foobar.git'

To fix it, reinitialize and follow the proper sequence:

git init
git add .
git commit -m 'message'
git *create remote
git push -u origin master
初懵 2024-10-09 15:13:46

面临的问题

当我在 GitHub 上创建一个新存储库并将其与我拥有的客户端计算机中的 React 应用程序链接时,我遇到了同样的问题。

我使用了以下步骤:

问题之前使用的命令

git init
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

我的错误

但正如你所看到的,我的错误是没有使用 git add . 命令。
我犯了这个错误,因为我已经有了 README.md 文件,并且 GitHub 在创建存储库时指导我们使用基本命令。

我的解决方案

我的解决方案是在git init命令之后使用git add .

以相同的顺序使用以下命令集来解决该问题:

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

Problem faced

I had the same problem when I was creating a new repository on GitHub and linking it with my React app in the client computer I have.

I used the following steps:

Commands used before the problem

git init
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

My mistake

But as you can see, my mistake was not using the git add . command.
I did this mistake, because I already had the README.md file and GitHub instructs us with basic commands while creating the repository.

My solution

My solution is to use git add . after the git init command.

Use the following set of commands in the same order to overcome the problem:

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main
与之呼应 2024-10-09 15:13:46

要修复此问题,请重新初始化并遵循正确的代码顺序:

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

To fix it, re-initialize and follow the proper code sequence:

git init
git add .
git commit -m 'message'
git push -u origin master
书间行客 2024-10-09 15:13:46

当您位于特定分支并尝试推送尚不存在的另一个分支时,也会发生这种情况,例如:

$ git branch
* version-x  # you are in this branch
  version-y

$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'origin_address'

This happens too when you are in a specific branch and try to push another branch that does not exist yet, like:

$ git branch
* version-x  # you are in this branch
  version-y

$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'origin_address'
a√萤火虫的光℡ 2024-10-09 15:13:46

我遇到了同样的问题,我使用了--allow-empty

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

补充

这个问题的主要原因之一是一些Git服务器,例如BitBucket,没有他们的master< /code> 分支在克隆新存储库时初始化。

I faced the same problem, and I used --allow-empty:

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

Supplement

One of main reasons of this problem is that some Git servers, such as BitBucket, don't have their master branch initialized when a fresh repository is cloned.

青巷忧颜 2024-10-09 15:13:46

几天前我遇到了同样的问题。

如果您现在(2020 年)创建了一个新存储库,那么默认分支是 GitHub 上的ma​​in

您现在可以在您的存储库分支中检查 GitHub。

您还可以通过运行命令来检查终端中的分支:

git branch

这就是为什么您需要运行

git push origin main

而不是

git push origin master

I faced the same issue some days ago.

If you created a new repository nowadays (2020) then the default branch is main on GitHub.

You can check on GitHub now in your repository branches.

And you can also check the branch in the terminal by running the command:

git branch

So that's why you need to run

git push origin main

instead of

git push origin master
深府石板幽径 2024-10-09 15:13:46

确保您已先添加,然后提交/推送:

例如:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master

Make sure you've added first, and then commit/ push:

Like:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master
年华零落成诗 2024-10-09 15:13:46

两种可能性:

1-您忘记包含 .gitignore 文件。

以下是所需的所有步骤:

  1. 在远程创建一个空的 Git 存储库,

  2. 在本地创建 .gitignore
    为您的项目创建文件。 GitHub 为您提供了示例列表

  3. 启动终端,然后在您的项目中执行以下操作命令:

    git 远程添加源 YOUR/ORIGIN.git
    
    git 添加 .
    
    git commit -m“初始提交或第一次提交的任何消息”
    
    git Push -u 原始主机
    

2- 或者您正在尝试创建一个新的 GitHub 项目。

GitHub 将 master 替换为 main 作为默认分支名称。要解决该问题:

  1. 在您的本地项目上:
    1. 删除 .git 文件夹(如果存在)
    2. 通过在项目中启动以下内容来重新创建一个干净的存储库:

在终端中:

git init

git add .

git commit -m "YOUR FIRST MESSAGE HERE"

git branch -M main

git remote add origin _GIT_LINK_TO_PROJECT_HERE_

git push -u origin main

Two possibilities:

1- Either you forgot to include the .gitignore file.

Here are all the steps required:

  1. Create an empty Git repository on remote,

  2. On local, create the .gitignore
    file for your project. GitHub gives you a list of examples here

  3. Launch a terminal, and in your project do the following commands:

    git remote add origin YOUR/ORIGIN.git
    
    git add .
    
    git commit -m "initial commit or whatever message for first commit"
    
    git push -u origin master
    

2- Or you are trying to create a new GitHub project.

GitHub replaced master with main as the default branch name. To resolve the issue:

  1. On your local project:
    1. remove the .git folder if it exists
    2. recreate a clean repository by launching the following in your project:

In the terminal:

git init

git add .

git commit -m "YOUR FIRST MESSAGE HERE"

git branch -M main

git remote add origin _GIT_LINK_TO_PROJECT_HERE_

git push -u origin main
凯凯我们等你回来 2024-10-09 15:13:46

对我来说,以下工作是移动未跟踪的文件:

git add --all

接下来,我遵循类似的步骤

 git commit -m "First commit"

然后,

git remote add origin git@github.....

最后但并非最不重要的:

git push -u origin master

当您执行此操作时,Windows 安全将弹出,询问您的用户名和密码。

For me,following worked to move untracked files:

git add --all

Next, I followed similar steps

 git commit -m "First commit"

Then,

git remote add origin git@github.....

Last but not the least:

git push -u origin master

As you do this, Windows security will pop up asking for your username and password.

年少掌心 2024-10-09 15:13:46

您可能忘记了 git init 命令之后的命令 git add .

You probably forgot the command git add . after the git init command.

━╋う一瞬間旳綻放 2024-10-09 15:13:46

GitHub 2000-10-01 更新后,您应该使用 main 而不是 master

这样做...

  1. 在 GitHub 上创建存储库
  2. 删除本地目录中现有的 .git 文件
  3. 转到本地项目目录并输入 git init< /code>
  4. git add .
  5. git commit -m"My first commit"
  6. 现在检查您的分支名称。它将是本地项目中的 master
  7. git remote add origin,然后输入 git remote -v< /code>
  8. git push -f origin master
  9. 现在检查 GitHub 存储库。您将看到两个分支 1. main 2. master
  10. 在本地存储库中创建一个新分支,分支名称将为 main
  11. git checkout main
  12. git merge master
  13. git pull origin main
  14. git push -f origin main

注意:从 2020 年 10 月 1 日起, GitHub 决定使用 main 而不是 master 分支作为默认分支名称。

After the GitHub update 2000-10-01, you should use main instead of master.

Do it like this way...

  1. Create a repository on GitHub
  2. Delete existing .git file in your local directory
  3. Go to the local project directory and type git init
  4. git add .
  5. git commit -m"My first commit"
  6. Now check your branch name. It will be master in your local project
  7. git remote add origin <remote repository URL past here from the GitHub repository>, and then type git remote -v
  8. git push -f origin master
  9. Now check the GitHub repository. You will see two branch 1. main 2. master
  10. In your local repository create a new branch and the branch name will be main
  11. git checkout main
  12. git merge master
  13. git pull origin main
  14. git push -f origin main

Note: from 2020-10-01, GitHub decided use main instead of master branch to use as the default branch name.

安静被遗忘 2024-10-09 15:13:46

只需添加初始提交即可。请按照以下步骤操作:

  • git add .

  • git commit -m "initial commit"< /p>

  • git Push origin master

这适用于我。

Just add an initial commit. Follow these steps:

  • git add .

  • git commit -m "initial commit"

  • git push origin master

This worked for me.

梦里°也失望 2024-10-09 15:13:46

我也遇到了同样的问题,这解决了我的问题:

只需创建一个分支:

git checkout -b“master”

之后,

git push -u origin master

繁荣。

I have faced the same issue, and this solved my problem:

Just make a branch:

git checkout -b "master"

After that,

git push -u origin master

Boom.

旧人九事 2024-10-09 15:13:46

2022 年 2 月更新:

如果您的分支“main”

在此处输入图像描述

运行此命令

git push origin main

如果您的分支“master”

在此处输入图像描述

运行此命令

git push origin master

Feb, 2022 Update:

If your branch is "main":

enter image description here

Run this command:

git push origin main

If your branch is "master":

enter image description here

Run this command:

git push origin master
我是男神闪亮亮 2024-10-09 15:13:46

我的问题是“主”分支尚未在本地创建。

A Quick

git checkout -b "master"

创建了主分支,此时,A Quick

git push -u origin master

将工作推送到了 Git 存储库。

My issue was that the 'master' branch hadn't been created locally yet.

A quick

git checkout -b "master"

created the master branch, at which point, a quick

git push -u origin master

pushed the work up to the Git repository.

你的往事 2024-10-09 15:13:46

也许分支是main而不是master

尝试

git push origin HEAD:main

git push origin main

Maybe the branch is main instead of master.

Try

git push origin HEAD:main

or

git push origin main

画尸师 2024-10-09 15:13:46

GitHub 将默认分支名称从 master 更改为 main
因此,如果您最近创建了存储库,请尝试推送 main 分支:

git push origin main

参考

重命名master 的默认分支 (GitHub)

GitHub changed the default branch name from master to main.
So if you created the repository recently, try pushing the main branch:

git push origin main

Reference

Renaming the default branch from master (GitHub)

溇涏 2024-10-09 15:13:46

当您添加文件但忘记提交和推送时,就会发生这种情况。
因此提交文件然后推送。

This happens when you have added your file, forgot to commit and pushing.
So commit the files and then push.

尴尬癌患者 2024-10-09 15:13:46
  1. 首先,git add .
  2. 其次,git commit -m "message"
  3. 第三,git push originbranch

请检查拼写错误,因为这也可能会导致错误。给出那个错误。

  1. First, git add .
  2. Second, git commit -m "message"
  3. Third, git push origin branch

Please check for spelling mistakes because that could also give that error.

深海少女心 2024-10-09 15:13:46

如果您在分离 HEAD 模式下工作时遇到此错误,您可以执行以下操作:

git push origin HEAD:remote-branch-name

另请参阅:从分离的 head 进行 Git 推送

如果您位于与远程分支不同的本地分支,您可以执行以下操作:

git push origin local-branch-name:remote-branch-name

If you get this error while working in detached HEAD mode, you can do this:

git push origin HEAD:remote-branch-name

See also: Making a Git push from a detached head

If you are on a different local branch than the remote branch, you can do this:

git push origin local-branch-name:remote-branch-name
战皆罪 2024-10-09 15:13:46

如果您在第一次推动之前忘记承诺,就会发生这种情况。只需运行:

git commit -m "first commit"

It happens if you forget to commit before pushing for the first time. Just run:

git commit -m "first commit"
不交电费瞎发啥光 2024-10-09 15:13:46

要检查当前状态,git status

并按照以下步骤操作:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master

To check the current status, git status.

And follow these steps as well:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master
楠木可依 2024-10-09 15:13:46

这只是意味着您忘记进行初始提交,请尝试

git add .
git commit -m 'initial commit'
git push origin master

This just mean you forgot to do the initial commit, try

git add .
git commit -m 'initial commit'
git push origin master
×纯※雪 2024-10-09 15:13:46

当我错过运行时,我遇到了同样的问题:(

git add .

你必须至少有一个文件,否则你会再次收到错误。)

I had the same problem when I missed to run:

git add .

(You must have at least one file, or you will get the error again.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文