1

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:

enter image description here

Community
  • 1
  • 1
Nezam
  • 4,122
  • 3
  • 32
  • 49
  • I would pull and merge into the real (server) branch. Then move the local head to point to the server/origin head. This way nothing is 'forced'. Git is like a tree, not a highway. – user2864740 Aug 01 '15 at 08:44
  • Please give me a working solution in commands instead of directions. I want to come out of this mess!! – Nezam Aug 01 '15 at 08:48
  • _Re: Edit about apparently lost changes:_ Hu? In your second Screenshot, I can still see the same revisions, just the local `Head` branch is gone. (Have you deleted it although the merge was aborted due to missing commit message?) `git checkout -b Head origin/Head` should restore your local `Head` branch (because the remote `Head` branch is still pointing at the right revision `09298ea`, as far as I can see from your screenshot). – das-g Aug 01 '15 at 11:31

2 Answers2

2

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The branch "Head" is there in the remote repo. Do i need to rename in the remote repo too ? Can you make the commands bit more specific to the screenshot i provided. Thanks,i am noob when it comes to git. – Nezam Aug 01 '15 at 09:05
  • @Nezam those commands are specific to your screenshot. It won't rename the remote branch though. For that: http://stackoverflow.com/a/4754132/6309 – VonC Aug 01 '15 at 09:06
  • @Nezam yes (if you have renamed your branch first): you cherry-pick from the oldest to the newest. – VonC Aug 01 '15 at 10:06
  • 1
    @Nezam If anything "vanishes", type "git reflog", and look for commit 09298ea: do a `git checkout -B Tip 09298ea` and your branch gets back where it was initially. – VonC Aug 01 '15 at 10:19
  • Thanks!! Ok now i have a new branch called `Tip` and it has the updated code. How to push this up to the remote `master` now. So that the remove master has only single branch and this updated code? – Nezam Aug 01 '15 at 10:23
  • 1
    @Nezam Before pushing anywhere, did you replay your Tip commit on top of your local master branch as I mention in the answer? You will push only when the situation is ok locally. – VonC Aug 01 '15 at 10:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84879/discussion-between-nezam-and-vonc). – Nezam Aug 01 '15 at 10:55
2

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
    
Community
  • 1
  • 1
das-g
  • 9,718
  • 4
  • 38
  • 80
  • That is looking quite detailed, a good alternative to my answer. +1 – VonC Aug 01 '15 at 09:21
  • when i execute `git merge --strategy ours master` i get to a vim. How to properly type commit msg and go ahead? – Nezam Aug 01 '15 at 09:52
  • when i execute `git merge --strategy ours master` i get to a vim. I pressed `ZZ` and i got a msg `error: There was a problem with the editor 'vi'. Not committing merge; use 'git commit' to complete the merge.` So i executed `git commit -m "Merge"` and proceeded but i still see that the remote has code from the earlier branch and not this current branch. Only different is `Head` is deleted. – Nezam Aug 01 '15 at 10:07
  • I have no idea what `ZZ` does in vim. For merges, I'm usually happy with the message that git already populated the editor with, so I just type `:x[ENTER]`, which is short for `:wq[ENTER]` (or `:w[ENTER]` followed by `:q[ENTER]`), i.e. write (=save) and quit (=close the editor). – das-g Aug 01 '15 at 11:24