[git] Git copy changes from one branch to another

I am new to git. I have a branch named BranchA from the master. I have some changes in BranchA (I am not going to merge changes from BranchA to master) Now I have created another branch from the master named BranchB. I want the changes from BranchA to BranchB.

Can anyone help me how to achieve this?

Thanks

This question is related to git

The answer is


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.


This is 2 step process

  • git checkout BranchB ( destination branch is BranchB, so we need the head on this branch)
  • git merge BranchA (it will merge BranchB with BranchA. Here you have merged code in branch 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)

Merge the changes from BranchA to BranchB. When you are on BranchB execute git merge BranchA


If you are using tortoise git.

please follow the below steps.

  1. Checkout BranchB
  2. Open project folder, go to TortoiseGit --> Pull
  3. In pull screen, Change the remote branch "BranchA" and click ok.
  4. Then right click again, go to TortoiseGit --> Push.

Now your changes moved from BranchA to BranchB


Copy content of BranchA into BranchB

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