Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git hook when a new branch is created and/or pushed

Tags:

git

I want to hook into the creation of a new branch. Either when the branch is created or when the first push is done. Is one or both possible?

like image 905
Rasmus Christensen Avatar asked Jan 12 '13 20:01

Rasmus Christensen


People also ask

Do git hooks get pushed?

No. Hooks are per-repository and are never pushed.

What is git pre Push hook?

The pre-push hook runs during git push , after the remote refs have been updated but before any objects have been transferred. It receives the name and location of the remote as parameters, and a list of to-be-updated refs through stdin .


Video Answer


2 Answers

The accepted answer says to use the update hook. I'm not sure that helps everyone, as that is a server-side hook. It will work if you push your new branch, but what about keeping it purely local?

I'm creating local branches that are never pushed, so I'm using post-checkout instead. After creating your branch, aren't you typically going to check it out before doing anything else with it? When I detect a new branch, I modify it and add a commit automatically. After that, I'm able to determine if this is a new branch on a checkout by virtue of whether it has a commit history.

Here's how I do it (my hooks are in bash):

     true=1     false=0          isNewBranch()     {            local logQuery=$(git log --all --not $(git rev-list --no-walk --exclude=refs/heads/$(getBranchName) --exclude=HEAD --all))           if [ -z $logQuery ]; then              echo $true         else             echo $false         fi       }          getBranchName()     {         echo $(git rev-parse --abbrev-ref HEAD)     } 
like image 68
BuvinJ Avatar answered Sep 19 '22 00:09

BuvinJ


It's the update hook, it gets a zero sha for new refs, branches will say heads not tags

like image 21
jthill Avatar answered Sep 19 '22 00:09

jthill