美文网首页
git rebase / squash to 1 commit

git rebase / squash to 1 commit

作者: flyrain | 来源:发表于2023-07-14 19:50 被阅读0次
    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前)

    相关文章

      网友评论

          本文标题:git rebase / squash to 1 commit

          本文链接:https://www.haomeiwen.com/subject/fhfbudtx.html