2011-08-31 831 views
13

我想在同一時間在不同的分支上進行一次提交,因爲在我的項目中,我爲不同的客戶端有不同的分支。Git:同時提交多個分支

說,我在分支A上做了一個新的功能提交。我也可以同時在分支B,分支C和D上進行此提交嗎?有沒有捷徑命令?這對於簽出一個分支來說是非常麻煩的,並且每次有時候,如果許多分支都需要提交,那麼就會挑選提交,這對我來說將是一場噩夢。

這個操作的任何簡單的bash腳本?

那裏有一個similar question。但重新貸款並不是我想要的。

+0

的[提交到多個分支在同一時間與GIT](可能重複http://stackoverflow.com/questions/4532063/commit-to-multiple-branches-at-the-same-time-與git) – Johann

+0

參見http://stackoverflow.com/a/30186843/6309 – VonC

+0

如果Spoutnik的答案幫助你,考慮接受它與綠色複選標記 –

回答

0

不,我不認爲你可以這樣做。你最好的選擇是提交你的一個分支(或一個主分支),然後或者將提交合併到其他分支中,或者將提交合併到每個其他分支中。

+0

我不想合併,因爲我不想使所有的分支都是一樣的。櫻桃挑選是一種選擇,但它需要時間,需要結賬到每個分支,如果我有超過5個分支要做,它需要時間......嗯,你認爲製作一個腳本可以做到嗎? –

+0

腳本應該是相當簡單的。同時檢查@ rangalo的答案。 – harald

2

我認爲你可以編寫一個post-commit鉤子來合併或櫻桃採摘與其他分支。但它當然不會是一個單一的承諾。

鉤子會自動化你想要達到的目標。

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

7

有些事情,你可能想看看:git stash更改,git commitgit checkout另一個分支,git stash applygit commit

man pages

+1

這個方案可以工作,但你希望'git stash apply',而不是'git stash pop'。 – wjl

+0

@wjl:感謝您的糾正 - 我本應該指導Kit Ho來閱讀手冊頁;-) –

9

由於我的問題沒有典型的答案,我寫了一個簡單的腳本來自動執行此過程。隨意評論這段代碼。

#!/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 
+0

爲什麼你不接受你的答案作爲解決方案呢? – Sampath

0

我不認爲有辦法。也許你需要爲它寫一個bash腳本,或者爲不同的分支做一個不同的回購。

33

的摘櫻桃功能將做的工作,僅適用最後一次提交:

考慮臂A,B

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

希望這有助於!

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 

但這裏的問題可能是,如果這兩個分支分歧,你不能推他們。您仍然可以重新組織您的代碼,您的客戶端檢測基於您正在構建的分支。這樣你就可以維護數十個客戶端,並始終保持所有分支機構同步。

0

也許這將幫助一些人,

我用「包浩」 S上面的方法,並增加它的.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"