153

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?

2

8 Answers 8

175
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.

5
  • 5
    Just a note that this is going to create a merge commit which may not be what you want.
    – icfantv
    Commented May 8, 2018 at 16:31
  • 13
    The asker wants changes from BranchA into BranchB, but this answer is merging changes from BranchB to BranchA. Please correct! Commented May 10, 2018 at 20:17
  • I used git merge DRG origin/master
    – Ashish
    Commented Apr 5, 2019 at 8:27
  • What will happen if I delete branch A? Will it affect changes in the branch B?
    – surya
    Commented Jun 25, 2020 at 10:41
  • I think once you create a branch on your remote repository , you can do a git fetch in your local git bash/command line, and then git checkout "your new branch" would also work fine. This way your changes will only be copied
    – Brooklyn99
    Commented Oct 4, 2020 at 19:28
75

Instead of merge, as others suggested, you can rebase one branch onto another:

git checkout YourBranch
git rebase AnotherBranch

This takes YourBranch and rebases it onto AnotherBranch.

This effectively looks as if YourBranch was branched from AnotherBranch, not master.

3
  • 7
    This is the right answer if one wish to merge the changes from one branch to another branch without disturbing the master branch Commented Apr 27, 2017 at 6:37
  • 6
    What's the difference in merge and rebase and why is rebase preferred over merge?
    – Coddy
    Commented Jan 22, 2021 at 15:16
  • 2
    @Coddy: While merge creates a new commit when the merge happens. However, rebase "integrates" commits from other branch to the current branch. For more info: atlassian.com/git/tutorials/merging-vs-rebasing Advantage of rebase is that it helps to keep commit history "cleaner".
    – SKPS
    Commented Jun 8, 2022 at 18:52
12

Copy content of BranchA into BranchB

git checkout BranchA
git pull origin BranchB
git push -u origin BranchA
2
  • 3
    Why do you want to add -u option?
    – Sampath
    Commented Mar 4, 2021 at 10:18
  • 1
    I don't think this does what the OP wanted.
    – MarkHu
    Commented Feb 18, 2022 at 0:14
9

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)
8

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

4

For me the solution was - stash the current changes in the branch and then switch branches and then apply that stash to that branch.

2

Simplified solution using git switch

if you have git 2.23 or above you can use git switch command to do the same since git checkout command used for a lot of things which is confusing sometime


// This command will switch(go) to BranchB
// use flag -c if you haven't created this branch yet 
git switch BranchB 


// git merge combines sequences of commits from BranchA to the 
// current branch (which is BranchB)
git merge BranchA
0
1

If you are using tortoise git.

please follow the below steps.

  1. Checkout BranchB
  2. Open project folder, go to TortoiseGit --> Fetch
  3. In the 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.