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

无人久伴 提交于 2019-12-04 11:39:39

问题


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 specifies some environments variables. I have in the past updated the master branch version then rebased each branch. This has a sort of n+1 overhead, I'd like to avoid.


回答1:


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

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



回答2:


To extend fork0's comment, you need to combine:

  • "How to iterate through all git branches using bash script"
  • "git checkout specific files from another branch" (git checkout <branch_name> -- <paths>, from git checkout man page)

Ie:

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

(This is be adapted to your case, since here it always checkout the file from the master branch.)



来源:https://stackoverflow.com/questions/11940977/how-to-update-a-file-across-all-branches-in-a-git-repository

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!