I often have to create new git repos and while the instructions that Github provides are fine, I find myself typing the same commands over and over again. So I decided to create a handy function that I can call from the command line to create a new repo and push it to Github.
First of all open your .bashrc
file (I use ~/.zshrc
, but wherever you keep your aliases is fine) and add the following function:
gitinit() (
if [[ ! "$1" ]] ; then
echo "You must supply a git origin e.g. [email protected]:yourusername/your-repo.git"
echo "If you haven't created a repo yet, do that first -> https://github.com/new"
return 0
fi
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin $1
git push -u origin main
)
Source the file to make the function available in your current shell session:
source ~/.zshrc # or whatever your file is called
You can also restart your terminal if you prefer.
Now you can create a new repo and push it to Github with a single command:
gitinit [email protected]:yourusername/your-repo.git
Bear in mind that you’ll still need to create the repo on Github first.
Maybe I’ll automate that the next time I need to do it 🙂.