簡體   English   中英

將更改合並到git repo中,就好像它們過去發生過一樣

[英]merge changes to git repo as if they had happened in the past

我對git很陌生,所以如果這很明顯(或不可能),請原諒我!

我將其用於CMS網站。 我經常遇到我對第3方組件進行了修改,然后進行更新的情況。

我如何才能從已更新的組件中添加更改,就好像它們是在更改之前發生的一樣? 還是這是“重寫歷史”?

我是否從引入了組件的舊提交中創建分支,添加更新的代碼,然后合並到master中? 子模塊? 還是我吠叫了錯誤的樹,而以另一種方式更好地處理了這種情況?

(最初,此答案假設第3方組件來自公共git repo,@ jacob-dorman澄清說他是通過從較大項目的快照中復制組件代碼來獲取更新的)

您需要維護一個包含兩個分支的存儲庫:

  • master :每次獲取組件的新快照時,將其提交到此分支
  • tweaks :基於master分支的分支,其中包含構成您的自定義調整的提交

每次使用組件的新快照更新“ master”分支時,請將tweaks分支重新設置在master分支之上:

$ git rebase master tweaks

這將使您的調整有效地成為發生的最后一件事

因此,從一開始,這就是命令行上的樣子……

抓取第一個快照並將其存儲在“ master”分支中

mkdir my-fork-of-shinything
cd my-fork-of-shinything
git init
cp -R /tmp/shinything-snapshot-1 .  # copy the snapshot of code into your repo
git add -A    // stages ALL directory contents for commit
git commit -m "V1 snapshot of the shinything component"

這樣就建立了您的“主”分支。

在新的“ tweaks”分支上添加您的調整

git checkout -b tweaks   // creates your new branch
... make your tweaks, commit them

后來,當對原始組件進行了一些有趣的更新時...

獲取新的組件快照,更新“ master”和“ tweaks”分支

git checkout master
rm -Rf * // delete the contents of the old snapshot
cp -R /tmp/shinything-snapshot-2 . // get the brand new snapshot in place
git add -A
git commit -m "V2 snapshot of shinything, brings fix for #234"

現在,您的master分支具有該組件的更新歷史記錄,並且每個快照更新都具有一個提交。 最后, 重訂到主分支“微調”分支-每個你的“微調”分支的提交將被重播,一前一后,在主分支的新梢頂部:

git rebase master tweaks // re-bases the commits from your tweaks branch

還有更多調整?

如果您需要添加更多調整,只需將分支檢出並添加即可:

git checkout tweaks
... make your tweaks, commit them

一個簡單的命令git pull --rebase

Br,蒂姆

暫無
暫無

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

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