Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge the current branch into another branch

I have two branches, master and dev. I always work on dev and only check code into the master branch once it's been approved for production use. When I do so, I have to do the following:

git checkout master
git merge dev
git checkout dev

That's awfully verbose, and since I do it frequently, I'd like to minimize it. Is there any one git command I can use to merge from my current branch dev to the other branch master without having to checkout the master branch first? Something maybe like:

git merge dev to master

would be awesome. I looked through the git documentation and didn't see anything.

like image 940
Chris Avatar asked Sep 08 '10 21:09

Chris


People also ask

How do I merge one branch to another branch?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

How do I merge a branch from another branch in GitHub?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.

What is the command to merge the current branch with another branch?

The "merge" command is used to integrate changes from another branch. The target of this integration (i.e. the branch that receives changes) is always the currently checked out HEAD branch.

How do I merge an already merged branch?

You can do it like this: git checkout missing-commits git checkout -b correct-merge git merge D # do it right, this time around! git checkout master git checkout -b correct-master git rebase --onto correct-merge wrong-merge correct-master # have fun with the mother of all rebases!


8 Answers

1. Add a remote alias for your local repository, ex:

git remote add self file:///path/to/your/repository

(Or on windows git remote add self C:\path\to\your\repository)

2. Push to the self remote, ex:

git push self dev:master
like image 176
zerome Avatar answered Oct 02 '22 19:10

zerome


The current highest-voted answer by @zerome is a good one, but is a bit needlessly verbose.

In the base of your git repo you can just do this: git push . dev:master

A more generalised solution that would work anywhere in the tree would be:

git push $(git rev-parse --show-toplevel) dev:master
like image 29
Kevin Lyda Avatar answered Oct 02 '22 20:10

Kevin Lyda


Your best bet would be to just use an alias, placed in your global gitconfig (~/.gitconfig):

[alias]
    merge-to = "!f() { git checkout $1 && git merge $2 && git checkout -; }; f"

so that you can invoke it from any repository as

git merge-to master dev
like image 27
Cascabel Avatar answered Oct 02 '22 19:10

Cascabel


A little modification from Jefromi alias that doesn't require you to type in the current branch.

So you use it like: git merge-to dev.

This will switch over to the dev branch, merge it with CURRENT and then will switch back.

For example, assuming you are on master branch, it will merge the master into dev and you will still be on the master.

It definitely goes to my dotfiles :)

[alias]
  merge-to = "!gitmergeto() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git checkout $1 && git merge $tmp_branch && git checkout $tmp_branch; unset tmp_branch; }; gitmergeto"
like image 35
Dmytrii Nagirniak Avatar answered Oct 02 '22 20:10

Dmytrii Nagirniak


This is old, but...

Combining the solutions from @kevin-lyda and @dmytrii-nagirniak above. this alias merges the current branch into the specified branch. It uses the remotes method with and uses git commands to get the context.

[alias]
    merge-to = "!gitmergeto() { git push \"`git rev-parse --show-toplevel`\" `git rev-parse --abbrev-ref HEAD`:$1; } && gitmergeto"

To be used like:

git merge-to other-branch-name
like image 24
tField Avatar answered Oct 02 '22 20:10

tField


To merge the current branch into another branch without checking out the other branch:

Fast-forward merge

This is really easy. By definition, a fast-forward merge simply means the branch pointer is moved ahead in the commit tree. So all you need to do is just simulate that:

git branch -f master dev

Caveats: This assumes that master points to a commit that is also in dev branch or some other branch. If it doesn't, you risk losing work! Unlike git merge which will create a merge commit (or complain) when fast-forward is not possible, this method silently forces the branch pointer to point to another commit.

This also assumes you're the only one working on the repo, and/or you know what you're doing.

Tip: If you did a git fetch and you have new commits in origin/master, you can move the master branch without checking out using:

git branch -f master origin/master

Merge via merge commit

This is not always possible. To create a merge commit, you have to do a merge operation. And to do a merge operation, you should have commits in the other branch that are not in the current branch.

If you do have commits in the master branch which are not in dev branch, you can:

Disclaimer: This is merely a proof-of-concept, just to show it's sometimes possible to do a merge to the other branch without checking out. If you would want to use it everyday, you probably want to make an alias for it using shell redirection or make a shell script for it. Then again, you can also make a shell script for the shorter process shown in the question.

git checkout -b temp
git merge --no-ff -e master
git branch -f master temp
git checkout dev
git branch -D temp

Explanation:

  1. Check out a temporary branch that points to the same commit as current branch.
  2. Merge master into the temporary branch and launch commit message editor. If you want the merge commit to look like you had merged the dev branch into master, edit it from this:

    Merge branch 'master' into temp
    

    to this:

    Merge branch 'dev'
    

    Tip: You can use -m "Merge branch 'dev'" instead of -e to be quicker.

  3. Update the master branch pointer to point to the merge commit.
  4. Check out the dev branch.
  5. Force delete the temporary branch.

This still touches your working tree, but minimally so. It doesn't roll back the tree all the way to state of the original master just to bring in the development changes once again. Some may not care, but for others it may be important.

like image 21
ADTC Avatar answered Oct 02 '22 19:10

ADTC


A lot of times you're coming from the branch you would like to merge the current branch into. In that case you could do:

git co - && git merge @{-1}

for example:

git checkout somebranch      // (while on master)

// add some commits

git co - && git merge @{-1}  // will merge somebranch into master
like image 37
Tieme Avatar answered Oct 02 '22 20:10

Tieme


My solution is similar to the other answers, with the following differences:

  • the function is split into multiple lines for readability
  • the function calls set -ex so each command is printed and if command fails function exits right away
  • alias passes its arguments except the first (target branch) to git merge
  • the function includes a null command : git merge which makes sure tab-completion works with some shell setups (e.g. gitfast from oh-my-zsh)
[alias]
  merge-to = "!f() { : git merge ; \
      set -ex ; \
      local this=$(git rev-parse --abbrev-ref HEAD) ; \
      local target=$1 ; \
      shift ; \
      git checkout $target ; \
      git merge $this \"$@\" ; \
      git checkout $this ; \
    } ; f"
like image 22
artm Avatar answered Oct 02 '22 21:10

artm