同时提交多个分支

问题描述 投票:0回答:10

我想同时在不同的分支上进行一次提交,因为在我的项目中,我针对不同的客户端有不同的分支。

假设我已经在分支 A 上进行了新功能提交。我是否也可以同时在分支 B、分支 C 和 D 上进行此提交?有没有这方面的快捷命令?这样签出一个分支,每次都要cherrypick提交是非常麻烦的,有时候,如果很多分支都需要提交,那对我来说将是一场噩梦。

有任何简单的 bash 脚本可以执行此操作吗?

那里有一个类似的问题。但变基并不是我想要的。

git
10个回答
92
投票

cherry-pick 功能将完成这项工作,并且只会应用最后一次提交:

考虑分支 A、B

git checkout A
git commit -m "Fixed the bug x"
git checkout B
git cherry-pick A

希望这有帮助!


19
投票

由于我的问题没有典型的答案,我编写了一个简单的脚本来自动化此过程。请随意评论此代码。

#!/bin/bash
BRANCHES=(
master_branch
develop_branch
testing_branch
)
ORIGINALBRANCH=`git status | head -n1 | cut -c13-`
git commit -m $1
CHERRYCOMMIT=`git log -n1 | head -n1 | cut -c8-`
for BRANCH in "${BRANCHES[@]}";
do
    git stash;
    git checkout $BRANCH;
    git cherry-pick $CHERRYCOMMIT;
    git checkout $ORIGINALBRANCH;
    git stash pop;
done

15
投票

您可能想查看的内容:

git stash
您的更改、
git commit
git checkout
另一个分支、
git stash apply
git commit

请参阅 手册页


4
投票

我认为你可以编写一个提交后钩子来与其他分支合并或挑选。但这当然不会是一次提交。

Hook 会自动化你想要实现的目标。

http://git-scm.com/docs/githooks


3
投票

不,我不认为你能做到这一点。您最好的选择是提交到您的一个分支(或主分支),然后将提交一一合并到其他分支中,或者将提交挑选到每个其他分支中。


2
投票

也许这会对某些人有所帮助,

我使用了上面“Kit Ho”的方法,并将其添加到 .gitconfig 作为别名。

; commitall commits to the current branch as well the all the branches mentionedin BRANCHES array var as shown below.
commitall = "!f()  { \
    BRANCHES=( \
    branches1 \
    branches2 \
    );  \
    usage() \
    { \
        echo \"Usage: git commitall -m 'JIRA: BRANCHNAME:<comment to check in>'\"; \
        exit 1; \
    }; \
    OPTIND=1; \
    DFNs=\"\"; \
    while getopts \"h:m:\" opt; \
    do \
        case \"$opt\" in \
            h) \
            usage; \
            ;; \
            m)  export Comment=$OPTARG; \
            ;; \
        esac; \
    done; \
    ORIGINALBRANCH=`git symbolic-ref HEAD|cut -d/ -f3- `; \
    echo \"Current branch is $ORIGINALBRANCH \" ; \
    echo $Comment | grep \"^JIRA: $ORIGINALBRANCH:\"  > /dev/null 2>&1 ; \
    if [ $? -ne 0 ]; then \
        usage; \
    fi; \
    LOGMSG=`git log -1 --pretty=%B --grep=\"JIRA: $ORIGINALBRANCH:\" `; \
    MSG='commit first time in this branch is a success' ; \
    if [ \"$LOGMSG\" == \"\" ]; then \
        git commit -m \"$Comment\"; \
    else \
        git commit -a --amend -C HEAD; \
        MSG='commit with amend succeeded' ; \
    fi; \
    if [ $? -ne 0 ]; then \
        echo \"git commit failed!\"; \
        exit 1; \
    else \
        echo \"$MSG\" ; \
    fi; \
    CHERRYCOMMIT=`git log -n1 | head -n 1 | cut -c8- ` ; \
    if [ \"$CHERRYCOMMIT\" == \"\" ]; then \
        echo \"'git log -n1 | head -n 1 | cut -c8-' no commits for this branch\"; \
        exit 1; \
    fi; \
    echo \"found this commit -> $CHERRYCOMMIT for current branch\"; \
    stashes=`git stash list | grep \"WIP on $ORIGINALBRANCH\" ` ; \
    for BRANCH in \"${BRANCHES[@]}\"; do \
        if [ \"$stashes\" ]; then \
            git stash; \
        fi;\
        git checkout $BRANCH; \
        if [ $? -ne 0 ]; then \
            echo \"git checkout $BRANCH failed!\"; \
            exit 1; \
        fi; \
        git cherry-pick $CHERRYCOMMIT; \
        git checkout $ORIGINALBRANCH; \
        if [ \"$stashes\" ]; then \
            git stash pop; \
        fi; \
    done; \
    }; \
f"

0
投票

我认为没有办法做到这一点。也许您需要为其编写一个 bash 脚本或为不同的分支创建不同的存储库。


0
投票

您还可以使用一些 Python 来实现良好的自动化:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Populate specified or latest commit across all branches in the repository.

"""

import os
import re
import sys
import sh


git = sh.git.bake(_cwd=os.curdir)

if len(sys.argv) > 1:
    if '-h' in sys.argv or '--help' in sys.argv:
        print('Usage: git-populate.py [commit] [-h|--help]')
        sys.exit(0)
    else:
        commit = sys.argv[1]
else:
    # By default choose latest commit.
    git_log = git('--no-pager', 'log', '-n', '1', '--no-color').stdout
    commit = re.search(r'[a-f0-9]{40}', git_log).group()

print('Commit to be populated: {0:.6s}'.format(commit))

git_branch = git.branch('-a', '--no-color').stdout
source_branch = re.search(r'\*\s(\w+)$', git_branch, re.MULTILINE).group(1)
print('Source branch: {0}'.format(source_branch))

branches = re.findall(r'remotes/\w+/([\w-]+)$', git_branch, re.MULTILINE)
# Exclude current branch from target branches.
branches = [i for i in branches if i != source_branch]
print('Target branches: {0}'.format(branches))


print('Stashing local changes')
git_stash = git.stash().stdout

for branch in branches:
    print('Ading commit {0:.6s} to branch {1}'.format(commit, branch))
    git.checkout(branch)
    try:
        result = git('cherry-pick', commit)
    except sh.ErrorReturnCode_1:
        # Ignore diplicate cherry pick and discard changes.
        git.reset()


git.checkout(source_branch)
print('Return to branch {0}'.format(source_branch))

if not git_stash.startswith('No local changes to save'):
    print('Restoring local changes')
    git.stash.pop()

0
投票

这可能取决于您将如何推动您的提交。我的意思是,您总是有机会使用一个

git push
命令推入多个分支。

执行以下

.git/config
设置以确保
master
分支也始终被推送到
foobar
分支。

[remote "origin"]
        url = [email protected]:mgerhardy/example.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        push = refs/heads/master:refs/heads/master
        push = refs/heads/master:refs/heads/foobar 

但这里的问题可能是,如果这两个分支出现分歧,你就无法推送它们。您仍然可以根据您正在构建的分支重新组织客户端检测的代码。这样您就可以维护数十个客户并始终保持所有分支机构同步。


-1
投票

要同时推送到多个分支,只需安装和配置 SourceTree 并选择“推送”和您想要部署到的分支。

© www.soinside.com 2019 - 2024. All rights reserved.