How to Push an Initial Commit To a New GitHub Remote With a Single Shell Command | by Rob Sturgeon | Apr, 2022

Aliases scale back the complexity of utilizing command-line interfaces

Picture by OpenClipart-Vectors from Pixabay

I’m not an enormous fan of CLIs, or Command-Line Interfaces, however this time I made a decision to problem myself by attempting to cut back the hassle of building good supply management practices. I often refuse Xcode’s supply to place new initiatives below supply management, as a result of I’m typically experimenting and don’t essentially desire a report of early prototypes.

Nevertheless it’s surprisingly difficult to get that venture that has no supply management to the purpose the place it not solely has native commits however has additionally been pushed to a distant repository.

Till now…

If you happen to haven’t put in Homebrew already, open Terminal and paste this command:

/bin/bash -c "$(curl -fsSL https://uncooked.githubusercontent.com/Homebrew/set up/HEAD/set up.sh)"

After you have Homebrew, run this to put in the GitHub CLI:

brew set up gh

Run this to log into the GitHub CLI as soon as put in:

gh auth login

I used HTTPS and authenticated with a browser, however it’s possible you’ll select otherwise.

This command will add the one alias we’d like for the GitHub CLI:

gh alias set new 'repo create --private --push --source=.'

This merely creates a non-public distant repository and pushes no matter it finds within the present folder.

Working the gh new command will create the distant and push your commit(s) when you’ve got an area repository already. If you happen to don’t have an area repository the command will fail, citing the truth that the listing just isn’t but a git repository.

Run the next command so as to add a brand new Git alias to your international ~/.gitconfig file:

git config --global alias.new '!new()  cd -- $GIT_PREFIX:-.; if getopts "rf:" arg; then mkdir "$OPTARG"; cd "$OPTARG"; fi; contact README.md; git init; git add .; git commit -m "Preliminary commit"; ; new'

The exclamation level (!) permits using non-git instructions, however it additionally modifications the default location of the shell to the person’s residence folder.

To repair this we merely change listing to GIT_PREFIX:-., which is wherever the git command is being referred to as from.

git new

This new alias lets you create a brand new native repository within the present folder, no matter whether or not it accommodates information or is empty.

git new -f MyNewProject

Working this command creates a brand new subfolder referred to as MyNewProject.

The subfolder argument is handed as $OPTARG, as it’s the first argument after the -f choice letter. If the -f choice just isn’t handed then every other arguments are ignored and the command is run the identical method as git new within the present folder with out making a subfolder.

I’ve thought of varied methods to mix the aliases into one, calling them one after one other, however I feel doing every little thing directly might be overkill in a variety of conditions.

Let’s say I need to create a brand new subfolder with an area repo after which push it to a brand new GitHub distant:

git new -f MyNewProject && cd MyNewProject && gh new

git new returns me to the listing the place I first referred to as the command, that means I have to manually enter the subfolder if I created one.

If I don’t need to create a subfolder, and as a substitute need to create a brand new repository within the present folder, I merely run this:

git new && gh new

If you happen to actually need to add this mixture as a Z-Shell alias, run this:

echo -n 'alias newgit="git new && gh new"' >> ~/.zshrc; supply ~/.zshrc

Simply keep in mind that this newgit alias can solely be referred to as within the folder you need to use it.

You may’t move it a subfolder title as a result of it should run gh new within the authentic folder and never the subfolder.

More Posts