Bash中Git别名的变量名不正确
我的全局Git配置中有以下Git别名Bash中Git别名的变量名不正确,git,bash,git-alias,Git,Bash,Git Alias,我的全局Git配置中有以下Git别名 pushnew = !f() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git push --set-upstream origin $tmp_branch; unset $tmp_branch; }; f 运行时,我得到以下输出: * [new branch] bug/graphs -> bug/graphs Branch bug/graph
pushnew = !f() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git push --set-upstream origin $tmp_branch; unset $tmp_branch; }; f
运行时,我得到以下输出:
* [new branch] bug/graphs -> bug/graphs
Branch bug/graphs set up to track remote branch bug/graphs from origin.
f() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git push --set-upstream origin $tmp_branch; unset $tmp_branch; }; f: 1: unset: bug/graphs: bad variable name
我已经看到了这一点,但如果这是解决方案,那么如何在Git别名的上下文中应用修复程序呢?应该是:
unset tmp_branch
如果您尝试取消设置
$tmp\u branch
,shell将替换$tmp\u branch
的值,尝试取消设置bug/graphs
我想在这种情况下问题不大,但您应该始终引用shell变量,例如git push--设置上游原点“$tmp\u branch”
。当变量的值和全局扩展中有空格时,当存在诸如*
之类的字符时,这可以防止分词。如果要查找当前分支名称,则不希望是grep
pinggit branch
的输出。您希望使用git symbolic ref HEAD
(也可能使用--short
参数)。参见。Tom,Etan:感谢这些建议-肯定会改进它并有助于将来的维护。没有必要取消设置tmp_分支
,因为它是在Git创建的子shell中定义的,用于评估别名。也不需要出口,;您只是在定义它的同一个shell中扩展它,而不是子进程f(){git push--设置上游原点“$(git symbolic ref HEAD)”;}
应该足够了。