git 合並commit操作實例 merge --squash 選項 和 git rebase


方法一、用 git merge

 參考:

[轉] git merge 將多個commit合並為一條之--squash 選項

 

git checkout master

git pull origin master  # 本地先拉取最新的master,最后目標是要merge到master

git branch feature-123-merge  # 從master創建一個新的專門用來做merge的新branch:feature-123-merge

git checkout feature-123-merge

git merge --squash feature-123  # 這里把原來有n多commit的feature-123的branch合並到feature-123-merge

git commit -m "feature-123: A test change for merging with squash"  # commit一次,然后push

git push origin feature-123-merge:feature-123-merge  # 這時候在服務器上的feature-123-merge就只有最新的這一次commit,可以pull request再merge到master了

 

 

方法二、用 git rebase

# 在當前branch中,假設已經有14個commit,想合並他們
# Rebase:
git rebase -i HEAD~14   # 從最新的HEAD開始,合並總共14個commit
#   Or:   git rebase -i da4a634~1 從前面某個commit開始,合並到第一個
#  在rebase的交互編輯中, "pick" 第一行,可以修改comment, 然后"fixup" 剩下的行,最前面的pick改成字母f就行了)

# 然后強制push,搞定
git push --force

 

修改最后一次commit的message:

git commit --amend

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM