7

git快速合并当前分支到其他分支

 2 years ago
source link: https://www.liesauer.net/blog/post/git-a-quick-way-to-merge-current-branch-to-other-branch.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

git快速合并当前分支到其他分支

当前在A分支,要求将当前分支的提交合并到B分支。

  1. # 暂存当前未提交的改动
  2. git stash -m "xxx"
  3. # 切换到B分支
  4. git checkout B
  5. # 合并A分支到当前分支(即B分支)
  6. git merge A
  7. # 推送提交
  8. git push origin B:B
  9. # 切换回A分支
  10. git checkout A
  11. # 取出暂存的改动继续工作
  12. git stash pop

这种常规合并的操作方式不仅繁琐,而且要求当前分支不能有未提交的更新(意味着如果有未提交的改动,你必须先进行stash,否则无法切换分支),还有一个问题就是我们的项目是做了文件改动后自动增量编译的,意味着这一系列操作将会触发两次重编译(两次checkout),浪费大量的时间。

  1. # 直接将当前分支合并到B分支
  2. git push . HEAD:B
  3. # 推送提交
  4. git push origin B:B

这种常规合并的方式不需要任何的暂存、切换分支操作,也不会触发重编译,可节省大量时间。但是这种方式也有缺点,首先只支持fast-forward模式的合并,如果非fast-forward模式则需要先将B分支合并到当前分支,这样子就会对当前分支有侵入,其次用这种push的方式合并在reflog是不会有记录的,所以以后如果要回退,就不容易找到对应位置的commit id。如果你确定要合并的提交不会造成冲突且不会造成大的影响,可以使用这种方式合并。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK