【问题标题】:How to update a file across all branches in a Git repository如何跨 Git 存储库中的所有分支更新文件
【发布时间】:2012-08-10 01:49:51
【问题描述】:

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

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

【问题讨论】:

  • 你不能。编写脚本,依次访问每个分支,并更改(或从某处复制)其中的文件。您可以通过采用与您在第一个(工作?主?)分支上提交的完全相同的 Git 对象来避免签出,如果可以的话,准确地采用文件的相同内容。
  • 我认为编写变基脚本会更好。无论您在每个分支中变基还是复制文件,开销都是相同的。那么,为什么不拥有没有重复消息和变更集的干净历史记录呢?

标签: git git-branch


【解决方案1】:

要扩展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 分支签出文件。)

【讨论】:

    【解决方案2】:

    我想,有点晚了,但下面的脚本会帮助你。

    #!/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}"
    

    【讨论】:

    • 也很有趣。 +1
    • 请记住检查当前存储库中未提交的更改。我后来添加以检查当前分支中的任何本地更改。 git status --untracked-files=no --porcelain
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 2018-07-08
    • 2014-01-12
    • 1970-01-01
    • 2013-09-24
    相关资源
    最近更新 更多