2

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
Tim
  • 1
  • 141
  • 372
  • 590

2 Answers2

2

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

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.

Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • Thanks. Can you explain what might be my problem and what the commands you wrote do? – Tim Feb 04 '17 at 05:16
  • Thanks. Why does `git push -u origin my-branch ` work, while `git push origin HEAD:my-branch` doesn't work with the same error? – Tim Feb 04 '17 at 07:05
  • Here, somehow `local/my-branch` is not finding remote tracking branch ("`error: src refspec my-branch does not match any`"). So, `-u = --set-upstream` flag with "git push" tells git to track the newly created remote branch (`my-branch`). – Sajib Khan Feb 04 '17 at 07:46