Set up a Remote GitHub Repository From Terminal.

RoarzOnRailz
thelearningcurve
Published in
4 min readNov 22, 2019

Learn how to write a .bashrc function to interact with the GitHub API

So as it turns out. Since starting the 6-month Fast Track coding boot camp, at Coder Academy, Brisbane 3 months ago, I have developed a small obsession with automating the process from terminal to save time and make the process more efficient. Who needs GUI’s anyway right?

I started out by writing a small Bash script which I could call from anywhere in terminal which would prompt me for details required and then make the task happen. A friend of mine Carlie Hamilton(twitter plug -> @bluecodethree) then said why waste all that time why not make it a function! So I stared at the computer screen blankly for a while…..scratched my head….then asked my friend Google what a bash function was and like a dazzled gazelle I set of to work transferring my Bash script into a function.

I will assume that if your reading this particular article that you already have some sought of an idea about the CLI, and what a bash script and or function is, and we will jump straight into making this happen.

The Function

So the following is a function that we write straight into our .bashrc file. This will enable us to use the function just like any other terminal command and simply type the command with 2 arguments into the terminal from our new project directory and BOOM! a remote GitHub repository will be set up with a readme, initial commit, remote origin set for the master branch & an initial push. All with a few words!!

In the above function, I have 2 functions. A help function so I don't forget what I need o do if I don't use it for a while 😆 and the main function.

Step 1

Find your .bashrc file and open it in a text editor. I used nano…..much to my friend's disgust. VIM is life and you should probably learn it. 👀…

If you navigate to your home directory and do ls -a you you should see your bash file listed there. Technically you should just be able to type nano ~/.bashrc & this will open it from anywhere.

step 2 … important notice

Copy the above script into your .bashrc and replace my username with yours. You will need to do this where we make the API call and where we set the remote in the URL below it. In other words, replace the word Roakz with whatever your GitHub username is.

Step 3

If you don't already understand what its doing or don't care then move on to step 4 otherwise read on.

The help function

In this snippet, we see the help function. A bash function is initialised with the word function followed by the desired name gitRemote — help & parenthesis (). Then we create a block. { insert coder here}. This is really handy to know! we can now write all sorts of functions to make terminal life easier.

In this case, we have added a couple of echos to print some helpful information to the screen. You can change this to whatever you want though!!

The Github Remote function

Above is the business function. Where the magic happens. You probably recognise a lot of going on here! This is because we have literally just initialised a function and then carried out business as per usual. It doesn't work much different from a bash script only it accept arguments straight onto the function on the command line without needing prompting.

When adding arguments to functions on the command line they are accessible via “$1” “$2” “$3” etc … $0 is reserved for the function name.

Technically we could just type the API request from the terminal with the curl command anytime we wanted to do this. But why?….. Now we just type a few words and everything happens.

Any questions shoot my way! @RoarzOnRails is my twitter addy :D

Step 4

Save and exit from the text editor. If your using nano you press ctrl X then Y then enter to save. If you don’t press Y and then enter it won't save.

Now that we have saved it we need to let bash terminal know! we can use the command below to reload the .bashrc file into the current shell script

source ~/.bashrc

At this stage, if there are any errors or typos it will tell you. Use your technical sophistication (google) to fix it! 😁

Step 5

Test it out !!!

Type the command gitRemote-help into the terminal and follow the instructions to make it work.

Thankyou…your far to kind..

I hope this makes your life easier for years of pushing to everyone's favourite open source control platform for years to come !!!!

Any feedback or comments welcome on my socials.

twitter ->@RoarzOnRailz

linkedin -> www.linkedin.com/in/roarzonrails

--

--