merge one local branch into another local branch

GitMerge

Git Problem Overview


I have multiple branches which are branched off the master (each in a separate subdirectory).

  • Branch1: new development, not yet completely finished
  • Branch2: hotfix for a problem, but still under test
  • Branch3: mess around branch, which I will not restore

Before testing of the hotfix is finished I would like to have the code already available in Branch1, so I can continue developing with the fix in place.
(But since my experience with git is not that much I first started to play around with merge in a 3rd branch, especially created to mess around in, before I mess up either Branch1 or Branch2)

In my 3rd branch I first tried the following:

git merge feature/Branch1

but this gave the following error:

fatal: 'feature/Branch1' does not point to a commit

I next did a commit -a in my Branch1 and tried again, but it keeps giving the same error.

What am I doing wrong? What should I do to merge the code from - in this case - Branch1 with Branch3?

Git Solutions


Solution 1 - Git

First, checkout to your Branch3:

git checkout Branch3

Then merge the Branch1:

git merge Branch1

And if you want the updated commits of Branch1 on Branch2, you are probaly looking for git rebase

git checkout Branch2
git rebase Branch1

This will update your Branch2 with the latest updates of Branch1.

Solution 2 - Git

  1. git checkout [branchYouWantToReceiveBranch] - checkout branch you want to receive branch
  2. git merge [branchYouWantToMergeIntoBranch]

Solution 3 - Git

To merge one branch into another, such as merging "feature_x" branch into "master" branch:

git checkout master

git merge feature_x

This page is the first result for several search engines when looking for "git merge one branch into another". However, the original question is more specific and special case than the title would suggest.
It is also more complex than both the subject and the search expression. As such, this is a minimal but explanatory answer for the benefit of most visitors.

Solution 4 - Git

You can use these commands:

git checkout <the branch you you want to merge in to>

git merge <the branch you want contents from>

Solution 5 - Git

Just in case you arrived here because you copied a branch name from Github, note that a remote branch is not automatically also a local branch, so a merge will not work and give the "not something we can merge" error.

In that case, you have two options:

git checkout [branchYouWantToMergeInto]
git merge origin/[branchYouWantToMerge]

or

# this creates a local branch
git checkout [branchYouWantToMerge]

git checkout [branchYouWantToMergeInto]
git merge [branchYouWantToMerge]

Solution 6 - Git

A slightly more long winded approach, but it works none-the-less:

In branch 3:

git fetch origin Branch1
git merge --no-ff origin/Branch1

At this point merge conflicts may occur, save all changes made to files containing merge conflicts

git add -A
git commit -m "Merge"
git push

DONE

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
QuestionNemelisView Question on Stackoverflow
Solution 1 - GitgabraView Answer on Stackoverflow
Solution 2 - GitAndrew KoperView Answer on Stackoverflow
Solution 3 - Gitmichael_teterView Answer on Stackoverflow
Solution 4 - GitAbraams gtrView Answer on Stackoverflow
Solution 5 - GitSimone GianniView Answer on Stackoverflow
Solution 6 - GitdylanwhittakerView Answer on Stackoverflow