0

I've downgraded django and built a large feature in my latest branch (personal project). Github wants me to resolve conflicts before merging. I tried to follow the instructions but stuck in an infinite loop.

Their instructions:

Step 1: From your project repository, bring in the changes and test.
a)git fetch origin 
b)git checkout -b foobranch origin/foobranch 
c)git merge main

Step 2: Merge the changes and update on GitHub.
a)git checkout main
b)git merge --no-ff foobranch 
c)git push origin main

I've done all that, and at the end, it rejects me with error:

Updates were rejected because the tip of your current branch is behind

'git pull ...') before pushing again

I did "git pull" and all the conflicts come back. Back to square one...

I tried to use Github Desktop, it shows the diff side-by-side, but there's no way to choose the version I want and push/merge.

Please help, how can I push my latest branch on Github?

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
kaka
  • 83
  • 7

1 Answers1

2

There is likely an error in the instructions which assumes your local copy of main is up to date.

To fix it:

  1. First delete your local copy of main and foobranch so you can start over fresh.
  2. Now change step 1c to read git merge origin/main.

Why this works:

Your copy of main was out of date. Anytime you want to use main but you don't need it checked out, you should use origin/main instead. When you do need it, deleting it and checking it out again will make it up to date with origin/main which you just fetched in step 1a. Note you could checkout main and then pull, which will also update it, but if you have done any merge to it on your own this will create a merge commit which you usually don't want.

Caveat: depending on how long it takes you to complete the instructions and how fast commits appear on main, it's possible that by the time you get to step 2c you'll be out of date again. If that happens, you could just start over if you wish and do it faster next time. You could even script it so it only takes seconds to run.

Tip: consider deleting your local copy of main as soon as you're done with it. This way you'll never have an outdated copy of it. The next time you check it out it will be identical to origin/main.

Side Note: suggesting that you delete your local copy of main and foobranch is assuming you have no new commits on those branches that you need to keep, and that seems to be true from the question. That being said, if you delete them, and later discover you had something you wish to get back, you can use git reflog to help you find those commits.

TTT
  • 22,611
  • 8
  • 63
  • 69
  • Thanks @TTT for the detailed answer. I got it sorted before I saw your message. I took the risk and force pushed it according to https://stackoverflow.com/questions/39389094/git-how-to-perform-hard-push#39389342. Your answer makes sense and seems more proper, I'll mark it as correct. – kaka Aug 24 '21 at 22:46
  • 1
    @kaka Be careful with force push. In general, you rarely want to force push shared branches such as `main`. (Oftentimes `main` would be protected to prevent it too.) – TTT Aug 25 '21 at 05:06