如何使用touch
命令从 PowerShell 创建“.ignore”文件?
我可以在 git Bash (MINGW64) 中使用该命令
me@my_computer MINGW64 ~/stuff (master)
$ touch .gitignore
...但如果能继续使用 PowerShell,并解决 Windows 不希望文件名以句点开头的问题,那就太好了。Bash 帮助/手册/信息页面无法识别touch
。FWIW,我正在关注本教程并对答案中的链接进行了一些研究这里但还没有找到答案。当我touch .gitignore
在 PowerShell 中尝试时,我收到此错误消息:
PS C:\Users\_user_name_\stuff> touch .gitignore
The term 'touch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:6
+ touch <<<< .gitignore
+ CategoryInfo : ObjectNotFound: (touch:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
答案1
将此代码添加到我的 .ps1 配置文件即可解决问题:C:\Users\user_name\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
<#
The following function and alias assignment
are for use with git to create files which
start with a period, e.g. .gitignore
#>
function touch_file
{new-item $args[0] -itemtype file}
new-alias -name touch -value touch_file
现在我可以进入touch .gitignore
PowerShell 提示符,而 Windows 不会因为以句点开头的文件名而犹豫,也不会要求我告诉它正在生成什么类型的对象:
PS C:\Users\user_name> touch .gitignore
Directory: C:\Users\user_name
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 8/16/2016 11:41 PM 0 .gitignore
距离让我的笔记本电脑充分响应“做我想做的事!”又近了一步:)