簡體   English   中英

如何跨Git存儲庫中的所有分支更新文件

[英]How to update a file across all branches in a Git repository

在aa存儲庫具有多個分支的情況下:如何簡單地跨所有分支更新文件。

在這種情況下,它是一個類似bashrc的文件,它指定了一些環境變量。 我過去更新了主分支版本,然后重新定位了每個分支。 這有一個n + 1開銷,我想避免。

我想,這有點晚了,但是下面的腳本會幫助你。

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

要擴展fork0注釋 ,您需要組合:

即:

#!/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分支中檢出文件。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM