使用终端的别名创建一个git repo

时间:2023-02-04 01:19:27

Hey there I want to create a git repository like this:

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

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

But I want to do it using a alias in terminal (Ubuntu), like

但是我想在终端(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;

So after in the terminal I type:

所以在终端后我输入:

newRepo test

And it creates the repo and add the remote "origin".

它创建了回购并添加了远程“原点”。

3 个解决方案

#1


You can do that like this:

你可以这样做:

First create a script newRepo:

首先创建一个脚本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\"}"

Then make alias:

然后制作别名:

alias newRepo=/pathtothenewReposcript

Now you can use:

现在你可以使用:

newRepo username reponame

to create a new repository in github.

在github中创建一个新的存储库。

#2


You can find good examples of git alias involving a shell function in "Git alias for displaying the GitHub commit url".

你可以在“用于显示GitHub提交URL的Git别名”中找到涉及shell函数的git别名的好例子。

The trick is to find the right combination of single quotes/doubles quotes/escape, when typing the git config alias command.

诀窍是在键入git config alias命令时找到单引号/双引号/转义的正确组合。

In your case, considering you have a '!', you cannot surround your git config alias parameter with double-quotes if you are in bash (no '!' within double quotes)

在你的情况下,考虑到你有'!',你不能用双引号括起你的git config别名参数如果你在bash中(双引号内没有'!')

That leaves you with $ single quote ($'...'), which accepts '!', and escaped single-quotes (again in bash; that would be easier in zsh):

这将留下$ single引号($'...'),它接受'!',并转义单引号(再次使用bash;这在zsh中会更容易):

As an example:

举个例子:

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'

The final command would be:

最后的命令是:

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'

That being said, if you typed curl -u 'USER' https://api.github.com/user/repos -d '{"name":"xxx"}', it would ask you for the password for 'USER'.

话虽这么说,如果你输入curl -u'USER'https://api.github.com/user/repos -d'{“name”:“xxx”}',它会询问你输入'USER'的密码”。

That means:

  • maybe 'USER' shoud be $(whoami)
  • 也许'用户'应该是$(whoami)

  • and... a git config multiple-command alias doesn't seem to support well waiting for an argument to be entered in stdin...
  • 和...一个git配置多命令别名似乎不支持等待在stdin中输入参数...

#3


You can't pass parameters to aliases. Create a bash function.

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

#1


You can do that like this:

你可以这样做:

First create a script newRepo:

首先创建一个脚本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\"}"

Then make alias:

然后制作别名:

alias newRepo=/pathtothenewReposcript

Now you can use:

现在你可以使用:

newRepo username reponame

to create a new repository in github.

在github中创建一个新的存储库。

#2


You can find good examples of git alias involving a shell function in "Git alias for displaying the GitHub commit url".

你可以在“用于显示GitHub提交URL的Git别名”中找到涉及shell函数的git别名的好例子。

The trick is to find the right combination of single quotes/doubles quotes/escape, when typing the git config alias command.

诀窍是在键入git config alias命令时找到单引号/双引号/转义的正确组合。

In your case, considering you have a '!', you cannot surround your git config alias parameter with double-quotes if you are in bash (no '!' within double quotes)

在你的情况下,考虑到你有'!',你不能用双引号括起你的git config别名参数如果你在bash中(双引号内没有'!')

That leaves you with $ single quote ($'...'), which accepts '!', and escaped single-quotes (again in bash; that would be easier in zsh):

这将留下$ single引号($'...'),它接受'!',并转义单引号(再次使用bash;这在zsh中会更容易):

As an example:

举个例子:

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'

The final command would be:

最后的命令是:

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'

That being said, if you typed curl -u 'USER' https://api.github.com/user/repos -d '{"name":"xxx"}', it would ask you for the password for 'USER'.

话虽这么说,如果你输入curl -u'USER'https://api.github.com/user/repos -d'{“name”:“xxx”}',它会询问你输入'USER'的密码”。

That means:

  • maybe 'USER' shoud be $(whoami)
  • 也许'用户'应该是$(whoami)

  • and... a git config multiple-command alias doesn't seem to support well waiting for an argument to be entered in stdin...
  • 和...一个git配置多命令别名似乎不支持等待在stdin中输入参数...

#3


You can't pass parameters to aliases. Create a bash function.

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