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 ?

Thanks

SourceTree

EDIT

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!!!

Screenshot:

在这里输入图像描述


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:

  • 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
    
  • The actual merge

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

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

    git branch -D Head
    
  • 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
    
    链接地址: http://www.djcxy.com/p/90394.html

    上一篇: 如何通过命令行更改Gerrit的主题名称

    下一篇: 强制如何将我本地的所有内容合并到远程主机中?