解释 Git 中的 squash 合并
想像一下你的功能分支包含大量提交 - 例如 100 個提交。不用逐個將所有提交從功能分支合併到主分支,可以將所有提交加起來放到一個提交中。這稱為 squash 提交,因為它將所有個別提交「壓縮」成一個大型變更。就主分支的歷史而言,功能分支的歷史將會消失。
我們可以使用以下命令將分支的提交 squash 合併到主分支。
$ git merge --squash feature_branch
圖表顯示主分支中有 3 個提交。我們建立的功能分支有兩個提交 F1 和 F2。現在我們需要合併功能分支和主分支中的所有提交,就像主分支中只有一個提交一樣。
在完成 squash 合併之後,主分支將會如以下所示。一旦 squash 合併成功,我們可以移除功能分支,因為它僅是儲存庫中的本機分支。
範例
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo $ git init Initialized empty Git repository in E:/tut_repo/.git/ $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'c1' [master (root-commit) 2f992c9] c1 1 file changed, 1 insertion(+) create mode 100644 hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello again>>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'c2' [master 2e550c5] c2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git branch feature $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git switch feature Switched to branch 'feature' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git add world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -m 'F1' [feature bf61466] F1 1 file changed, 1 insertion(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world again>>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -am 'F2' [feature 08061f5] F2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git switch master Switched to branch 'master' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello how are your>>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'C3' [master 91cc8c2] C3 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 91cc8c2 (HEAD -> master) C3 | * 08061f5 (feature) F2 | * bf61466 F1 |/ * 2e550c5 c2 * 2f992c9 c1 $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git merge --squash feature Automatic merge went well; stopped before committing as requested Squash commit -- not updating HEAD $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git status -s A world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'squashing F1 and F2' [master ad342f3] squashing F1 and F2 1 file changed, 2 insertions(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * ad342f3 (HEAD -> master) squashing F1 and F2 * 91cc8c2 C3 | * 08061f5 (feature) F2 | * bf61466 F1 |/ * 2e550c5 c2 * 2f992c9 c1 $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat world.txt world world again $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git branch -D feature Deleted branch feature (was 08061f5). $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * ad342f3 (HEAD -> master) squashing F1 and F2 * 91cc8c2 C3 * 2e550c5 c2 * 2f992c9 c1
广告