390

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?

1
  • 5
    This question is needlessly complex. It should be clearer which branch has the code and which branch we want that code to be merged in.
    – sbhatla
    Oct 18, 2022 at 23:03

6 Answers 6

481

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.

7
  • 1
    (I want the updates of Branch2 to Branch1 ;-) ) Branch2 does see Branch1, but not the other way around when I do a git branch -a in both branches. Hence I cannot do a rebase to Branch2 in Branch1: git rebase Branch2 --> fatal: Needed a single revision
    – Nemelis
    Jul 11, 2016 at 8:40
  • When I do the rebase as git rebase origin/Branch2 it still gives that error message + invalid upstream origin/Branch2
    – Nemelis
    Jul 11, 2016 at 8:45
  • 1
    @Si8 this will add only the commits from Branch1 to Branch3. Branch1 will be kept as is.
    – gabra
    Dec 10, 2017 at 21:53
  • 1
    Thank you so if I am currently in Dev branch and I do a merge of Dev1, it will merge Dev1 to Dev, am I correct?
    – Si8
    Dec 11, 2017 at 18:32
  • 1
    Yes. I recommend trying it. If something breaks, you can go back. That is the purpose of a VCS.
    – gabra
    Dec 11, 2017 at 18:40
126
 git checkout [branchYouWantToReceiveBranch] //checkout branch to receive branch
 git merge [branchYouWantToMergeIntoBranch]
1
  • 2
    Simple and should-be-the-accepted answer to me!
    – Nam G VU
    Oct 26, 2022 at 2:58
73

You can use these commands:

git checkout <the branch you want to merge into>

git merge <the branch you want contents from>
3
  • 2
    This is the fourth answer saying the exact same thing. Am I missing something, or why does this have 43 upvotes?
    – phil294
    Sep 17, 2022 at 16:26
  • 10
    @phil294 this guy says the same thing better. Nov 1, 2022 at 14:27
  • 2
    the readability is strong with this one
    – wintermute
    Nov 30, 2022 at 1:49
57

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

git checkout master

git merge feature_x

* Note that the original branch name is now commonly main instead of master. Choose the correct name based on your situation.

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.

17

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]
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.