我最近創建了一個 master 分支來恢復一個非常大的提交。
然后我首先創建了一個 master 分支,
我用過git revert "commit tag from gitlab",
但是我現在有很多合并沖突,并且想解決它們,以便只有 Head 更改或根據我的理解解決合并,以便在合并中排除來自大型提交的更改。這個想法是創建一個分支,在錯誤提交之后包含每個提交,然后將其與 master 合并,以便排除錯誤提交。
因此,如果 F 是錯誤提交,則新分支將是:
大師:A->B->F->C->D
熱固定分支:A->B->C->D
謝謝!
uj5u.com熱心網友回復:
對于任何想知道的人,這是 OP 所說的從我的回答中幫助他的內容:
git revert F選項 1 [git checkout --ours] 最終幫了大忙!
現在,這是我的答案:
如何:
- 恢復 git 中的提交,這是幾個提交
- 洗掉 git 中的單個提交,以及
git reset --soft使用該技術在 git 中壓縮多個提交
設想:
master:A->B->F->C->D
您想要洗掉或恢復提交 F 并最終得到:
hot_fixed_branch:A->B->C->D
關于手動解決沖突的說明
如果你運行git revert F,你有沖突的原因是因為 C 和/或 D 中的更改與 F 中的更改觸及了相同的行。因此,沒有簡單的方法來“撤消 F”。如果您嘗試撤消 F,它也會撤消 C 和 D 的部分內容,因此您必須手動解決這些沖突以保留 C 和 D 的所需部分,同時從相同的行中洗掉 F 的內容。
在解決git revert F沖突的程序中,您可以選擇嘗試以下方法:
# Option 1:
# keep only the changes expressed by `git diff F..HEAD`, meaning: the changes
# done by C and D
git checkout --ours
# Option 2:
# keep only the changes expressed by `git diff F..F~`, meaning: the changes
# which are **the opposite of F**, thereby exactly undoing F
git checkout --theirs
另請閱讀我的git revert部分,以防萬一有助于您理解它:根據 Git,誰是“我們”/“我們的”和“他們”/“他們的”?.
但是,如果您執行上面的選項 1,您仍然會保留 F 的一些更改,因為 C 和 D 是在 F之上構建的。而且,如果您執行上面的選項 2,它可能會破壞或洗掉由 C 和 D 添加的更改,因為它們是建立在 F 之上的。上面兩個選項之一可能會讓你接近你想要的,但無論你選擇哪個,你仍然會破壞一些東西。這就是為什么沖突首先存在的原因!沖突的存在告訴您 C 和/或 D 觸及與 F 相同的線路。
所以,最好的辦法是打開一個新終端并在解決所有沖突的同時運行它,這樣您至少可以看到 F 添加了什么,然后能夠手動理解它并像手動一樣正確撤消它解決沖突:
- 確保
meld是您的差異工具。請在此處查看我的說明:如何使用 meld 作為您的 git difftool 代替 git diff。Meld 是比較 GUI 變化的好方法。 - 運行此程式以查看 F 創建的更改,因此您可以知道如何在解決沖突時手動撤消它們:
# see the changes added by F git difftool F~..F # OR, to specify just a particular folder to look at git difftool F~..F -- path/to/dir # OR, to specify just a particular file to look at git difftool F~..F -- path/to/file # OR specify a couple files git difftool F~..F -- file1 file2 # etc.
洗掉或撤消提交 F 的各種方法
如果下面的任何 git 命令沒有意義,請閱讀并研究我所有的參考資料。我實際上是一次打開了這些選項卡來自己寫這個答案,在寫這個答案時參考我所有的參考資料。
有多種方法可以做到這一點。
- 使用
git revert F(你最終會得到A->B->F->C->D->F',其中F'是相反的F,從而撤消F的更改):# create and check out hot_fix from B git checkout -b hot_fix B # Now revert F git revert F # manually fix conflicts, then commit them git add -A git revert --continue - 如果
git revert F太難解決,那么嘗試簡單地將 C 和 D 挑選到 B 上(你最終會得到A->B->C->D):# create and check out hot_fix from B git checkout -b hot_fix B # Now cherry-pick C and D onto it git cherry-pick C D # manually resolve conflicts as necessary, and continue, as often as # necessary git add file1.c file2.c # etc. git cherry-pick --continue - 執行上面的櫻桃挑選可能需要多次解決相同的沖突,因此首先壓縮 C 和 D 更容易,然后將壓縮的結果挑選到 B 上(你最終會得到
A->B->E,其中E包含的提交的更改C并D壓縮為單個提交):# Create and check out a "master_copy" branch from "master" so you don't # mess up master git checkout -b master_copy master # squash C and D on "master_copy" into a single commit git reset --soft C~ git commit -m "Squashed commit containing both C and D" # Now check out "hot_fix" from B and cherry-pick that squashed # commit we just made onto it git checkout -b hot_fix B git cherry-pick master_copy # manually resolve conflicts, then commit them git add -A # or git add file1.c # etc. # Once done fixing and adding ALL files, then do: git cherry-pick --continue
參考:
- 根據 Git,我對誰是“我們”/“我們的”和“他們”/“他們的”的回答?
- 我對從另一個分支在 git 中創建分支的各種方法的回答
- 我對如何挑選單個提交、多個提交或一系列提交的回答
- 如何采取“
git reset --soft方式”來壓縮提交。在我的eRCaGuy_dotfiles 存盤庫中搜索我的git 和 Linux cmds、幫助、提示和技巧 - Gabriel.txt檔案`git reset --soft` way - 如何使用 meld 作為 git difftool 代替 git diff
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521440.html
