使用来自终端的别名创建一个 git 存储库

Create a git repo using an alias from terminal

嘿,我想像这样创建一个 git 存储库:

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'

但我想在终端 (Ubuntu) 中使用别名,例如

alias newRepo = curl -u 'USER' https://api.github.com/user/repos -d '{"name":""}';
                git remote add origin git@github.com:USER/.git;

所以在终端输入之后:

newRepo test

它创建了 repo 并添加了远程 "origin"。

您不能将参数传递给别名。创建一个 bash 函数。

您可以在“Git alias for displaying the GitHub commit url”中找到涉及 shell 函数的 git 别名的很好示例。

诀窍是在键入 git 配置别名命令时找到单个 quotes/doubles quotes/escape 的正确组合。

在你的情况下,考虑到你有一个 '!',如果你在 bash (no '!' within double quotes)[ 中,你不能用双引号将 git 配置别名参数括起来=23=]

剩下 $ single quote ($'...'),它接受 '!',并转义单引号(同样在 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":""}\'; git remote add origin git@github.com:USER/.git; }; f'

也就是说,如果您键入 curl -u 'USER' https://api.github.com/user/repos -d '{"name":"xxx"}',它会要求您输入“USER”的密码。

这意味着:

  • 也许“USER”应该是$(whoami)
  • 并且... git config 多命令别名似乎不支持等待在标准输入中输入参数...

你可以这样做:

首先创建脚本newRepo:

#!/bin/bash
user=""
reponame=""
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 中创建一个新存储库。