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

GitBranch

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

In the link below it is explained how to create a hotfix branch, make changes and merge it to the master. Only difference, hotfix branch is deleted after merge.

Just use Farmcrops as a branch name and do not delete branch after merge.

GIT-SCM: Basic Branching and Merging

[STEP 1] Create a branch and make your changes

$ git checkout Farmcrops
Switched to a new branch 'Farmcrops'
$ vim index.html
$ git commit -a -m 'fix the broken email address'
[Farmcrops 3a0874c] fix the broken email address
 1 files changed, 1 deletion(-)

[STEP 2] Then, go back to master branch and merge

$ git checkout master
$ git merge Farmcrops
Updating f42c576..3a0874c
Fast-forward
 README | 1 -
 1 file changed, 1 deletion(-)

And, if you want to make more changes on the same branch, apply [step 1] again.

When you complete changes apply [step 2] again.

Follow these steps as much as required.

Once you finished your job with this branch you can delete it.

$ git branch -d Farmcrops
Deleted branch Farmcrops (was 3a0874c).

NOT: Instead of merge I suggest rebase GIT-SCM: Rebase

git rebase Farmcrops

Solution 4 - Git

  1. Merge feature branch PR
  2. Delete feature branch on GitHub
  3. git branch -d feature-branch
  4. git checkout -b feature-branch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjgravoisView Question on Stackoverflow
Solution 1 - Gitjub0bsView Answer on Stackoverflow
Solution 2 - GitJoël SalaminView Answer on Stackoverflow
Solution 3 - GitclockworksView Answer on Stackoverflow
Solution 4 - GitalbingroenView Answer on Stackoverflow