I want to make my current branch exactly the same as my main branch. When I do 'git reset --hard main', it does the job. But then when I push it complains that it is behind.
error: failed to push some refs to 'https://gitlab.bol.io/sequoia/relevanc.git'
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.
I tried using logs but it doesn't work... just want to go back to the main branch... how to do this?
A branch in Git is merely a name for a commit. That's just about all it is. So the phrase "make my current branch exactly the same as my main branch" is ambiguous. What is it supposed to mean?
Let's say you have this:
When you say you want
mybranchto be the same asmain, you might mean one of two things (at least).Possibility 1
You might mean: "I don't care about Z. I don't care about Y. I don't care about X. I'm sorry I ever made them. I'm sorry about all the work I've done on
mybranch. I wantmybranchto start all over, emanating from wheremainis now."Then there is no need for all this violence (hard reset, force push, etc.). Just delete
mybranchaltogether! It serves no purpose:Now
mybranchis gone — both locally and on the remote. If you now want to start over, withmybranchemanating frommain, then start over: makemybranch:Result on the remote:
As you can see,
mainandmybranchare the same commit, and the separate history ofmybranchis effectively gone.Possibility 2
You might mean: I want to keep X and Y and Z but I want the state of my project in
mybranchto be identical to the state of my project inmain.Then you would reverse merge
mybranchintomainwith theoursstrategy, and then mergemainintomybranch:Result on the remote:
As you can see,
mainandmybranchnow point to the same commit, M, which is identical (in content) to D, which was the state ofmain; but the history has been preserved.