Can't push local changes to an existing remote branch
Asked Answered
T

2

7

There is a remote branch called "my-remote" that I had previously pushed to with no problem. As of today, I can't push and I get different errors.

First error I got was:

hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you did not intend to push that branch, you may want to
hint: specify branches to push or set the 'push.default' configuration
hint: variable to 'current' or 'upstream' to push only the current branch.

I did some research and ran this hoping it would solve the problem:

git config push.default tracking

after running that I ran the push again:

git push https://github.com/someurl/mybranch.git

I got the following error:

pushing to remote 'https://github.com/someurl/mybranch.git', which is not the upstream of
your current branch 'my-remote', without telling me what to push
to update which remote branch.

I have tried running this:

git push master:my-remote https://github.com/someurl/mybranch.git

but it tells me that:

fatal: remote part of refspec is not a valid name in https://github.com/someurl/mybranch.git
Tarnopol answered 9/9, 2012 at 18:0 Comment(0)
C
8

If you want to push your master branch on the my-remote remote branch, the correct syntax would be:

 git push https://github.com/someurl/mybranch.git master:my-remote 

(first: remote repo reference, the refspec, from git push man page)

Regarding your first error message, if it really didn't tell you to merge, then a git pull --rebase might have been needed.
Or at least a:

 git config --global push.default current

(As mentioned in "Configure Git to Only Push Current Branch").

If you are in the local branch 'my-remote' (which is the case, according to the command), you can make sure the upstream branch is set by making a:

git push -u https://github.com/someurl/mybranch.git my-remote:my-remote
Comedienne answered 9/9, 2012 at 19:8 Comment(8)
Thanks, I ran this but I still get this error: error: failed to push some refs to 'github.com/newcolabs/relay_contractors.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. I'm pretty sure nobody edits this branch since the last time I committed. I can't see any new commits on github. How do I merge?Tarnopol
@Tarnopol I have made 2 other edits to that answers, including a git pull --rebase option that should put your local branch on top of the remote one.Comedienne
when I run git pull --rebase it tell me that the branch is up to dateTarnopol
@Tarnopol what does git branch -a returns (in other words, what are your current local and remote branches?). A bit like in https://mcmap.net/q/13478/-how-to-merge-remote-changes-at-githubComedienne
running git branch -a shows me a list of all remote branches with a star next to my-remote.Tarnopol
@Tarnopol then what a git push -u https://github.com/someurl/mybranch.git my-remote:my-remote says? (in order to explicitly set the upstream branch, as in #5698250)Comedienne
this is embarrassing but somewhere along the line, looks like my changes where committed when it gave me an error. (I just saw on github) the last command you asked me to run said: everything up to date.Tarnopol
@Tarnopol Excellent. I have included the last command in the answer for more visibility.Comedienne
C
0

There are 9 steps to pushing to an existing git repo.

I tried "git push --set-upstream origin master" and got the following error:

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push --set-upstream origin master
Password for 'https://[email protected]':
To https://[email protected]/User/app.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://[email protected]/User/
app.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I then tried "git pull" and got the lastest changes:

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git pull
Password for 'https://[email protected]':
warning: no common commits
remote: Counting objects: 344, done.
remote: Compressing objects: 100% (275/275), done.
remote: Total 344 (delta 45), reused 336 (delta 41)
Receiving objects: 100% (344/344), 15.91 MiB | 43.00 KiB/s, done.
Resolving deltas: 100% (45/45), done.
From https://bitbucket.org/User/app
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> master

I did a "git push" and the changes failed:

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://[email protected]':
To https://[email protected]/User/app.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/User/
app.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

"git add -A" and "git commit" did not work because there was nothing to commit.

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git add -A

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git commit

On branch master nothing to commit, working directory clean

"git branch --set-upstream-to=origin/master master" seemed to do the trick.

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.

However, a "git push origin master" didn't work because the tip of the current branch is behind it remote counterpart.

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://[email protected]':
To https://[email protected]/User/app.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/User/
app.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

A "git pull" was needed to merge the repo's.

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git pull
Password for 'https://[email protected]':
Merge made by the 'recursive' strategy.

After executing the "git pull", "git push origin master" was exactly what was needed to get the sync commanded working in Visual Studio git plugin.

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://[email protected]':
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 539 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To https://[email protected]/User/app.git
   40d72a2..9748b8b  master -> master

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>
Chimpanzee answered 9/8, 2014 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.