2

I'm working on an engineering project with a colleague and we wanted to try out two different routes. I decided to use Git branches so we could split the code and I could try my own way of solving the problem, he could try his own and finally we could compare which worked better. So I created two branches, let's say, "Francesco" and "Marco". Unfortunately, I did a mistake: I committed a change I intended for my own branch on the master branch and pushed it to the remote before realising I had messed up. I didn't despair though and reverted the last commit using:

$ git reset --soft HEAD~
$ git checkout Francesco

So far so good. Now the situation looks something like this, with branch "Francesco" beyond master by one commit:

73ef6b6 (HEAD -> Francesco, origin/master, origin/HEAD) some commit message
ae09794 (master, Marco) Some other commit message

The problem I face now is that the origin/master is still one commit beyond the master on my local machine. I want to make the situation in the remote repository on GitHub look exactly like the situation on my local folder. I try:

$ git checkout master
$ git push

As a response for my first command I get:

Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

(Which of course is exactly the opposite of what i want to achieve). An then from the second one I get:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/Francesco-Ghizzo/Idrodinamica'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Git is suggesting me again to overwrite my local folder with the content of the remote repository by pulling it, which is the opposite of what I intend to do. At this point, as a desperate move, I try:

$ git push -f

Everything now seems to work fine:

$ git status
On branch master
Your branch is up to date with 'origin/master'.
$ git log --oneline
73ef6b6 (HEAD -> Francesco) some commit message
ae09794 (origin/master, origin/HEAD, master, Marco) some other commit message

until I try to push committed changes on my own branch. I tried to set up my branch in the remote repository by typing:

$ git push --set-upstream origin Francesco

But then, whenever I try to commit a change, I get the message:

On branch Francesco
Your branch is based on 'origin/Francesco', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

even if I have already typed

$ git branch --unset-upstream

many times. Did I mess up something when forcing the push? How can I stop git sending me this message every time?

Francesco Ghizzo
  • 57
  • 1
  • 1
  • 6

1 Answers1

0

It seems that there is no remote branch called 'Francesco' anymore.

If the branch doesn't exist in your repository, you can create it again (with the commits of your local branch) with the command:

git push --set-upstream origin Francesco

If that's not the case, (if the remote branch exists) you can tell your local branch to follow it with the command:

git branch --set-upstream-to=origin/Francesco

I think that this issue is related with Why call git branch --unset-upstream to fixup?

The answer given in the issue linked is pretty well explained and you will probably have all the elements of information to understand and fix your problem.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Maxime Dezette
  • 126
  • 1
  • 5