Retrieving branch names from within a post-checkout hook

↘锁芯ラ 提交于 2019-12-10 20:41:19

问题


When switching from one branch to another in Git, is there any way to retrieve the names of both branches from within the post-checkout hook?

So assuming I were to run the following two commands:

$ git branch
* branch_a
  branch_b
  master

$ git checkout branch_b
Switched to branch 'branch_b'

I'd be looking for the following two strings (in the post-checkout hook):

"branch_a"
"branch_b"

回答1:


You can use git reflog inside your hook to get the previous and next branch. Here a simple working example:

#!/bin/bash

# $6 = previous branch, $8 is next branch
git reflog | awk 'NR==1{ print $6 " -> " $8; exit }'

EDIT: updated answer. Here the previous:

This hook is given with three arguments:

  • the ref of the previous HEAD
  • the ref of the new HEAD
  • 1 if it was a branch checkout, 0 for a file checkout.

With the two firsts you should have enough information for what you are trying to do.



来源:https://stackoverflow.com/questions/25590267/retrieving-branch-names-from-within-a-post-checkout-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!