请遵守这些 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 ≡]>
最佳答案
当您将参数传递给别名时,Git 将该参数放在别名的扩展之后。
即使您使用 $<em>number</em>
也是如此在扩展中。因此,扩展文本:
!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
(出于展示和讨论的目的,我将它分成了三行——在内部,它们都是一大行)。
第一条和第二条命令是 and do what you want.
第三个命令表示推送两个 引用,带有-u
选项集,远程origin
.第一个引用是 shelve_pbi405783
,这就是你想要的;第二个引用是 pbi405783
.
Git 成功推送shelve_pbi405783
并将其上游设置为 origin/shelve_pbi405783
, 但无法推送 pbi405783
因为它不存在。所以它会打印错误消息并在执行完您想要的操作后停止。
您可以简单地忽略该错误,但最好避免它。为避免这种情况,您必须编写一个 shell 命令来接受并处理额外的参数。通常的方法是写一个shell函数:
'!f() { ...; } f'
然后 shell 函数本身接收参数,如果它包含文字 $<em>number</em>
字符串,那些扩展适当的参数。 (所以现在 Git 是否扩展 $1
并不重要。)
关于git - 为什么带有推送的 git 别名不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50053343/