2012-08-13 101 views
5

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

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

+2

你不行。編寫腳本,依次訪問每個分支,並更改(或從某處複製)其上的文件。您可以通過採用與您在第一個(working?master?)分支上提交的Git對象完全相同的Git對象來避免檢查,如果可以的話,請準確地獲取**相同的文件內容。 – fork0 2012-08-13 19:32:33

+0

我認爲這會更好地腳本rebasing。無論您是重新分配還是複製每個分支中的文件,開銷都是一樣的。那麼,爲什麼不有一個沒有重複消息和變更集的清晰歷史記錄? – 2012-08-14 06:14:44

回答

3

爲了延長fork0comment,你需要結合:

即:

#!/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分支結帳的文件。)

2

我認爲,它有點晚,但下面的腳本將幫助你在這。

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

有趣的方法也是如此。 +1 – VonC 2017-07-13 08:37:51

+0

請記得檢查您當前回購中的未定變化。 我稍後添加來檢查當前分支中的任何本地更改。 ** git status --untracked-files = no --porcelain ** – 2017-07-14 02:57:55

相關問題