我最初在 GH 上更新了一個檔案。在 PR 中,我最終做了 3 次提交。我想使用 CLI 將它們壓縮成一個提交。我最終又犯了兩次錯誤。現在,我不知道怎么回去。
這是我的git log --oneline:
4t0po5ec2 (HEAD -> page-update, origin/page-update) Merge branch 'page-update' of github.com:usern>
45yh4t106 Fixes links
wer42q158 Reverts animation
g0otle08b Updates syntax
adf4213bd Fixes links
t549tie217 (master) Review.
我試圖將前三個提交(adf4213bd、g0otle08b、wer42q158)壓縮成一個 after (master) Review。最近的兩次提交(45yh4t106、4t0po5ec2)是在嘗試這樣做時錯誤進行的,因此不需要。
我試過git reset --soft HEAD~3了,但它讓我更進一步master。我試過git pull origin master --rebase了,但這也保留了最新的提交,這意味著最后有不止一個提交。
uj5u.com熱心網友回復:
注意:備份您的 repo 和作業樹以避免丟失任何(未提交的)作業。
查看歷史記錄,很明顯您已經重寫了本地歷史記錄,然后將舊歷史記錄合并回來,再次添加了那些重寫的提交。要擺脫這種情況,請將您的本地分支移回一次提交。你可以使用git reset --hard HEAD^它。讓我重復上面的注釋:reset --hard將洗掉您擁有的任何未提交的作業。如果這樣做,任何未提交的更改都會丟失并永遠消失。
回到你最初的問題:
聽起來您想壓縮在master之后發生的分支的所有提交。git reset --soft是實作這一目標的簡單方法:
使用--soft, reset 將使您的索引和之前提交的所有更改處于“暫存”狀態,這意味著它們將成為下一次提交的一部分。
git checkout page-update
git reset --soft master
git commit # now enter your new commit message
現在,在推送時,Git 會抱怨,因為您更改了已經推送的提交。假設提交沒有在任何地方合并并且沒有人在它們之上構建更新的提交,您必須強制推送(而不是pull來自遠程存盤庫的舊提交)。
uj5u.com熱心網友回復:
我會使用互動式變基,即
git rebase -i HEAD~5
并標記fixup除最舊的提交之外的每個提交。洗掉要完全洗掉提交的行。
也許,畢竟,編輯提交訊息。(并且強制推動,我相信在改寫歷史時這是不可避免的)。
uj5u.com熱心網友回復:
我強烈建議始終使用--graphwith git log(特別是對于人類查看,可能有一點不要在腳本中使用它),否則您無法理解正在查看的歷史記錄中的分支和合并。
“git reset HEAD~3太遠了”可能表明之后的提交序列master不是一條直線,而是一個 fork&merge 序列:
# given the message of topmost commit, it is a merge commit
* 4t0po5ec2 (HEAD -> page-update, origin/page-update) Merge branch 'page-update' of github.com:usern>
# blind guess on the history: the local and remote branches forked after master
|\
* | 45yh4t106 Fixes links
* | wer42q158 Reverts animation
| * g0otle08b Updates syntax
| * adf4213bd Fixes links
|/
* t549tie217 (master) Review.
如果你運行,這也會導致意想不到的事情git rebase master——默認情況下,git rebase 完全忽略其串列中的所有合并提交。
(注意:上圖是盲目猜測,據我們所知,分叉點甚至可以在 master 之前,或者可能有其他中間合并)
如果你想在你的回購中恢復線性歷史:
- 找出兩個分支中的哪一個是你想要保留的(讓我們堅持我上面的圖表,并假設你想在上面寫你的提交
g0otle08b Updates syntax) - 去那個“頭”提交:
git reset --hard g0otle08b - 挑選另外兩個:
git cherry-pick wer42q158 45yh4t106
你現在應該有類似的東西:
* (HEAD -> page-update) b4rfdp0dl Fixes links # rewritten, new sha
* m3rglbl0b Reverts animation # rewritten, new sha
* g0otle08b Updates syntax
* adf4213bd Fixes links
* t549tie217 (master) Review.
并且重新安排提交會更容易(例如,使用git rebase -i)
uj5u.com熱心網友回復:
我同意@mbojko,互動式 rebase 是我會使用的工具。
如果您以前從未做過變基,那么變基可能會有點令人生畏 - 所以這里詳細描述了如何為您的場景運行互動式變基。
1.啟動互動式rebase:
git rebase -i master^
# rebase the commits from your head to one before master
# (`master^` means one commit before master).
或者
git rebase -i HEAD~5
# rebase the last five commits
# same as above, just a different way of writing it
git 將打開一個看起來像這樣的編輯器:
pick t549tie217 (master) Review.
pick adf4213bd Fixes links
pick g0otle08b Updates syntax
pick wer42q158 Reverts animation
pick 45yh4t106 Fixes links
pick 4t0po5ec2 Merge branch 'page-update' of github.com:usern>
請注意,提交以相反的順序列出,從最舊的頂部到最近的底部!
這使您可以選擇每次提交會發生什么(變基的“互動式”部分)。
2.將三個提交壓縮為一個并丟棄最后兩個提交,然后將檔案編輯為如下所示(我在某些行上更改了“pick”)。
pick t549tie217 (master) Review.
pick adf4213bd Fixes links
fixup g0otle08b Updates syntax
fixup wer42q158 Reverts animation
drop 45yh4t106 Fixes links
drop 4t0po5ec2 Merge branch 'page-update' of github.com:usern>
檔案的底部是描述每個命令將執行的操作的注釋,但這里有一個快速摘要:
p,pick: 默認情況下,將按原樣使用提交和訊息。f,fixup: 將提交壓縮到前一個提交中并使用前一個提交的訊息d,drop: 放棄提交
所以這意味著
pick t549tie217 (master) Review. # keep this one
pick adf4213bd Fixes links # keep this one
fixup g0otle08b Updates syntax # merge into adf4213bd
fixup wer42q158 Reverts animation # merge into adf4213bd
drop 45yh4t106 Fixes links # drop commit
drop 4t0po5ec2 Merge branch 'page-update' of github.com:usern> # drop commit
3.保存并退出編輯器后,git 將執行 rebase。如果一切順利,那么您的分支將處于所需的狀態。
注意 - 如果你開始變基然后決定不想做任何事情 - 即你想中止變基 - 那么只需注釋掉檔案中的每一行。
這將中止 rebase,因為 git 無事可做:
#pick t549tie217 (master) Review.
#pick adf4213bd Fixes links
#fixup g0otle08b Updates syntax
#fixup wer42q158 Reverts animation
#drop 45yh4t106 Fixes links
#drop 4t0po5ec2 Merge branch 'page-update' of github.com:usern>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515221.html
標籤:混帐
