1

I would like to merge a couple of branches in my project but I don't know how to get Git to merge. I am doing my work on Linux Fedora 22.

I had the following Git structure on my project, where CAPITAL letters are branches and small letters are commits:

                    ATTINY
                     /
>- MASTER - a - b - c

Then, I checkout out a previously tagged version, did some editing and performed some commits:

>- TAG1.0 - e - f

That means: I was working in master, then I created branch ATTINY for safekeeping, then checked out an older (tagged) branch, did some work there.

Somewhere along the way (immediately?) I got a Detached HEAD. Oops.

So I made those commits become a branch, and checked it out. Then I finished my work, tagged it, and made a branch at that point called TAG2.0, then I updated master to point to it:

                    ATTINY
                     /
>- MASTER - a - b - c

>- TAG1.0 - e - f - OOPS (master) - g - h (...tag:2.0) - TAG2.0 (master)

Now I want to merge the work that I've done for branch TAG2.0 with the work that I left behind at branch ATTINY. How do I do it?

I tried to merge my TAG2.0 (master) branch with branch ATTINY but I got "Already up-to-date".

I did a git reset --hard ATTINY based on Git merge reports "Already up-to-date" though there is a difference but now it looks like I only have the ATTINY branch stuff. I don't have any of the changes I made in the TAG2.0 branch.

No amount of git merge seems to work, even though there are diff's between TAG2.0 and ATTINY branches.

Help me Obi-Wan. You're my only hope.

Community
  • 1
  • 1
Mike S
  • 1,235
  • 12
  • 19

3 Answers3

2

It looks like you managed to lost some commits by issuing

git reset --hard ATTINY

Reset moves current branch to some other commit (ATTINY in your case). If no other branch points to you current commit, you will loose it. Example:

a -> b -> c -> d (master)
     |
     \ -> x -> y -> x (ATTINY)

$ git checkout master
$ git reset --hard ATTINY

The result

a -> b -> c -> d 
     |
     \ -> x -> y -> x (master, ATTINY)

So you have no reference at the d commit and it will disappear

a -> b -> x -> y ->x (master, ATTINY)

But before the garbage collector physically removes it, you can get it back by creating a new branch pointing to that commit

$ git reflog
641f62e HEAD@{0}: ...
641f62e HEAD@{1}: ...
6fb53aa HEAD@{2}: ...
3aed47e HEAD@{3}: ...

Reflog shows you references, that were involved in the past operations. Select one or more potential candidates (from the top of the list) and create branches:

git branch test1 641f62e 
git branch test2 6fb53aa 

Then inspect the situation to see, if you get your lost commits back

gitk --all

Now you can put your master branch back using git reset --hard (with care). I hope, you will be able to restore all you commits with all this.

Now back to your question: As Collin Driscoll mentioned, it should be a matter of

git checkout master
git merge ATTINY

You may get "fast-forward" merge (where no merge commit get created). This is ok most of the time. But if you really want to create the merge commit, then do

git checkout master
git merge ATTINY --no-ff
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
  • Thanks for the reply. I'm not worried about losing commits, because I created branches at all the important spots (so your second graphic just below "the result" is not accurate; I have a reference to commit d). I guess my question boils down to this: between branches, `git diff` shows differences. Why am I not able to merge the branches? How can `git merge` report "Already up-to-date" when there are clearly differences between branches? – Mike S Sep 04 '15 at 15:03
  • Did you tried `--no-ff` switch? This should work as expected creating a new merge commit. – Boris Brodski Sep 04 '15 at 15:11
  • It says: "Already up-to-date." I tried to check out my ATTINY branch, then merge with the master, and it looks like it wipes out the lines in ATTINY that I made since TAG1.0 and the file is made to be simply TAG2.0 (== master). I presume that it looks at the situation and says, "Well since TAG1.0 you made updates in the TAG2.0 branch that are much more recent than ATTINY so, Mr. ATTINY, you lose." That's how it looks to me anyway. I thought a merge was supposed to be a combining of the two branches, but something else is going on. – Mike S Sep 04 '15 at 15:16
  • Technically, am I supposed to be able to: From branch A, create branch B, do some work on it, then check out branch A again, do some work, call it branch C, then merge B and C so that both of their changes are incorporated in my file? Does git have that capability? Maybe it sounds like a dumb question but I'm dumbfounded that I can't seem to merge them the way I expect. – Mike S Sep 04 '15 at 15:22
0

Should be a simple matter of:

git checkout master
git merge ATTINY

If that's not working, I'd recommend checking out SourceTree. Your repo structure might be a bit goobed up.

CollinD
  • 7,304
  • 2
  • 22
  • 45
  • Thanks for the reply but as I mentioned 'I tried to merge my TAG2.0 (master) branch with branch ATTINY but I got "Already up-to-date".' Something is goobed up, but git reflog, gitg, and gitk all show me pictures that I understand. `git diff` concurs with my expectations. It's just that `git merge` won't "merge changes made in one branch with those made in another" (...from http://git-scm.com/docs/git-merge). – Mike S Sep 04 '15 at 15:11
0

It appears as though git merge was no help. Since I mostly only changed a single file, I checked out the old ATTINY branch that I wanted to merge in, copied the file to /tmp, then checked out master, then edited the file to incorporate the changes from the old branch.

I don't know why I couldn't merge :-( .

Mike S
  • 1,235
  • 12
  • 19
  • That's a very strange thing. If two branches point to two different commits, you must be able to merge those branches with `git merge --no-ff ...`. – Boris Brodski Sep 09 '15 at 09:56