0

I want to push my local work to a branch i tried the following commands on master :

git add -A
git commit -m "message of commit"
git push -u origin <branch_name>

And gets error: src refspec failed to push some refs to the repo. also if a pull (on the branch master) and resolve conflicts (don't know why i have to resolve them ) between existing master and my code i am still not able to push to the new branch.

If i push by setting a HEAD then it's working, but i don't want to push to a detached head. The problem is fairly easy (push code to a newly created branch) still it's not working.

Thank you for your suggestions.

  • Your commands work for me. Can you post a more complete error message? – derekbaker783 Apr 28 '20 at 10:58
  • Yes but i want them working here :) the error message is "error: failed to push some refs to `myrepo` in Red –  Apr 28 '20 at 11:00
  • Where are you pushing? (Github, Gitlab, Bitbucket, Azure DevOps, etc.) – derekbaker783 Apr 28 '20 at 11:07
  • Bitbucket using the Gitbash command liner –  Apr 28 '20 at 11:08
  • Does this answer your question? [git error: failed to push some refs to remote](https://stackoverflow.com/questions/24114676/git-error-failed-to-push-some-refs-to-remote) or [Cannot pull/push my git repository anymore. "Merge conflicts"](https://stackoverflow.com/questions/31490017/cannot-pull-push-my-git-repository-anymore-merge-conflicts) or [git push rejected, merge conflicts, git pull --rebase](https://stackoverflow.com/questions/45205404/git-push-rejected-merge-conflicts-git-pull-rebase) – Melebius Apr 28 '20 at 11:27
  • no my question is very specific, none of your links do –  Apr 28 '20 at 11:32
  • 1
    You need to provide more details about how you came to have a detached head. I am missing also if you want to push a new branch or you want to push N commit on origin/master. – Lorenzo Isidori Apr 28 '20 at 14:43
  • push a new branch. and i said it's doesn't work and when i tried to push HEAD:branch it worked which i don't want –  Apr 28 '20 at 15:58

2 Answers2

1

It's relatively easy to do, but this is usually not the right way to work. Suppose you have done these, while on your own branch master:

git add -A
git commit -m "message of commit"

This created a new commit and you'd like to send it to the Git server over at origin without first creating a new local branch name newbranch; but you'd like that Git not to call this master, but rather to call this newbranch. You can just run:

git push origin master:newbranch

or:

git push origin HEAD:newbranch

The variant with HEAD works regardless of whether the current branch is named master.


The reason not to do this is simple enough: your branch name master (or whatever it may be) now corresponds to the server's newbranch and you will need to remember to use your local name origin/newbranch to work with your branch named master. Your branch named master is no longer a good way to deal with their branch named master.

In other words, you now need to keep, in your head, the mapping: "my newbranch is their master". Git certainly can be used this way, but eventually you are likely to forget that you are doing this.

If you first create your own branch name newbranch, like this:

git add -A
git status
# oops, I'm on `master`, and I didn't want that, so:
git checkout -b newbranch
git commit -m "message"

you can then:

git push -u origin HEAD

to send newbranch to origin using the name newbranch over there, so that your newbranch pushes to their newbranch which is your origin/newbranch, and now your newbranch has origin/newbranch set up as its upstream, and you don't have to remember anything special.

If you accidentally did commit on your master and wish to undo that, that's easy enough. For instance, suppose you did:

git add -A
git commit -m "message"

and only then realized: oops, I'm on my master, you can now do:

git branch newbranch
git reset --hard origin/master
git checkout newbranch

or equivalently (but less churn in your work-tree):

git checkout -b newbranch
git branch -f master origin/master

(this is also one command shorter, so it's better in two ways). You're now in a setup where you are on a new branch newbranch with your new commit on it, and your existing master matches your origin/master again. You can now git push -u origin HEAD to send the commit and create the new branch name newbranch on the other Git over at origin, as before.


Note that if you have run git pull and it has fetched from origin and run git merge—that's what git pull does; it runs git fetch followed by git merge—and you are in a conflicted state, you'll have to either finish or abort (terminate without finishing) the merge before you can proceed. The easiest thing is to abort the merge:

git merge --abort

since everything it did was automated and you can easily have Git just do that again later.

torek
  • 448,244
  • 59
  • 642
  • 775
  • The problem is that i don't want to push to HEAD (as that might create a discontinuity in the commits line) but rather just push to newly created branch (by its name) –  Apr 28 '20 at 16:26
  • 1
    @ites: you can't push *to* `HEAD` (each Git has its own separate HEAD, and theirs is not something you can set from your end with `git push`). When you run `git push`, your Git calls up the other Git (their Git), offers them some set of *commits* (by raw hash ID), lets them pick and choose which ones they need vs which they already have, and then at the end, asks them to set some *names* (usually branch and/or tag names) to specific raw hash IDs. When you use the `git push origin mybranch:theirbranch` syntax, they never even see the `mybranch` part. – torek Apr 28 '20 at 19:53
  • 1
    The same goes for `git push HEAD:theirbranch`: they never even *see* your branch name. If you `git push -u origin HEAD`, your Git translates your `HEAD` to your branch name, and then, since you didn't use `mine:theirs`, uses your *branch name* as the branch name your Git tells their Git to create-or-update. – torek Apr 28 '20 at 19:54
  • 1
    The linkages from commit to commit are part of the *commits*. All this stuff with branch names is just to enable your Git, and their Git, to *find* the commits. A `git push` sends the same commits, no matter how you do it, but the `:theirbranch` part tells your Git what name to tell their Git to send at the end. Leave it out, and your Git sends them the same branch name you're using—which is the least-confusing mode. – torek Apr 28 '20 at 19:56
0

When I use this command (without -u), it works.

git push origin <branch_name>

Another possible cause of the error is when <branch_name> does not match the actual name of the Git branch.

derekbaker783
  • 8,109
  • 4
  • 36
  • 50