- git rebase
git rebase 我通常會用于想在保留本地分支改動的同時拉取最新的master改動,然后將我們本地的commit移動到最后,例如
feature branch A check out from the origin master
feature branch B check out from the origin master
featureA commit with commit1
featureB commit with commit2 and merge to origin master
featureA rebase origin master
featureA merge to origin master
// 此時 commit1 在git log中會位于commit2之后,盡管在rebase前 commit1先提交
- squash to 1 commit
方法1:
// X equals to the commits you want to squash
git rebase -i HEAD~[X]
方法2:
git checkout master and pull from origin master
git checkout -b tmp_banch
git merge --squash your_feature_branch
git add and git commit
git push -f origin tmp_branch:your_feature_branch
推薦使用第二種方法,因為借助了臨時分支,相當(dāng)于做了一層保護(hù),就算在這個臨時分支上操作錯誤也不會影響到你的feature分支(force push前)