Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge a branch into master but continue working on the branch?

Tags:

git

branch

I created a branch to try a different approach and it worked, so I would like to merge the "Farmcrops" branch into "Master" to keep those changes but continue the "Farmcrops" branch to explore another possibility. That way, if this latest change doesn't work, I can revert back to "Master" which would now include the first round of changes.

How can I do that?

like image 402
jgravois Avatar asked Sep 24 '14 19:09

jgravois


People also ask

Can we commit on branch that's already been merged with master?

You can continue working on your branch and then when you merge with master again, it will bring the commits that are missing on master.

Can you merge a branch of a branch to master?

Once the feature is complete, the branch can be merged back into the main code branch. First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch.

What happens to branch after merge to master?

The answer is: nothing happens to the feature branch as a result of the merge. The new merge commit is added on the current branch (which is master when the merge is done).

Does a branch still exist after merging?

When you perform a merge, you effectively merge one branch into another—typically a feature branch or bug fix branch into a main branch such as master or develop. Not only will the code changes get merged in, but also all the commits that went into the feature branch.


2 Answers

If I understand correctly, you're starting from

-- o -- o -- o [master]     \      o -- o [Farmcrops] 

You shouldn't merge Farmcrops directly into master, because you run the risk of breaking the code in master, which, by convention, is supposed to be more stable. Instead, check out Farmcrops and merge master into it.

git checkout Farmcrops git merge master 

Then you'll get

-- o -- o -- o [master]     \         \      o -- o -- o [HEAD -> Farmcrops] 

Run some tests; make sure everything works as expected. Then check out master and merge Farmcrops into it:

git checkout master git merge Farmcrops 

Your repo will then look like this:

-- o -- o -- o     \         \      o -- o -- o [HEAD -> master,Farmcrops] 

(Note that this merge is a fast forward: it doesn't create a merge commit because Farmcrops is a direct descendant of master.)

Now check out Farmcrops again and continue your experiment, make more commits on it, etc...

-- o -- o -- o     \         \      o -- o -- o [master]                 \                  o -- o -- o [HEAD -> Farmcrops] 

You can always fall back on master (which now contains "the first round of changes", as you put it) if your new experiment on Farmcrops doesn't pan out so well.

like image 112
jub0bs Avatar answered Sep 19 '22 08:09

jub0bs


Here is the process you are looking for:

  1. git checkout master
  2. git merge Farmcrops
  3. git push origin master
  4. git branch -d Farmcrops
  5. git checkout master
  6. git checkout -b Farmcrops
  7. continue your commits on the branch Farmcrops...

Branches are just pointers, it's very easy to create/delete a branch and if your branch Farmcrops isn't pushed on remote repository, there is absolutely no dependency with it. You can delete it after the merge and recreate it from the master.

Hope this will help you.

like image 38
Joël Salamin Avatar answered Sep 20 '22 08:09

Joël Salamin