Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking current git branch with ifneq in Makefile

Tags:

git

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.

like image 570
JKiely Avatar asked Oct 11 '16 18:10

JKiely


People also ask

How do I pull just the current branch?

Git already only pulls the current branch. If you have branch set up as a tracking branch, you do not need to specify the remote branch. git branch --set-upstream localbranch reponame/remotebranch will set up the tracking relationship. You then issue git pull [--rebase] and only that branch will be updated.

How does git keep track of current branch?

Git stores all references under the . git/refs folder and branches are stored in the directory . git/refs/heads. Since branch is a simple text file we can just create a file with the contents of a commit hash.

What does git branch Branch_name do?

Git branch_name Branch is a beauty of git. You can work with coworkers without any interference with them. This will create a local branch with the name branch_name . Then you can switch your branch to this by git checkout branch_name .


1 Answers

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)
like image 140
MadScientist Avatar answered Oct 04 '22 18:10

MadScientist