Jeroen van Dijk
Jeroen van Dijk

Reputation: 1029

Alias for "git checkout main_branch; git merge topic_branch"?

In our team we are using a development, staging and a master branch plus branches for issues. With this workflow I find myself doing a lot of the following:

For example in the development branch:

git checkout staging; git merge development

Does anyone have a simple alias for this?

It should be something like:

merge_with master

would do:

git checkout master; git merge previous_branch

Upvotes: 2

Views: 1168

Answers (6)

Jujhar Singh
Jujhar Singh

Reputation: 3770

I'm forever doing this, especially since on one of our projects we have several subtly different production branches. Deploying a fix/update can make your finger's tired.

For all you Windows bods out there: I've just created a merge.bat file somewhere in my %path% and it contains:

git checkout %1 && git merge %2 --verbose

then all I do is

merge foo bar

and it runs:

git checkout foo 
git merge bar --verbose

Upvotes: 0

Jeroen van Dijk
Jeroen van Dijk

Reputation: 1029

Here is sort of what I mean except it does not work yet.

branch="$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')"; echo "git checkout master; git merge $branch"

First it finds out what the current branch is. Then it uses that to determine which branch to merge with.

So the next thing is to find out how to append an alias argument...

Upvotes: 0

Jeroen van Dijk
Jeroen van Dijk

Reputation: 1029

Thanks for all your help. None of all did exactly what I wanted but did get me in the right direction.

I have added the following to ~/.bash_login. For every git repository "merge_to somebranch" will do git checkout somebranch; git merge previousbranch.

# Git functions to quickly merge branches
# e.g. merge_to staging
function current_branch { ref=$(git symbolic-ref HEAD 2> /dev/null); echo ${ref#refs/heads/} }
function merge_to       { branch=`eval current_branch`; git checkout "$@" && git merge $branch }

Upvotes: 0

iamamac
iamamac

Reputation: 10126

git merge is always performed on the current branch in case of conflict, so you have to checkout the staging branch first.

However, if linear history is permitted, you can use git rebase development staging.

As Michael said, you can always make an alias.

EDIT: Add this to .git/config will do it

[alias]
    merge_to = !sh -c 'CURRENT=$(git symbolic-ref HEAD) && git checkout $1 && git merge $CURRENT' -

Upvotes: 1

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143279

git config alias.thething '!git checkout staging ; git merge $1'

should give you one as

git thething development

Upvotes: 0

Antony Hatchkins
Antony Hatchkins

Reputation: 34034

git checkout master; git merge HEAD@{1}

You can assign an alias to that using:

git config alias.mm '!git checkout master; git merge HEAD@{1}'

So that git mm will merge your current branch into master.

Upvotes: 2

Related Questions