Version 2 (modified by damoxc, 14 years ago) (diff)

--

In addition to the steps outlined by GitTips we have put together a wrapper script around git-commit that can be used to commit to multiple branches. Simply save this script as git-commit somewhere on your PATH.

You can use it like so:

$ git-commit -b some_branch -m "some commit message"

git-commit

#!/usr/bin/python
#
# Copyright (C) 2010 Damien Churchill
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.    If not, write to:
#   The Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor
#   Boston, MA    02110-1301, USA.

import sys
import subprocess

GIT              = '/usr/bin/git'
GIT_BRANCH       = '/usr/lib/git-core/git-branch'
GIT_COMMIT       = '/usr/lib/git-core/git-commit'
GIT_CHECKOUT     = '/usr/lib/git-core/git-checkout'
GIT_FORMAT_PATCH = '/usr/lib/git-core/git-format-patch'

if __name__ == '__main__':

    branches = []
    git_args = []

    args = sys.argv[1:]

    while args:
        arg = args.pop(0)
        if arg in ('-b', '--branch'):
            branches.append(args.pop(0))
        elif arg.startswith('--branch='):
            branches.append(arg[9:])
        else:
            git_args.append(arg)

    # Find out the current branch
    current_branch = None
    local_branches = []
    p = subprocess.Popen([GIT_BRANCH], stdout=subprocess.PIPE)
    for line in p.stdout:
        branch = line[2:].strip()
        local_branches.append(branch)
        if line[0] != '*':
            continue
        current_branch = branch

    # Check to make sure that the provided branches are valid
    for branch in branches:
        if branch not in local_branches:
            print >> sys.stderr, 'invalid branch: %s' % branch
            sys.exit(1)

    # Perform the main commit
    subprocess.call([GIT_COMMIT] + git_args)

    # Loop through the specified branches applying the commit to all of
    # them.
    for branch in branches:
        subprocess.call([GIT_CHECKOUT, branch])
        p1 = subprocess.Popen([GIT_FORMAT_PATCH, '-k', '--stdout', '-1',
            current_branch], stdout=subprocess.PIPE)
        p2 = subprocess.Popen([GIT, 'am', '-3', '-k'], stdin=p1.stdout)
        p2.communicate()

    # Switch back to the original branch
    subprocess.call([GIT_CHECKOUT, current_branch])