git rebase / squash to 1 commit

2023-07-14  本文已影响0人  flyrain
  1. 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先提交
  1. 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

推荐使用第二种方法,因为借助了临时分支,相当于做了一层保护,就算在这个临时分支上操作错误也不会影响到你的feature分支(force push前)

上一篇 下一篇

猜你喜欢

热点阅读