简体   繁体   English

将更改从一个分支复制到另一个分支

[英]Copy changes from one branch to another

I have a branch named BranchA from master .我有一个来自master名为BranchA的分支。 I have some changes in BranchA (I am not going to merge changes from BranchA to master ).我在BranchA有一些更改(我不打算将BranchA更改合并到master )。

Now I have created another branch from master named BranchB .现在我从master创建了另一个名为BranchB分支。

How can I copy the changes from BranchA to BranchB ?如何将更改从BranchA复制到BranchB

git checkout BranchB
git merge BranchA
git push origin BranchB

This is all if you intend to not merge your changes back to master.如果您不打算将更改合并回母版,这就是全部。 Generally it is a good practice to merge all your changes back to master, and create new branches off of that.通常,将所有更改合并回 master 并从中创建新分支是一种很好的做法。

Also, after the merge command, you will have some conflicts, which you will have to edit manually and fix.此外,在合并命令之后,您将遇到一些冲突,您必须手动编辑并修复这些冲突。

Make sure you are in the branch where you want to copy all the changes to.确保您位于要将所有更改复制到的分支中。 git merge will take the branch you specify and merge it with the branch you are currently in. git merge将采用您指定的分支并将其与您当前所在的分支合并。

Instead of merge, as others suggested, you can rebase one branch onto another:正如其他人建议的那样,您可以将一个分支重新设置为另一个分支,而不是合并:

git checkout BranchB
git rebase BranchA

This takes BranchB and rebases it onto BranchA , which effectively looks like BranchB was branched from BranchA , not master .这需要BranchB并将其BranchABranchA ,这实际上看起来BranchB是从BranchA分支的,而不是master

This is 2 step process这是 2 步过程

  • git checkout BranchB ( destination branch is BranchB, so we need the head on this branch) git checkout BranchB (目标分支是BranchB,所以我们需要这个分支上的head)
  • git merge BranchA (it will merge BranchB with BranchA. Here you have merged code in branch B) git merge BranchA (它会将 BranchB 与 BranchA 合并。这里你已经合并了分支 B 中的代码)

If you want to push your branch code to remote repo then do如果要将分支代码推送到远程仓库,请执行以下操作

  • git push origin master (it will push your BranchB code to remote repo) git push origin master (它会将你的 BranchB 代码推送到远程仓库)

Copy content of BranchA into BranchBBranchA 的内容复制到BranchB

git checkout BranchA
git pull origin BranchB
git push -u origin BranchA

Merge the changes from BranchA to BranchB.合并从 BranchA 到 BranchB 的更改。 When you are on BranchB execute git merge BranchA当你在 BranchB 时执行git merge BranchA

If you are using tortoise git .如果您使用的是乌龟 git

please follow the below steps.请按照以下步骤操作。

  1. Checkout BranchB结账BranchB
  2. Open project folder, go to TortoiseGit --> Pull打开项目文件夹,转到TortoiseGit --> Pull
  3. In the pull screen, Change the remote branch BranchA and click ok.在拉取屏幕中,更改远程分支BranchA ,然后单击确定。
  4. Then right-click again, go to TortoiseGit --> Push .然后再次右键单击,转到TortoiseGit --> Push

Now your changes moved from BranchA to BranchB现在您的更改从 BranchA 移至 BranchB

对我来说,解决方案是 - 将当前更改存储在分支中,然后切换分支,然后将该存储应用到该分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM