嘿,我想创build一个这样的git仓库:
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
但我想要在terminal(Ubuntu)中使用别名来做,就像
alias newRepo = curl -u 'USER' https://api.github.com/user/repos -d '{"name":"$1"}'; git remote add origin git@github.com:USER/$1.git;
所以在terminal后我input:
newRepo test
它创build回购并添加远程“起源”。
你可以这样做:
首先创建一个脚本newRepo :
#!/bin/bash user="$1" reponame="$2" if [ "$user" = "" ]; then read -p "Enter Github username: " user fi if [ "$reponame" = "" ]; then read -p "Enter Github Repository Name: " reponame fi curl -u "$user" https://api.github.com/user/repos -d "{\"name\":\"$reponame\"}"
然后制作别名:
alias newRepo=/pathtothenewReposcript
现在你可以使用:
newRepo username reponame
在github中创建一个新的仓库。
你可以在“ 用于显示GitHub提交url的Git别名 ”中找到涉及shell函数的git别名的好例子。
诀窍是在输入git config alias命令时找到单引号/双引号/转义的正确组合。
在你的情况下,考虑到你有一个'!',你不能用双引号括住你的git配置别名参数,如果你在bash中( 双引号内没有'!' )
这给你留下$
单引号( $'...'
),它接受' !
',并逃脱单引号(再次在bash中,这将在zsh更容易):
举个例子:
vonc@bigvonc MINGW64 /c/Users/vonc $ git config --global --replace-all alias.newrepo2 '!f() { echo \'e\'; }; f' bash: syntax error near unexpected token `}' vonc@bigvonc MINGW64 /c/Users/vonc $ git config --global --replace-all alias.newrepo2 $'!f() { echo \'e\'; }; f'
最后的命令是:
vonc@bigvonc MINGW64 /c/Users/vonc $ git config --global --replace-all alias.newrepo $'!f() { curl -u \'USER\' https://api.github.com/user/repos -d \'{"name":"$1"}\'; git remote add origin git@github.com:USER/$1.git; }; f'
这就是说,如果你输入curl -u 'USER' https://api.github.com/user/repos -d '{"name":"xxx"}'
,它会要求你输入' USER
”。
这意味着:
USER
'应该是$(whoami)
git config
多命令别名似乎不支持很好地等待输入参数在标准输入… 您不能将参数传递给别名。 创建一个bash函数。