从post-checkout钩子中检索分支名称 [英] Retrieving branch names from within a post-checkout hook

查看:450
本文介绍了从post-checkout钩子中检索分支名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在Git中从一个分支切换到另一个分支时,是否有任何方法从post-checkout钩子中检索两个分支的名称?

所以假设我要运行以下两个命令:

  $ git branch 
* branch_a
branch_b
master
$ b $ git checkout branch_b
转换到分支'branch_b'

我会寻找以下两个字符串(在结账后的钩子):

 branch_a
branch_b


解决方案

$ c> git reflog 在你的钩子里得到上一个和下一个分支。
这里有个简单的例子:

$ $ $ $ $ $ $ $ $ $ $ $ $ =前一分支,$ 8是下一分支
git reflog | awk'NR == 1 {print $ 6 - >$ 8;退出}'

编辑:更新了答案。这里是前面的内容:



这个钩子有三个参数:


  • 前一个HEAD的引用

  • 新头的引用

  • 1如果是分支签出,则为0用于文件签出。


有了这两个第一,你应该有足够的信息来做你想做的事情。


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"

解决方案

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.

这篇关于从post-checkout钩子中检索分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆