JKiely JKiely - 1 month ago 6
Git Question

Checking current git branch with ifneq in Makefile

I'm trying to get my makefile to check that it's running on the correct branch and throw and error if not.

I'm using

ifneq
to compare them and
git rev-parse --abbrev-ref HEAD
to get the checked out branch, but it will not see them as equal. How can I fix this?

Right now the the code looks like this:

ifneq ($(git rev-parse --abbrev-ref HEAD), master)
$(error Not on branch master)
else
git checkout gh-pages
git merge master
git checkout master
endif


Thanks.

Answer

There is no such make function as $(git ...), so that variable reference expands to the empty string. You're always running:

ifneq (, master)

which will be always true.

You want to use the shell GNU make function:

ifneq ($(shell git rev-parse --abbrev-ref HEAD),master)