「Git」合併多個 Commit
在使用 Git 作為版本控制的時候,我們可能會由於各種各樣的原因提交了許多臨時的 commit,而這些 commit 拼接起來才是完整的任務。那麼我們為了避免太多的 commit 而造成版本控制的混亂,通常我們推薦將這些 commit 合併成一個。
首先假設我們有3個 commit
![](http://upload-images.jianshu.io/upload_images/228805-ffd461efeb8a26a2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
git-log-origin.png
我們需要將 2dfbc7e8
和 c4e858b5
合併成一個 commit,那麼我們輸入如下命令
![](http://upload-images.jianshu.io/upload_images/228805-e96334b872909dc4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
git-rebase-i.png
其中,-i
的引數是不需要合併的 commit 的 hash 值,這裡指的是第一條 commit, 接著我們就進入到
vi
的編輯模式
![](http://upload-images.jianshu.io/upload_images/228805-fce11005e6ee8e2d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
git-rebase-edit.png
可以看到其中分為兩個部分,上方未註釋的部分是填寫要執行的指令,而下方註釋的部分則是指令的提示說明。指令部分中由前方的命令名稱、commit hash 和 commit message 組成。
當前我們只要知道 pick
和 squash
這兩個命令即可。
pick
的意思是要會執行這個 commitsquash
的意思是這個 commit 會被合併到前一個commit
我們將 c4e858b5
這個 commit 前方的命令改成 squash
或 s
,然後輸入:wq
以儲存並退出
![](http://upload-images.jianshu.io/upload_images/228805-8c742e137feb7ce5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
git-rebase-squash.png
這是我們會看到 commit message 的編輯介面
![](http://upload-images.jianshu.io/upload_images/228805-e0ca7e9d694e3f52.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
git-rebase-commit-message.png
其中, 非註釋部分就是兩次的 commit message, 你要做的就是將這兩個修改成新的 commit message。
![](http://upload-images.jianshu.io/upload_images/228805-7bb2a59daa90975c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
git-rebase-commit-message-combination.png
輸入wq
儲存並推出, 再次輸入git log
檢視 commit 歷史資訊,你會發現這兩個 commit 已經合併了。
![](http://upload-images.jianshu.io/upload_images/228805-aa15aa1014eadbcf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
git-rebase-log-new.png
注意事項:如果這個過程中有操作錯誤,可以使用 git rebase --abort
來撤銷修改,回到沒有開始操作合併之前的狀態。
作者:_Ryeeeeee
連結:http://www.jianshu.com/p/964de879904a
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
相關文章
- Git,互動式變基,合併多個commitGitMIT
- Git 如何合併某一個 commitGitMIT
- 多個commit合併成一個MIT
- Git 合併指定的commitGitMIT
- Git - 多個分支合併到 main 分支之後還存在一些 commit 無法併入一條時間線上GitAIMIT
- 多個excel合併Excel
- Git log 不太好看,我們來合併 commit 吧GitMIT
- git合併分支Git
- python多個txt合併Python
- Git 分支與合併Git
- oracle 合併多個sys_refcursorOracle
- Git 問題,一個 master,多個新功能分支,怎樣有序地合併和提交GitAST
- 多個excel檔案合併成一個excel表的方法 如何快速合併多個excel檔案Excel
- Git操作 :從一個分支cherry-pick多個commit到其他分支GitMIT
- python合併多個csv檔案需要注意的問題(合併多個列名問題)Python
- git 分支合併報錯Git
- git分支合併與rebaseGit
- Python合併多個csv檔案Python
- 合併單個commit到指定分支上——tortoisegit cherry pick 的使用MITGit
- git合併分支,如果選擇性的合併檔案?Git
- 視訊合併軟體有什麼,怎麼合併多個視訊
- git回退到某個commit git回滾到某個提交GitMIT
- git 知識之分支合併Git
- Git - 如何取消正在的合併Git
- 如何把多個pdf合併成一個pdf文件?
- Git合併時遇到衝突或錯誤後取消合併Git
- git commit後如何取消commitGitMIT
- git commit --amendGitMIT
- 多個 EXCEL 檔案如何合併成一個檔案Excel
- 合併多個工作薄workbooks到一個工作薄workbook
- iStylePDF把多個PDF合併成一個PDF檔案
- 這才是真正的 Git——分支合併Git
- 教你玩轉Git-分支合併Git
- git合併分支和提交步驟Git
- Git合併不同url的專案Git
- Git 分支 - 分支的新建與合併Git
- 熟練使用git 程式碼合併Git
- Git如何合併一個已經在GitHub上提交但沒有合併的Pull Request請求Github