GIT:如何将一个分支的内容复制到其他分支?

Bal*_*lli 14 git git-branch

我有'develop'和'InitialPomChanges'分支.我想将develop分支的所有内容复制到InitialPomChanges分支.

Dav*_*sch 13

假设您要使用开发中的内容覆盖InitialPomChanges的所有内容(即您希望InitialPomChanges的内容与开发完全匹配),请执行以下操作:

git checkout InitialPomChanges
git checkout develop .  #copies the contents of develop into the working directory
git commit -am "Making InitialPomChanges match develop"
Run Code Online (Sandbox Code Playgroud)

这将使InitialPomChanges中的最后一次提交与develop中的最后一次提交相匹配.为了使两个分支之间的未来合并更容易,现在做一个是个好主意git merge develop.

或者,如果要更改InitialPomChanges的内容并在一次提交中执行合并,则可以执行以下操作:

git checkout InitialPomChanges
git merge -s theirs develop
Run Code Online (Sandbox Code Playgroud)

  • top 方式比合并更可靠,因为在多次合并分支之后,如果你犯了一个错误,你最终可能会遇到 git 说没有什么可以合并的情况,因为你之前以特定方式解决了合并,并且它假设是你想要什么,但你假设文件现在都是一样的,但事实并非如此。当将代码从暂存部署到生产时,您不应该合并,如果您想避免有一天出现严重的挫败感,您应该完全复制此处所示的内容 (3认同)

The*_*ous 7

你可以使用git mergegit rebase

如果你在InitialPomBranch上,你可以简单地运行

git merge develop
Run Code Online (Sandbox Code Playgroud)

要么

git rebase develop
Run Code Online (Sandbox Code Playgroud)

第一个将开发分支的所有提交合并到InitialPomBranch.第二个将把develop分支的所有提交放在InitialPomBranch的第一个提交之下

编辑:Rebase将更改InitialPomBranch的所有提交的SHA哈希值.所以你必须跑步

git push -f origin InitialPomBranches 
Run Code Online (Sandbox Code Playgroud)

推动所有变化

  • 公共分支中的`git rebase`(或一般的强制推送)可能会影响其他贡献者;考虑他们是否也应该重新调整基础,以及合并是否更合适。 (2认同)

小智 3

$ git merge develop 
Run Code Online (Sandbox Code Playgroud)

确保您当前的分支是InitialPomChanges