簡體   English   中英

將git存儲庫中的所有現有文件替換為除.gitignore中的遠程分支之外的其他遠程分支

[英]Replace all existing files in git repository with a different remote branch except the ones in .gitignore

我有幾個存儲庫,我總是需要合並一個core存儲庫。 core存儲庫在每個存儲庫中設置為remote存儲庫。

通常,我更新存儲庫的工作流程如下:

git reset --hard origin/master
git pull origin master
git fetch core master

然后我做一個git merge --squash core/master並修復任何沖突,然后再將存儲庫推回到remote

這很好,除了它有點多余,因為除了每個存儲庫的.gitignore文件中的所有內容之外,這些存儲庫中的所有內容在技術上應該與core存儲庫相同。

隨着存儲庫數量的增加,我想知道將core分支拉入這些存儲庫的更有效方法是什么,我需要替換所有現有文件,除了.gitignore特別提到的每個文件,同時保持git歷史和日志中的完整性。

你可以使用git和bash的組合來完成它。 我寫了一個示例腳本來說明如何完成它。 您可以隨時修改它並使其更好。 我也提供了一些解釋。 該文件名為adder.sh

#!/bin/bash
# $1 -> Files from the branch you want (core)
# $2 -> Branch you want to merge into (master)

git checkout $2
git diff --name-status $1..$2 | grep '^\(D\|M\)\s*' | cut -f2 > ~/.dummy
git checkout $1 -- $(cat ~/.dummy)
git add .

要調用它,只需使用$ sh adder.sh core master 在此之后, core分支中的所有新添加和修改的文件將添加到master倉庫中。 使用git狀態,您可以看到新內容,然后相應地提交和推送。

$ git commit -m "Skipping Conflicts"
$ git push

關於它是如何工作的一些解釋:

$ git diff --name-status master..core

產生以下輸出:

M       public/stylesheets/main.css              # Modified
D       public/templates/createUser.html         # Present in core branch and not master (new file)
A       public/templates/dashboard.html          # Present in master and not in the core branch (don't touch)

因此,編寫一個簡單的正則表達式,僅選擇已修改的文件並將其修改為正確的格式,然后將其存儲在臨時文件中。

$ cat ~/.dummy

public/templates/createUser.html
public/stylesheets/main.css

然后我們需要將這些文件添加到當前分支,因此我們使用git checkout。 有關如何使用git checkout查看此答案


還有另一種方法可以做到這一點。 官方的方式,使用git rerere 手冊頁

在使用相對長壽命的主題分支的工作流中,開發人員有時需要反復解決相同的沖突,直到主題分支完成(或者合並到“發布”分支,或者發送並接受上游)。

此命令通過在初始手動合並中記錄沖突的自動登記結果和相應的手部解析結果,並將先前記錄的手部分辨率應用於其相應的自動登記結果,來幫助開發人員完成此過程。

注意:您需要設置配置變量rerere.enabled才能啟用此命令。

本文給出了該命令及其用例的正確概述。

暫無
暫無

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

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