如何利用Git进行代码Branch merge

如果你想将一个分支(比如叫做other-branch)上的提交合并到另一个新的分支(比如叫做new-branch)上,你可以使用以下几种方法:

方法 1:使用 git merge

  首先,确保你在new-branch上:

  git checkout new-branch

  然后,使用git merge命令将other-branch上的更改合并到new-branch上:

  git merge other-branch
  这样,other-branch上的所有提交都会被合并到new-branch上。

【注】若使用smartgit工具,则直接通过merge 按钮,选择需要merge的提交,再通过commit提交到new branch 上;

方法 2:使用 git cherry-pick

  如果你只想合并other-branch上的某些特定提交,而不是所有提交,你可以使用git cherry-pick

  首先,确保你在new-branch上:

  git checkout new-branch
  然后,使用git cherry-pick命令逐一选择你想要的提交:

  git cherry-pick <commit_hash>
  这样,只有你明确选择的提交会被合并到new-branch上。

方法 3:使用 git rebase

  如果你想更精细地控制哪些提交会被合并,你可以使用交互式rebase

  首先,确保你在new-branch上:

  git checkout new-branch
  然后,启动交互式rebase:

  git rebase -i other-branch
  这将打开一个文本编辑器,列出所有other-branch相对于new-branch的提交。你可以选择保留、删除或修改这些提交。

posted @ 2024-07-30 14:27  咸鱼书生  阅读(21)  评论(0编辑  收藏  举报