为什么使用push的git别名无法工作?

3
请注意以下Git命令:

C:\DeploymentSmokeTests [master ↑1]> git lg -n 2
* bd2e150 |  (HEAD -> master) Support Sql Server 2012. Limit the number of tested namespaces. (19 minutes ago) [Mark Kharitonov]
* 2d57b65 |  (origin/master) Merged PR 538: Implement the Schedule Jobs deployment test (36 minutes ago) [Kharitonov, Mark]
C:\DeploymentSmokeTests [master ↑1]> git config alias.shelve
!git branch shelve_$1;git checkout shelve_$1;git push -u origin shelve_$1
C:\DeploymentSmokeTests [master ↑1]> git shelve pbi405783
Switched to branch 'shelve_pbi405783'
error: src refspec pbi405783 does not match any.
error: failed to push some refs to 'http://tdc1tfsapp01:8080/tfs/defaultcollection/DFDevOps/_git/DFDeploymentSmokeTests'
C:\Dayforce\DevOps\DFDeploymentSmokeTests [shelve_pbi405783]> git lg -n 2
* bd2e150 |  (HEAD -> shelve_pbi405783, master) Support Sql Server 2012. Limit the number of tested namespaces. (19 minutes ago) [Mark Kharitonov]
* 2d57b65 |  (origin/master) Merged PR 538: Implement the Schedule Jobs deployment test (37 minutes ago) [Kharitonov, Mark]
C:\DeploymentSmokeTests [shelve_pbi405783]>

解释:

  • 我有一个提交要推送到服务器
  • 我有一个名为shelve的别名,它创建一个分支,检出它并带有跟踪的推送
  • 然而,尝试使用这个别名时,我遇到了一个错误,我不明白。由于某些原因,它拒绝推送。

请注意,单独运行push是可以的:

C:\DeploymentSmokeTests [shelve_pbi405783]> git push -u origin shelve_pbi405783
Counting objects: 9, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 2.58 KiB | 2.58 MiB/s, done.
Total 9 (delta 8), reused 0 (delta 0)
remote: Analyzing objects... (9/9) (4 ms)
remote: Storing packfile... done (23 ms)
remote: Storing index... done (59 ms)
To http://tdc1tfsapp01:8080/tfs/defaultcollection/DFDevOps/_git/DFDeploymentSmokeTests
 * [new branch]      shelve_pbi405783 -> shelve_pbi405783
Branch 'shelve_pbi405783' set up to track remote branch 'shelve_pbi405783' from 'origin'.
C:\DeploymentSmokeTests [shelve_pbi405783 ≡]>

错误:src refspec pbi405783与任何内容都不匹配。似乎“pbi405783”不是有效的refspec。 - gzh
@gzh,但是别名不应该推送pbi405783,它应该推送_shelve_pbi405783_。为什么前缀shelve_没有被识别? - Chris
1个回答

4
当您将参数传递给别名时,Git会将该参数放在别名扩展之后。
即使您在扩展中使用$number,这个规则仍然适用。因此,扩展文本如下:
!git branch shelve_$1;git checkout shelve_$1;git push -u origin shelve_$1

当被调用时变为:
git branch shelve_pbi405783;
git checkout shelve_$pbi405783;
git push -u origin shelve_pbi405783 pbi405783

我已将此内容拆分成三行以便于显示和讨论。在内部,它们都是一个大命令。

第一和第二个命令是您想要的并且执行了您想要的操作。

第三个命令表示要推送-u选项设置的两个引用到远程origin。第一个引用是shelve_pbi405783,这就是您想要的;第二个引用是pbi405783

Git成功推送了shelve_pbi405783并将其上游设置为origin/shelve_pbi405783,但无法推送pbi405783,因为它不存在。所以它打印出该错误消息并停止执行,完成了您想要的操作。

您可以简单地忽略此错误,但最好避免它。要避免它,您必须编写一个接受并处理额外参数的shell命令。通常的方法是编写一个shell函数

'!f() { ...; } f'

shell函数本身接收参数,如果包含文字$number字符串,则会扩展相应的参数。(因此,现在无论Git是否扩展$1都无关紧要。)


“'!f() { ...; } f'” 语法是特定于 shell 的吗?也就是说,它是 Bash 的语法吗?因为我在 Windows 上使用的是 PowerShell。 - mark
实际上,我有一个更简单的解决方案 - 只需在末尾添加echo命令,这样别名就变成了!git branch shelve_$1;git checkout shelve_$1;git push -u origin shelve_$1;echo - mark
是的,这是 POSIX-shell(bash/sh)中“定义函数”然后“调用函数”的语法。你的“echo”技巧也可以使用,只是让它打印一些无意义的东西有点烦人 :-) - torek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接