如何将除前两个之外的所有 bash 参数设置为 git 别名
How to set all bash args, except the first two, to git alias
我想创建一个 git 别名,它将使用我定义的模板创建提交消息。
这是模板:
"[$firstVariable] $secondVariable: $thirdVariable"
- 第一个是分行号,
- 第二个是类型的提交,
- 最后一个参数是 提交消息的主题
示例:
"[1000] feat: add new sales controller"
这是我在 .gitconfig
文件中的 bash 函数:
[alias]
c = "!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['"$branchNumber"'] '"$type"': '"$subject"' '; }; f"
我可以得到前两个变量,但是第三个变量在终端中出现错误。
如果我执行这个命令,我得到这个:
命令:$ git c 1000 feat add new sales controller
Return:
error: pathspec 'sales' did not match any file(s) known to git.
error: pathspec 'controller ' did not match any file(s) known to git.
观察:
- 如果我在
$subject
处添加回显功能,命令 return:
new sales controller
error: pathspec 'sales' did not match any file(s) known to git.
error: pathspec 'controller ' did not match any file(s) known to git.
- 如果我只在
$subject
处添加一个参数,则别名有效
Git "eats" 解析 .gitconfig
行时的一级双引号。因此,当您写道:
"!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['"$branchNumber"'] '"$type"': '"$subject"' '; }; f"
shell 看到的是:
!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['$branchNumber'] '$type': '$subject' '; }; f
请注意,唯一保留的引号是单引号,因此当 $subject
扩展时,它变成单独的单词:
git c 1000 feat add new sales controller
变成:
git
commit
-m
[1000] feat: add
new
sales
controller
(我将每个 "word" 单独画在一条线上)。 (controller
一词后还有一个空格,因为它受到单引号的保护。)如果您愿意,另一种出于 Whosebug 发布目的绘制它的方法可能是:
git
commit
-m
[1000] feat: add
new
sales
controller
也就是说,命令本身将这些中的每一个视为一个单独的 "word":因此提交日志消息是 [1000] feat: add
; new
、sales
和 controller
被视为 pathspec 参数。
一个简单的解决方法是用反斜杠双引号替换您希望保留的每个双引号:
c = "!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['\"$branchNumber\"'] '\"$type\"': '\"$subject\"' '; }; f"
为了使其更具可读性,请注意双引号足以表示整个 -m
论点(并且此处的各个地方都不需要大括号):
c = "!f() { branchNumber=; type=; shift 2; subject=$*; git commit -m \"[$branchNumber] ${type}: $subject\"; }; f"
我想创建一个 git 别名,它将使用我定义的模板创建提交消息。
这是模板:
"[$firstVariable] $secondVariable: $thirdVariable"
- 第一个是分行号,
- 第二个是类型的提交,
- 最后一个参数是 提交消息的主题
示例:
"[1000] feat: add new sales controller"
这是我在 .gitconfig
文件中的 bash 函数:
[alias]
c = "!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['"$branchNumber"'] '"$type"': '"$subject"' '; }; f"
我可以得到前两个变量,但是第三个变量在终端中出现错误。
如果我执行这个命令,我得到这个:
命令:$ git c 1000 feat add new sales controller
Return:
error: pathspec 'sales' did not match any file(s) known to git.
error: pathspec 'controller ' did not match any file(s) known to git.
观察:
- 如果我在
$subject
处添加回显功能,命令 return:
new sales controller
error: pathspec 'sales' did not match any file(s) known to git.
error: pathspec 'controller ' did not match any file(s) known to git.
- 如果我只在
$subject
处添加一个参数,则别名有效
Git "eats" 解析 .gitconfig
行时的一级双引号。因此,当您写道:
"!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['"$branchNumber"'] '"$type"': '"$subject"' '; }; f"
shell 看到的是:
!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['$branchNumber'] '$type': '$subject' '; }; f
请注意,唯一保留的引号是单引号,因此当 $subject
扩展时,它变成单独的单词:
git c 1000 feat add new sales controller
变成:
git
commit
-m
[1000] feat: add
new
sales
controller
(我将每个 "word" 单独画在一条线上)。 (controller
一词后还有一个空格,因为它受到单引号的保护。)如果您愿意,另一种出于 Whosebug 发布目的绘制它的方法可能是:
git
commit
-m
[1000] feat: add
new
sales
controller
也就是说,命令本身将这些中的每一个视为一个单独的 "word":因此提交日志消息是 [1000] feat: add
; new
、sales
和 controller
被视为 pathspec 参数。
一个简单的解决方法是用反斜杠双引号替换您希望保留的每个双引号:
c = "!f() { branchNumber=; type=; shift; shift; subject=${*}; git commit -m '['\"$branchNumber\"'] '\"$type\"': '\"$subject\"' '; }; f"
为了使其更具可读性,请注意双引号足以表示整个 -m
论点(并且此处的各个地方都不需要大括号):
c = "!f() { branchNumber=; type=; shift 2; subject=$*; git commit -m \"[$branchNumber] ${type}: $subject\"; }; f"