1

Is there any hook available locally in local machine, triggered whenever we switch through branch from command line?

Also how do we know which branch is we are working on by looking .git folder ?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Samundra Khatri
  • 997
  • 1
  • 9
  • 18

1 Answers1

2

You know which branch you are working on with the git branch command.
.git/HEAD references the current commit (usually, the name of a branch, but it can be a tag or a commit, in case of a detached HEAD)

Since changing branch involves a checkout, you can setup a post-checkout hook, which will be triggered each time you switch branch.

This hook is invoked when a git checkout is run after having updated the worktree.
The hook is given three parameters:

  • the ref of the previous HEAD,
  • the ref of the new HEAD (which may or may not have changed),
  • and a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0).
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hello Vonc, Thanks for your help, I also noticed that in .git folder there is HEAD file where the ref of which branch is kept if we keep watching this i think we can also get the working branch.. actually i want other programs like from nodejs to watch about it so – Samundra Khatri Jun 26 '16 at 07:46
  • @SamundraKC calling `git branch` is easier than trying to poke around in the git internals. But yes, reading .git/HEAD content can give you the name of the current branch (if it is not a detached HEAD) – VonC Jun 26 '16 at 07:47