为什么git alias with push不起作用?

时间:2018-04-26 23:44:13

标签: git

请观察这些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]>

说明:

  • 我有一个提交到服务器的提交
  • 我有一个名为搁置的别名,它会创建一个分支,检查并推送跟踪
  • 但是,尝试使用别名我得到一个错误,我不明白。出于某种原因,它拒绝推动。

请注意,单独运行推送可以正常工作:

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 ≡]>

1 个答案:

答案 0 :(得分:3)

当你将一个参数传递给别名时,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并不重要。)