0

Consider the following interaction I've just had with git:

$ git branch
* branch_A
  branch_B
  branch_C

$ git push
To git@thing.com:repo.git
 ! [rejected]        branch_B -> branch_B (non-fast-forward)
 ! [rejected]        branch_C -> branch_C (non-fast-forward)
error: failed to push some refs to 'git@thing.com:repo.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and merge the remote changes
hint: (e.g. 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I have not touched branches B or C since I last pulled them (though the remote has been updated). No one else has touched the remote branch A, which is my personal feature branch. Also, my local branch A does not have a divergent history; it's just one commit ahead of the remote branch A.

Why is this happening? How do I get around it?

  • 1
    Have you tried `git pull` first? – tymtam Mar 30 '21 at 22:44
  • As [tymtam suggested](https://stackoverflow.com/a/66879478/1256452), you almost certainly have your Git configured to send "matching" branch names. This was the default in some very ancient versions of Git (before Git 2.0). If you did not specifically set this up yourself, then, this means you're using a really old version of Git, and should probably upgrade. – torek Mar 31 '21 at 00:09

1 Answers1

1

It looks like your git is set up to push all local branches when you do a push. That's OK.

I'd take the situation step by step and approach it systematically.

Git tells us that there is something not right with braches B and C. If you don't care about them you can delete them, but a safter way is to check them and do a git pull.

Option A - pull B and C

If yod didn't change B or C then something like this should allow you to push A.

git checkout branch_B
git pull
git checkout branch_C
git pull
git checkout branch_A
git push

Option B - delete local B and C

git branch -D branch_B branch_C (please note capital D)
git push

If you need B and C after that you just check them out again.

tymtam
  • 31,798
  • 8
  • 86
  • 126
  • You can also just push branch A if you want ‘git push branch_A’ – D-Rock Mar 30 '21 at 23:07
  • It's kind of a hassle to have to checkout the different branches every time; I think @D-Rock's comment is helpful here. Is there a way to change the git configuration so it only pushes the current branch without having to specifically type it? – Marco Merlini Mar 31 '21 at 12:22
  • I use an alias setup in my zsh profile `alias gp=git push origin HEAD` – D-Rock Apr 01 '21 at 13:59
  • @MarcoMerlini have a look at [this answer](https://stackoverflow.com/a/948397/8559522). – Calum Halpin Apr 07 '21 at 14:22
  • @CalumHalpin This solved my problem perfectly. If you write it as an answer I'll accept it – Marco Merlini Apr 14 '21 at 11:34
  • @MarcoMerlini it's an answer to your question in these comments rather than your original question. Feel free to upvote the linked answer and question though. – Calum Halpin Apr 14 '21 at 15:32
  • @CalumHalpin I would be glad to, but I don't have enough rep for my votes to show up yet – Marco Merlini Apr 15 '21 at 16:08