How do force merge everything i have locally into the remote master?

The issue is that a team member had by mistake created a separate branch (main branch being master) called “Head”. “Head” has the latest code which i now want to over-write to the master (since it is up-to-date with all the code).

When i try to perform “Merge Head into master” i get an error saying “unrelated branches”. I searched on so and found out that i would need to pull the branch (“Head”) locally and then push (or force-push) it to the remote “master” branch to merge the code.

I used the following command:
git fetch && git checkout Head and checked it out (cloned it) to local. Now when i try to push it shows that i am 17 commits behind and 3 changes to push.

How do force merge everything i have locally into the remote master ?




I did what das-g told but it seems that the only change i see in remote is that the Head branch is deleted. The code changes which were originally there in Head has vanished (or lost!!!). I can see the tags and history but it seems the changes are deleted forever!!!


enter image description here

  • Cannot edit code in netbeans when managing merge conflict in a file
  • What happens to a branch once I merge it?
  • Tortoise SVN - Merge Trunk into Branch - Tree Conflict
  • Use git rebase because is easier to revert changes?
  • not sure what to use rebase or merge?
  • Git revert/cherry-pick to a commit beyond a merge
  • Disable “Merge” button in PR until certain conditions are met? (BitBucket Cloud)
  • Resolving Git conflicts in SourceTree
  • 2 Solutions collect form web for “How do force merge everything i have locally into the remote master?”

    It seems from your screenshot and from error message that master and Head do not have any common ancestors. If I understood you correctly,

    • you want the tip of master to have the current content of Head (and exactly that content, not Head‘s content merged with the content of master),
    • but you want to have both branch’s history preserved in master‘s ancestry. (Thus git checkout master; git reset --hard Head isn’t an option.)

    You can either rebase the Head commits on top of master, as VonC suggests, to produce a linear history, at the price that Head‘s commits will be re-written.

    If you want to preserve Head‘s commits exactly as they are, you can do a somewhat obscure merge:

    1. First, make sure your local master is up-to-date (from your screenshot, it doesn’t seem to be)

      git checkout master
      git merge --ff-only origin/master
    2. The actual merge

      git checkout Head
      git merge --strategy ours master
    3. Get the merge commit into master

      git checkout master
      git merge --ff-only Head
    4. You’ll probably want to get rid of the strangely named Head branch, now

      git branch -D Head
    5. and to get everything (including the branch deletion) onto the remote repo

      git push origin master :Head # should not need a force-push as no commits are discarded on the remote site

    First, do not call a branch “Head”: that is very close from HEAD, which references the “current commit”.

    You can rename it easily: git branch -m Head Tip

    Regarding the unrelate branch situation, you can try and cherry-pick it on top of master:

    git checkout master
    git checkout -b tmp
    git cherry-pick Tip~2 Tip~1 Tip
    git branch -f Tip tmp
    git checkout Tip

    (the rebase --onto option is a bit more complex)

    Once everything looks ok locally, you updtate the new branch name on the remote repo:

    git push origin --set-upstream Tip
    git push origin :Head
    Git Baby is a git and github fan, let's start git clone.