我想在同一时间在不同的分支上进行一次提交,因为在我的项目中,我为不同的客户端有不同的分支。Git:同时提交多个分支
说,我在分支A上做了一个新的功能提交。我也可以同时在分支B,分支C和D上进行此提交吗?有没有捷径命令?这对于签出一个分支来说是非常麻烦的,并且每次有时候,如果许多分支都需要提交,那么就会挑选提交,这对我来说将是一场噩梦。
这个操作的任何简单的bash脚本?
那里有一个similar question。但重新贷款并不是我想要的。
我想在同一时间在不同的分支上进行一次提交,因为在我的项目中,我为不同的客户端有不同的分支。Git:同时提交多个分支
说,我在分支A上做了一个新的功能提交。我也可以同时在分支B,分支C和D上进行此提交吗?有没有捷径命令?这对于签出一个分支来说是非常麻烦的,并且每次有时候,如果许多分支都需要提交,那么就会挑选提交,这对我来说将是一场噩梦。
这个操作的任何简单的bash脚本?
那里有一个similar question。但重新贷款并不是我想要的。
不,我不认为你可以这样做。你最好的选择是提交你的一个分支(或一个主分支),然后或者将提交合并到其他分支中,或者将提交合并到每个其他分支中。
我不想合并,因为我不想使所有的分支都是一样的。樱桃挑选是一种选择,但它需要时间,需要结账到每个分支,如果我有超过5个分支要做,它需要时间......嗯,你认为制作一个脚本可以做到吗? –
脚本应该是相当简单的。同时检查@ rangalo的答案。 – harald
我认为你可以编写一个post-commit钩子来合并或樱桃采摘与其他分支。但它当然不会是一个单一的承诺。
钩子会自动化你想要达到的目标。
由于我的问题没有典型的答案,我写了一个简单的脚本来自动执行此过程。随意评论这段代码。
#!/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
为什么你不接受你的答案作为解决方案呢? – Sampath
我不认为有办法。也许你需要为它写一个bash脚本,或者为不同的分支做一个不同的回购。
的摘樱桃功能将做的工作,仅适用最后一次提交:
考虑臂A,B
git checkout A
git commit -m "Fixed the bug x"
git checkout B
git cherry-pick A
希望这有助于!
您也可以使用一些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()
这可能取决于你将如何把你的提交。我的意思是你总是有机会用一个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
但这里的问题可能是,如果这两个分支分歧,你不能推他们。您仍然可以重新组织您的代码,您的客户端检测基于您正在构建的分支。这样你就可以维护数十个客户端,并始终保持所有分支机构同步。
也许这将帮助一些人,
我用“包浩” 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"
的[提交到多个分支在同一时间与GIT](可能重复http://stackoverflow.com/questions/4532063/commit-to-multiple-branches-at-the-same-time-与git) – Johann
参见http://stackoverflow.com/a/30186843/6309 – VonC
如果Spoutnik的答案帮助你,考虑接受它与绿色复选标记 –