How to update a file across all branches in a Git repository

前端 未结 2 1184
無奈伤痛
無奈伤痛 2021-02-06 09:06

In the case that a a repository has a number of branches: How does one simply update a file across all the branches.

In this case it\'s a bashrc like file that specif

2条回答
  •  孤独总比滥情好
    2021-02-06 09:51

    I think, it is bit late but following script will help you in this.

    #!/bin/bash
    
    if [ $# != 1 ]; then
        echo "usage: $0 "
        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}"
    

提交回复
热议问题