Autosquash 提交你希望在 rebase 期間壓縮的程式碼

鑑於以下歷史記錄,假設你要進行更改,以便壓縮到提交 bbb2222 A second commit

$ git log --oneline --decorate
ccc3333 (HEAD -> master) A third commit
bbb2222 A second commit
aaa1111 A first commit
9999999 Initial commit

完成更改後,可以像往常一樣將它們新增到索引中,然後使用 --fixup 引數提交它們,並引用要壓縮的提交:

$ git add .
$ git commit --fixup bbb2222
[my-feature-branch ddd4444] fixup! A second commit

這將建立一個新的提交,其中包含 Git 在互動式 rebase 期間可以識別的提交訊息:

$ git log --oneline --decorate
ddd4444 (HEAD -> master) fixup! A second commit
ccc3333 A third commit
bbb2222 A second commit
aaa1111 A first commit
9999999 Initial commit

接下來,使用 --autosquash 引數進行互動式 rebase:

$ git rebase --autosquash --interactive HEAD~4

Git 會建議你將使用 commit --fixup 做出的提交壓縮到正確的位置:

pick aaa1111 A first commit
pick bbb2222 A second commit
fixup ddd4444 fixup! A second commit
pick ccc3333 A third commit

為避免必須在每個 rebase 上鍵入 --autosquash,你可以預設啟用此選項:

$ git config --global rebase.autosquash true