error: src refspec my-branch does not match any

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

I created a feature branch from the master branch, and added and committed some change to my branch.

Now I want to push my work to the remote repository, but it failed. What can I do to solve the problem? Thanks.

When running git push command, it didn't ask for my password. Is it normal?

p.s. I am using Windows 10's cmd.

> git push origin my-branch error: src refspec my-branch does not match any. error: failed to push some refs to 'https://git.xxx.net/Infrastructure'   >git commit -m "my work" On branch my-branch nothing to commit, working tree clean  >git branch   master * my-branch  >git show-ref 687f22d54b89e0de91f16cf79d52c6ea21a3f562 refs/heads/master f85d2aa0900fb356d8d120f454ff2362d7475edb refs/heads/my-branch 687f22d54b89e0de91f16cf79d52c6ea21a3f562 refs/remotes/origin/HEAD 687f22d54b89e0de91f16cf79d52c6ea21a3f562 refs/remotes/origin/master   >git log commit f85d2aa0900fb356d8d120f454ff2362d7475edb Author: tim <tim@xxx.com> Date:   Fri Feb 3 23:50:43 2017 -0500      my work  commit 687f22d54b89e0de91f16cf79d52c6ea21a3f562 Author: Kevin <kevin@xxx.com> Date:   Thu Jan 19 12:26:26 2017 -0500      Added gitignore 

回答1:

By default, the push policy is simple (since Git 2.0).
That means Git tries to push to a remote branch named after the local one.

If you don't have any remote branch for that name, you need to explicitly state that you want to create and push said remote branch.
See "Why do I need to explicitly push a new branch?"

your local first push has no idea:

  • where to push
  • what to push (since it cannot find any upstream branch being either recorded as a remote tracking branch, and/or having the same name)

By adding git push -u origin my-branch, you will associate to your local branch a remote (branch.<name>.remote) and a destination (origin/mybranch: branch.<name>.merge)

The next push will be a simple git push.



回答2:

Try this:

  $ git push origin HEAD:my-branch   Or,   $ git push -u origin my-branch  

git push origin HEAD:my-branch push the current branch to the remote ref matching my-branch in the origin repository. This form is convenient to push the current branch without thinking about its local name.

Vs

git push origin my-branch find a ref that matches my-branch in the source repository (most likely, it would find refs/heads/my-branch), and update the same ref (e.g. refs/heads/my-branch) in origin repository with it. If my-branch did not exist remotely, it would be created.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!