git alias for merging current branch with master

Solution 1:

If you're going to use positional parameters in git aliases, be sure to specify sh -c. The following worked for me locally:

ff = !sh -c 'git checkout master && git merge "$1" && git checkout "$1"' -

(dead link) The "why" relates to the way git parses the alias commands and is summed up in this gmane thread.

The - at the end of the command signals sh that option processing is finished. From man sh:

A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.

If it weren't present, it would introduce a bug in the alias, by treating $1 as an option on the sh command instead of as a positional argument. Consider this trivial example:

$ sh -c 'echo $1' foo          # equivalent to 'echo' without argument

$ sh -c 'echo $1' - foo        # will pass 'foo' to the string as $1
foo

In the first case, 'foo' was consumed by sh as an option. In the second, it's correctly interpreted as $1 on the string.

Solution 2:

Here's a version with which you don't need to specify the current branch and can choose any target branch:

merge-into = !sh -c '_CURRENT_BRANCH=$(git symbolic-ref --short HEAD) && git checkout $1 && git merge $_CURRENT_BRANCH && git checkout $_CURRENT_BRANCH' -

Usage (from my-branch):

git merge-into master