Create git repository using alias from terminal

Hi I want to create a git repository like this:

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

      

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

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 I type in the terminal:

newRepo test

      

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

+3


source to share


3 answers


You can do it like this:

First, create a script 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 an alias:



alias newRepo=/pathtothenewReposcript

      

Now you can use:

newRepo username reponame

      

to create a new repository on github.

+3


source


You can find good examples of git aliases using the shell function in Git alias to display the GitHub commit url . "

The trick is to find the correct combination of single quotes / double quotes / escape when entering the git config alias command.

In your case, given that you have a "!", You cannot surround your git config aliases parameter with double quotes if you are in bash ( no '!' In double quotes )

This leaves you with a single quote ( ) that takes ' ', and strips off the single quotes (again in bash, which would be easier in 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'

      



Last command:

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'

      

In this case, if you typed curl -u 'USER' https://api.github.com/user/repos -d '{"name":"xxx"}'

, he will ask for a password for " USER

".

It means:

  • maybe ' USER

    ' shoud be$(whoami)

  • and ... a git config

    multiple command alias doesn't seem to support waiting for an argument to be entered into stdin ...
+2


source


You cannot pass parameters to aliases. Create a bash function.

0


source







All Articles