如何跨Git存储库中的所有分支更新文件

时间:2012-08-13 19:28:41

标签: git git-branch

如果存储库具有多个分支:如何简单地跨所有分支更新文件。

在这种情况下,它是一个类似bashrc的文件,它指定了一些环境变量。我过去更新了主分支版本,然后重新定位了每个分支。这有一个n + 1开销,我想避免。

2 个答案:

答案 0 :(得分:3)

要扩展fork0comment,您需要合并:

即:

#!/bin/bash
branches=()
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
for branch in "${branches[@]}"; do
  if [[ "${branch}" != "master" ]]; then
    git checkout ${branch}
    git checkout master -- yourFile        
  fi
done

(这适用于您的情况,因为它总是从master分支检出文件。)

答案 1 :(得分:3)

我认为,这有点晚了,但是下面的脚本可以帮助你。

#!/bin/bash

if [ $# != 1 ]; then
    echo "usage: $0 <filename>"
    exit;
fi

branches=`git for-each-ref --format='%(refname:short)' refs/heads/\*`
curr_branch=`git rev-parse --abbrev-ref HEAD`
# echo "curr_branch:"$curr_branch

filename=$1
file_in_repo=`git ls-files ${filename}`

if [ ! "$file_in_repo" ]; then
    echo "file not added in current branch"
    exit
fi

for branch in ${branches[@]}; do
    if [[ ${branch} != ${curr_branch} ]]; then
        git checkout "${branch}"
        git checkout "${curr_branch}" -- "$filename"
        git commit -am "Added $filename in $branch from $curr_branch"
        echo ""
    fi
done
git checkout "${curr_branch}"
相关问题