Moshe Shaham
Moshe Shaham

Reputation: 15974

Merge branch to a different branch from the starting branch

I have branch master and branch dev. I started a feature branch from master by mistake. Meaning, I did git checkout -b feature-branch while I was on master.

Now I want to merge the feature branch on branch dev. How do I do it?

Upvotes: 1

Views: 56

Answers (3)

JooMing
JooMing

Reputation: 932

If there are not too many commits, then "git cherrypick" is an option:

  1. create new topic branch from dev branch
  2. use git cherrypick to copy commits from the original topic branch
  3. delete the original topic branch

EDIT: I added an example how to do the same with "git rebase" command:

  1. figure out the hash for the commit that is the parent of the topic branch
  2. git rebase --onto dev hash_b4_topic topicbranch

You can also use the branch~N syntax instead of the commit hash if you are not afraid of counting commits ;) Although it just one command, I would still use the first option, because it it takes quite a bit more effort to get it right (i.e. check the manual, select the right command line arguments). So it would make sense only for moving longer topic branches.

Upvotes: 1

aerickson
aerickson

Reputation: 116

You can use 'git log' to find the point before you started your work and then use 'git diff' to find the difference between the current HEAD and that point. 'git apply' will apply that diff.

  1. On your feature branch, use git log to find the start of your work. Record the commit before your work.

     git checkout feature-branch
     git log
    
  2. Store the difference between the point you found above and your work (the current HEAD revision). HEAD is an implied argument.

     git diff (commit_from_step_above) > patch.txt
    
  3. Apply your change to a correct feature branch.

     git checkout dev
     git checkout -b proper_feature-branch
     git apply patch.txt
     (add, commit, etc)
    

If others have changed the same files you're changing, you'll have a merge conflict. This is expected.

Upvotes: 0

Deathstorm
Deathstorm

Reputation: 847

to merge the commited changes that you've made from one branch to another you could use: git merge <branchname>. Let me set you an example:"

I want to merge the changes i've made on branch dev with my master branch. The first thing i'll do is commit the changes on branch dev then i'll switch to branch master and i will merge the changes from the dev branch by using:

git merge dev

So step by step: first commit your changes from the development branch.

git commit -m "<commit message>"

Step two: Switch to the master branch

git checkout master

Last step: merge your changes from the dev branch/

git merge dev

Hope this helps!

Upvotes: 0

Related Questions