1

Is it possible to merge a git branch into another from one line.

Let's suppose i want to merge br1 into master branch.

I should do this:

git checkout master
git merge br1

Is there a way to type something like this:

git merge br1 master

I mean type a command which will works fine whatever the current branch is

Thanks

Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • I don't think this behavior is available with any existing git commands. You can create an alias to make it, though. – Code-Apprentice Jun 21 '18 at 20:20
  • See, e.g., SakoDaemon's answer. Note that this will leave you on branch `master`, regardless of which branch you were on before, if the `git checkout` succeeds. It's also wise to verify that the `git checkout` *did* succeed before running the `git merge`. – torek Jun 21 '18 at 21:39
  • Possible duplicate of [Merge, update, and pull Git branches without using checkouts](https://stackoverflow.com/questions/3216360/merge-update-and-pull-git-branches-without-using-checkouts) – Dherik Jun 21 '18 at 22:23

2 Answers2

0

Add the following to your ~/.bash_aliases file (this way you can create lots of shortcuts for better workflow):

gitmerge() {
    #merge first argument into second
    git checkout "$2"
    git merge "$1"
}
SakoDaemon
  • 973
  • 1
  • 6
  • 21
0

Git alias:

git config --global alias.cm '!f() { git checkout $1 && git merge $2; }; f'

Usage example:

git cm master br1  # checkout master, merge br1

To merge in reverse direction exchange $1 and $2.

To return to the branch that was checked out before merge:

git config --global alias.cm '!f() { prev_br=`git symbolic-ref HEAD` && git checkout $1 && git merge $2 && git checkout $prev_br; }; f'
phd
  • 82,685
  • 13
  • 120
  • 165