5

I created a new branch and made changes to the code and attempted to push

git add . git checkout -b newbranch git commit -m "commit" git push origin newbranch 

I get error : failed to push some refs .. updates were rejected because the tip of your current branch is behind its remote counterpart..

if I do git pull or git pull --rebase, I get

there is no tracking information for the current branch. Please specify which branch you want to merge with 

if I try git rebase origin/main and git pull origin main --rebase, it says

current branch newbranch is up to date 

if I try git pull origin/main and git pull main, it says

fatal: origin/main does not appear to be a git repository 

if I try git pull origin main, it says

fatal: not possible to fast-forward, aborting 

can anyone help?

2
  • 1
    Consider git pull origin newbranch. Commented Dec 10, 2021 at 4:28
  • 2
    I don't know why this got so many downvotes. The scenario is not that surprising a mistake to make. Upvoting both the question and TTT's excellent answer. Commented Dec 10, 2021 at 14:03

1 Answer 1

9

The title error message is likely because you created a branch with the same name as one that already exists on the remote. So when you attempt to push it Git is informing you that there are commits on the remote tracking branch with that name that you do not have yet.

You can confirm by listing all the remote branches with git branch -r.

If that's the problem there are lots of things you can do to solve this. Some of them are:

  1. Rename your branch to something else. Then when you push it you won't conflict with an already existing branch.
  2. If you feel your branch should get the new commits from the remote branch, then perform git pull origin newbranch to merge them in (or use git pull --rebase origin newbranch if you don't want to create a merge commit for this). After that you'll be able to cleanly push.
  3. If your intent was to blow those commits away on the remote branch, then you could do a force push, with git push --force-with-lease. If that still failed then it means there are commits on the remote repo that you haven't even fetched yet, so you would need to git fetch (and probably look at the commits to make sure you don't want them) and then perform the force push again.

I'm not sure which option is best for you, but I definitely would not do #3 unless you know for sure you don't need what's on the remote branch of the same name. If you aren't sure which is best I'd lean towards option #1, for now.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the branch name is same causes the problem on my side, thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.