Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. 如何使用 Git
  6. 如何在 Git 中管理历史记录
  7. 将提交复制到不同的分支
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

将提交复制到不同的分支

请转至您之前下载的git-tutorial/tutorial4目录。

当您检查此存储库的历史记录时,它将如下所示:

Current history

我们将在主分支中创建一个新提交,它是同一存储库不同分支上提交的副本。在这种情况下,让我们复制带有“append description of the commit command”消息的提交。

切换到主分支。使用 git cherry-pick 命令,并为“append description of the commit command”指定 commit hash。

提交哈希99daed2可能因您的本地存储库而异。您可以通过在该分支中运行 git log 命令,来获得提交哈希值。查找您本地存储库的提交哈希,并改用该提交哈希。

$ git checkout main
  Switched to branch 'main'
$ git cherry-pick 99daed2
  error: could not apply 99daed2... commit
  hint: after resolving the conflicts, mark the corrected paths
  hint: with 'git add <paths>' or 'git rm <paths>'
  hint: and commit the result with 'git commit'

如您所见,发生了冲突。手动解决该冲突,并继续提交更改。

$ git add sample.txt
$ git commit

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life