Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy changes from one branch to another

Tags:

git

I have a branch named BranchA from 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 master named BranchB.

How can I copy the changes from BranchA to BranchB?

like image 939
Mukil Deepthi Avatar asked Dec 05 '16 13:12

Mukil Deepthi


People also ask

How do I move current changes to a new branch?

The git checkout -b <BranchName> command will create a new branch and switch to it. Moreover, this command will leave the current branch as it is and bring all uncommitted changes to the new branch.

How do I pull changes from another branch without merging?

You could use git checkout from the branch you want to transfer the changes to: git checkout <branch name> . That will change all files to match the version in the desired branch. Then you can commit, change, discard whatever you want.


Video Answer


4 Answers

git checkout BranchB
git merge BranchA

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.

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.

like image 91
roymustang86 Avatar answered Oct 19 '22 20:10

roymustang86


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.

like image 25
Archie Avatar answered Oct 19 '22 19:10

Archie


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)
like image 9
TULSI JAIN Avatar answered Oct 19 '22 19:10

TULSI JAIN


Copy content of BranchA into BranchB

git checkout BranchA
git pull origin BranchB
git push -u origin BranchA
like image 7
Pankaj Singh Avatar answered Oct 19 '22 21:10

Pankaj Singh