git 错误:src refspec 不匹配任何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40202284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:34:40  来源:igfitidea点击:

Error: src refspec does not match any

gitgithub

提问by typos

I have a project with a few friends in GitLab, and there is of course the master branch, and there are some others too. When I cloned the repository, I created also an upstream with the command git remote add upstream ....

我和几个朋友在 GitLab 有一个项目,当然有 master 分支,还有一些其他的。当我克隆存储库时,我还使用命令创建了一个上游git remote add upstream ...

Then, I issued the git fetch upstream. Followed by git checkout upstream/test1. Now, if I type git branch -a, I get an output like this:

然后,我发布了git fetch upstream. 紧随其后git checkout upstream/test1。现在,如果我输入git branch -a,我会得到这样的输出:

* (HEAD detached at upstream/test1)
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/upstream/test1
  remotes/upstream/master

This is all fine, but then I did some changes to the code in my upstream/test1branch, and I want to push them to origin/test1repository, I get the error message on the title. Please note that I follow the steps below to push:

这一切都很好,但后来我对我的upstream/test1分支中的代码做了一些更改,我想将它们推送到origin/test1存储库,我收到标题上的错误消息。请注意,我按照以下步骤进行推送:

git add .
git commit -m "Sample message"
git push -u origin test1

If I issue git show-ref, I get the following output:

如果我发出git show-ref,我会得到以下输出:

refs/heads/master
refs/remotes/origin/HEAD
refs/remotes/origin/master
refs/remotes/upstream/test1
refs/remotes/upstream/master

I checked the following questions, but didn't find it helpful. Any ideas how to solve it?

我检查了以下问题,但没有发现它有帮助。任何想法如何解决它?

采纳答案by larsks

You don't appear to have a local branch named test1. You have a remotebranch named test1associated with your upstreamremote.

您似乎没有名为test1. 您有一个与远程关联的远程分支。test1upstream

You shouldn't be editing the upstream/test1branch directly. In fact, attempting to check that out should have yielded a warning:

您不应该upstream/test1直接编辑分支。事实上,尝试检查应该会产生警告:

$ git checkout upstream/test1

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

You should instead first check out a local branch that tracks the remote branch, which ought to look something like this:

您应该首先检查一个跟踪远程分支的本地分支,它应该是这样的:

$ git checkout test1
Branch test1 set up to track remote branch test1 from upstream by rebasing.
Switched to a new branch 'test1'

After doing this, an undecorated git pushwould push to the upstreamremote, while git push origin test1would push to your originremote. Adding the -uflag there would switch the tracking branch so that instead of tracking upstream/test1, your branch would track origin/test1, so that future git pulland git pushoperations would refer to that remote branch (origin/test1) by default.

执行此操作后,未装饰的git push将推送到upstream遥控器,同时git push origin test1推送到您的origin遥控器。在-u那里添加标志将切换跟踪分支,以便upstream/test1您的分支将跟踪而不是跟踪origin/test1,因此默认情况下未来git pullgit push操作将引用该远程分支 ( origin/test1)。

回答by Akshay Kumar

The following will create a new branch in local and in remote as well. I follow this when I create a new branch to work with.

下面将在本地和远程创建一个新分支。当我创建一个新的分支来使用时,我会遵循这个。

git checkout master

git pull origin master

git checkout -b your-branch

git 结帐大师

git pull 原点大师

git checkout -b你的分支

Make the new changes

进行新的更改

git push -u origin your-branch

git push -u origin你的分支

回答by vikash singh

In my case I had made changes in master branch and I was trying to push the changes to development branch and therefore getting the error. What I did was -

就我而言,我在 master 分支中进行了更改,并且我试图将更改推送到 development 分支,因此出现错误。我所做的是——

git checkout development

made my changes and then ran

做了我的改变然后跑了

git add .
git commit -m "my changes"
git push origin development

回答by toddmetheny

For me the problem was I was trying to push to a branch name inside a flag/folder for types we have set up (feature / bugfix / etc) and the folder name wasn't showing because of the way I have my terminal prompt set up. I needed to run gpo feature/branch-nameand was just absent-mindedly running gpo branch-name. (gpois the alias I have set up for git push origin, btw).

对我来说,问题是我试图将我们设置的类型(功能/错误修复/等)推送到标志/文件夹中的分支名称,但由于我设置终端提示的方式,文件夹名称没有显示向上。我需要跑步gpo feature/branch-name,只是心不在焉地跑步gpo branch-name。(gpo是我为 设置的别名git push origin,顺便说一句)。

Just in case this helps anyone.

以防万一这对任何人都有帮助。