问题
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