9

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.

domino
  • 141
  • 2
  • 5
  • 2
    You can't. Script it, by visiting each branch in turn, and changing (or copying from somewhere) the file on them. You can avoid the checking out by taking precisely the same Git object you committed on the first (working? master?) branch, if it is ok take **precisely** the same content of the file. – fork0 Aug 13 '12 at 19:32
  • I think it would be better to script the rebasing. Overhead is the same whether you rebase or copy the file in each branch. So why not have a clean history without duplicated messages and changesets? – Marcin Koziński Aug 14 '12 at 06:14

2 Answers2

6

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

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.)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
5

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}"
Vivek Kumar
  • 71
  • 1
  • 4
  • Interesting approach as well. +1 – VonC Jul 13 '17 at 08:37
  • 1
    Do remember to check for uncommited changes in your current repo. I later on added to check for any local changes in current branch. **git status --untracked-files=no --porcelain** – Vivek Kumar Jul 14 '17 at 02:57