Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-branch

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.

like image 644
domino Avatar asked Aug 13 '12 19:08

domino


1 Answers

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

like image 82
VonC Avatar answered Oct 04 '22 02:10

VonC