automatic git merge

Go To StackoverFlow.com

1

My use case is this...

I have a project that has two production branches. Most changes in any branch need to be merged into the other branch. It is only very specific and infrequent changes that need to be kept separate in the two branches.

What I want to do is to set up an automatic merge between two branches on the main remote repository that is used as the source for the production build machines so that anyone who pushes something into branch A automatically merges it into branch B and handling a special not-to-be-merged push (which happens infrequently) manually.

Any easy way to do this?

2012-04-04 22:01
by owagh


2

You could use a post-receive git hook. When someone pushes to the main remote repository, a script is invoked (the hook) where you get various sorts of information, what ref what pushed, preview commit hash and new commit hash.

Here you can determine exactly what branch was pushed. Given this information you could in another repository checkout the other branch, do the merge and push up this information.

There is a post-receive.sample file in your .git/hooks/ folder. Rename it to post-receive and make sure it's executable. In bash it could look like:

#!/bin/sh
# <oldrev> <newrev> <refname>
while read oldrev newrev ref
do
    echo "$oldrev $newrev $ref"
done
2012-04-04 23:05
by ralphtheninja
Hey I'm not sure why you say we should abort the pushes... Is there some reason for doing so or is it because my question is ambiguous? Most of the work that developers working on branch A and branch B should be combined. The only big difference is that they link to different third party libraries and so there are minor differences in the Makefiles and headers and those differences are by and large constant - owagh 2012-04-05 15:43
But yeah the post-recieve hook seems to be the way to do this. Can you add some more links to resources in your answer about post-recieve scripts? The only example in my .git/hooks folder is an email script that doesn't really show how I can do a merge from within the post-recieve script - owagh 2012-04-05 15:45
You can use my script as is. Assuming you have access to bash - ralphtheninja 2012-04-05 15:59
Scratch my comment about aborting etc. Just let the script check if it's A or B being pushed and do the appropriate merge. When the script tries to push it will only cause trigger the hook again if there was something to push. So it will terminate nicely - ralphtheninja 2012-04-05 16:14
http://book.git-scm.com/5githooks.htm - ralphtheninja 2012-04-05 16:15
Ads