Home > Net >  How to copy one branch to another in git
How to copy one branch to another in git

Time:01-16

I need to copy one branch to another in git. I need to copy only the commits from fix branch to master, only the last three commits. How can I do that?

- x - x - x (master)
  \
   x - x (dev)
        \
         x - x - x (fix)

CodePudding user response:

You could say

git switch master
git cherry-pick dev..fix

Or you could do the same thing by saying

git switch fix
git switch -c temp
git rebase --onto master dev temp
git switch master
git reset --hard temp
git branch -D temp

Either way, now master has acquired three new commits which are copies of the three commits on fix after dev.

CodePudding user response:

You can create a new branch from master(say, hotfix-abcd), and cherry-pick the commits in fix.

Lets say you have these three commits in fix,

a1234 FixBob
a1235 FixAlice
a1232 FixTom
  1. Go to master, git checkout master
  2. Create a branch from the head, git checkout -b 'hotfix-abcd'
  3. Now you'll be in hotfix-abcd branch, cherry-pick the commits git cherry-pick a1234 a1235 a1232
  4. Create a Pull Request from hotfix-abcd to master.
  •  Tags:  
  • Related