Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why after merge does GIT say "Already up-to-date", but differences between branches still exist?

I was originally working in 'newfeature' branch and I was called to fix a bug on the live branch urgently. I created a branch for that called 'generalmaintenance', did the job and then switched to develop and merged it in. I now want to return to 'newfeature' branch and to merge in the changes that I merged into it earlier.

When I switched to 'newfeature' and merged in 'develop', there were conflicts in 3 files.

I got in a tangle resolving the conflicts and eventually decided to use the "Revert" command in the 'Team' menu of Aptana Studio 3 (which is my IDE). I expected this to roll me back to before the merge, which it appears to have done.

Anyway, when I merge in 'develop' again, it says, Already-up-to-date, but when comparing files between the two branches, they are very different and the changes I added in the other branch are not being merged in.

How will I merge the two branches now please?

like image 358
Peter White Avatar asked Apr 01 '13 01:04

Peter White


People also ask

Does a branch still exist after merging?

When you're done with a branch and it has been merged into master, delete it. A new branch can be made off of the most recent commit on the master branch. Also, while it is ok to hang onto branches after you've merged them into the master they will begin to pile up.

Does git merge update both branches?

No, merging does only affect one branch.

How do you check if a branch is already merged with any branch or not?

You can use the git merge-base command to find the latest common commit between the two branches. If that commit is the same as your branch head, then the branch has been completely merged.

Does git merge change history?

A merge takes two or more branches, and creates a commit that interleaves the changes in all parents (resolving conflicts, as needed; the user has to decide how). git does not change history by itself.


2 Answers

Reverting Merges vs. Resetting Merges

My guess is that you actually are Already-up-to-date.

The problem is that git revert doesn't undo the merge, it only undoes the changes that the merge brought with it. When you create a merge commit, you're combining the commit histories of those two branches.

Merging

     develop         | A---B---C  \       \   E---F---M           |       newfeature 

In the case above, develop is merged into newfeature, creating the M commit. If you were to run git log newfeature you would see all the commits from both branches, however from the perspective of the newfeature branch, all those changes were performed by the M commit.

Reverting

The git revert command does not remove any commits, instead it creates a new commit that undoes the changes that the commit contained. For example if you had a commit containing this diff...

-This is the old sentence. +This is the new sentence. 

Then reverted this, the revert command would create a new commit that just preformed the opposite diff, it simply flips the signs.

-This is the new sentence. +This is the old sentence. 

This is really useful for undoing damage caused by commits that other developers already have. It moves history forward rather than changing history.

Reverting Merges

However, in the context of a non-fastforward merge it may have an undesired effect.

     develop         | A---B---C  \       \   E---F---M---W               |          newfeature 

Assuming W is a reversion commit, you can see how running git log newfeature will still include all the commits from the develop branch. As a result, additional merges from develop will no work, because it doesn't see anything missing from your branch.

Using git reset instead of revert.

In the future, you might want to consider using git reset --hard <ref> (where <ref> is the commit hash of the merge) to undo a merge if that merge has not been shared with other developers. In the example above, after having created merge commit M, running the command git reset --hard F would result in the following.

     develop         | A---B---C  \       \   E---F---M       |   newfeature 

As you can see this technique doesn't obliterate the commit as some people tend to think, it simply moves your branch back to the commit you selected. Now if you ran git log newfeature you would only get commit F, E, and A. Now the merge is actually gone from your branches history, so a later attempts to re-merge in develop will cause no problems.

This method is not without its complications. Realize that you are now modifying history, so if the newfeature branch was pushed to a remote branch after the M merge was made, then git is going to think you are simply out of date and tell you that you need to run git pull. If its just you working on that remote branch, then feel free to force-push - git push -f <remote> <branch>. This will have the same effect of the reset but on the remote branch.

If this branch is being used by multiple developers, who would have by now already pulled from it - then this is a bad idea. This is the very reason git revert is useful, because it undoes changes without changing the actual history.

Using reset on history is really only on option for commits that have not been shared.

The solution - reverting the reversion.

If the merge commit has already been shared, then the best approach is probably to use git revert on that merge. However as we said before, you can not then simply merge the branch back in and expect all the changes from that branch to re-appear. The answer is to revert the revert commit.

Lets say you did some work on the develop branch after having revered the merge in newfeature. Your history would look something like this.

         develop             | A---B---C---D  \       \   E---F---M---W               |          newfeature 

If you merge develop into newfeature now, you would only get D because its the only commit that is not already part of the history of the newfeature branch. What you also need to do is revert that W commit - git revert W should do the trick followed by git merge develop.

                 develop                     | A---B---C-----------D  \       \           \   E---F---M---W---M---G                       |                  newfeature 

This restores all the changes made by the original merge commit - which were actually made by C and B but were reverted in W, it then brings in D via a new merge commit G I would recommend reverting the revert before merging in the recent changes to develop, I suspect doing it in that order will have a lower chance of triggering conflicts.

TL;DR

Reverting creates a 'revert commit'. When undoing a revert, you need to run the revert command on the revert commit that was created when you reverted the first time. It should be easy enough to find, git tends to auto-comment on reverts so that they start with the word "Reverted".

git revert <commit>

like image 182
eddiemoya Avatar answered Sep 19 '22 09:09

eddiemoya


Found a hacky solution. But it works.

Whatever eddiemoya has answered is totally helpful. Thanks a lot for explanation. I encountered the similar situation. Where, I was able to see a lot of content in git diff <branch> but git merge was saying already up to date.

And I was not able to find the exact revert commit due to lot of reverts in the log. (Yeah bad thing. Shouldn't have happened in the first place)

Solution

git checkout branchX -- . 

This will stage all the changes from branchX to my current branch. Use on of your favorite git client, unstage and revert whatever is not intended.

Make a new commit and be happy :)

like image 33
Katti Avatar answered Sep 19 '22 09:09

Katti