Automate local and remote repo creation


This post comes out of an irritation I faced personally, when I was first learning to code.  I learned by doing projects (often Freecodecamp ones!).  But I needed to make sure I didn’t lose my hard work, and that others could see the hard work I was putting in, so every project had to go on Github.  The more complete projects I had on Github, the easier it would be for recruiters.   But the steps required to set up a project, initialise a repo and sync with Github was really annoying and repetitive, so I decided to solve it.  This isn’t going to be a big, fancy, detailed and technically sexy post.  It’s going to be very un-sexy.  And you don’t need to be a shell scripting god to do it.

So the typical project setup work flow usually goes like this:

1) Go to my /projects folder and mkdir – create a project-of-some-name.

2) cd into the project folder and do git init to initialise it as a local git repo on my machine.

3) touch README.MD and open it and add some basic descriptions, including links to the resources / tutorials I was implementing in that project. Save the file.

4) run git add . and then git commit -m ' ...some initial commit message...

5) open a browser, go to Github, login, create a new (remote) repository, copy the url, return to my terminal, make sure I was in the correct project folder project-of-some-name…then run the git scripts needed to set up the remote repo as the ‘upstream’ repo and connect my local repo to it.  Then, finally, I can run a git push and my local commit would get pushed up

6) lie down and take a nap, exhausted from this repetitive process.

Since automation is a great way to practice your coding skills, I decided to write a small shell script that automates these horrible and repetitive steps.  The script is at the bottom of this post, and be warned, it’s not sophisticated or fancy, but is sure gets the job done, and I don’t need to log in to Github and fool around will all those steps!

Before you copy the script, you need to know how to run it on your Mac. So below are the steps you need to implement to be able to use the script to automate your setup workflow.

1) I keep my scripts in my root/home folder, in a sub-folder called scripts.  I suggest you do the same or similar.  To get to the root/home folder, in your terminal type cd ~ because the tilda ( ~ )is the symbol for the home folder.  In your Mac Finder app it shows up as the one with a house icon.  So all my scripts are stored in ~/scripts

2) This matters because to run a shell script from any directory in the terminal, you have to type out the full path.  In my case I have to type out ~/scripts/git-script.sh to run the script.  But we’re getting ahead of ourselves.

3) copy the code in this post and then open a text editor, paste it in and then save it as [filename].sh.  The .sh is the extension for shell scripts.  Save the file in the directory you want to save it at – again I recommend ~/scripts as the folder to save your scripts in.

4) Navigate to that folder in your terminal. To be safe run ls in the terminal to check that you can see the script is there. If it’s not you’re in the wrong folder or step 3 didn’t successfully complete.

5) Make the shell script executable.  To do that you type the following in the terminal: chmod +x <<the-correct-filename.sh>>.  This is the unix way to make a shell script “executable”.  I’m not confident I fully understand what that means, other than it’s needed to make any shell scripts you write executable, so don’t ask me and I won’t lie to you.

6) navigate to your projects folder and make a new folder that you intend to house your project.  Effectively, you’ve got to do this:  mkdir – create a project-of-some-name inside the folder where you keep all your projects. So your project will eventually be placed inside my-computer/my-projects/project-of-some-name. cd into this folder and then type pwd to get the full path.  Copy that – you will need to paste it shortly. It should look like my-computer/my-projects/project-of-some-name

7) open your terminal again, and then type ~/scripts/<<the-correct-filename.sh>>.  The script runs.   The the main steps are:
    > what do you want to call your Github repo (don’t use spaces- ‘my-awesome-project’ is good. Don’t use ‘my awesome project’ as the repo name.

  > Enter a description that shows up in the Github repo’s description. For this it’s safe to use spaces.

 >  Enter the project path you got in step 6, the one that you get after typing pwd in the terminal and getting something like my-computer/my-projects/project-of-some-name

 > enter your Github username (not email address) and then your Github password.  Be careful as you type as these values don’t show up on the screen.

 > ….that’s it.  The script will set up your git repo locally inside my-computer/my-projects/project-of-some-name and then create a README.MD (blank) and then commit it locally, then set up a remote repo in Github (log you in via API) etc and then push everything up!

 > finally, you will see that the terminal you were interacting with has changed the currently active directory to your project folder. It will now be at my-computer/my-projects/project-of-some-name and you can type in ls and see the README.MD file. If you then type git status you will see your local repo’s status (the state of your local project) and if you type in git remote it will show you the Github url for your project!

Done! Happy Coding!

Annnd…..finally……here is the script! I’ve commented each step so you can reason your way through it.

# Make executable with chmod +x <<filename.sh>>

CURRENTDIR=${pwd}

# step 1: name of the remote repo. Enter a SINGLE WORD ..or...separate with hyphens
echo "What name do you want to give your remote repo?"
read REPO_NAME

echo "Enter a repo description: "
read DESCRIPTION


# step 2:  the local project folder path
echo "what is the absolute path to your local project directory?"
read PROJECT_PATH

echo "What is your github username?"
read USERNAME

# step 3 : go to path 
cd "$PROJECT_PATH"


# step 4: initialise the repo locally, create blank README, add and commit
git init
touch README.MD
git add README.MD
git commit -m 'initial commit -setup with .sh script'


# step 5 use github API to log the user in
curl -u ${USERNAME} https://api.github.com/user/repos -d "{"name": "${REPO_NAME}", "description": "${DESCRIPTION}"}"

#  step 6 add the remote github repo to local repo and push
git remote add origin https://github.com/${USERNAME}/${REPO_NAME}.git
git push --set-upstream origin master

# step 6 change to your project's root directory.
cd "$PROJECT_PATH"

echo "Done. Go to https://github.com/$USERNAME/$REPO_NAME to see." 
echo " *** You're now in your project root. ***"

Postscript For FreeCodeCamp students

I really, truly believe your most precious resources are your time, effort and money. Of these, the single most important resource is time, because the other two can be renewed and recovered. So if you’re going to spend time on something make sure it gets you closer to this goal.

With that in mind, if you want to invest 3 hours with me to find your shortest path to learning to code (especially if you’re a career changer, like me…), then head to my course site and use the form there sign up (not the popup!). If you add the words “FREE MY TIME” to the message, I will know you’re a freeCodeCamp reader, and I will send you a promo code, because just like you, freeCodeCamp gave me a solid start.

Also if you would like to learn more, check out  episode 53 of the  freeCodeCamp podcast, where Quincy (founder of FreeCodeCamp) and I share our experiences as career changes that may help you on your journey. You can also access the podcast on iTunes, Stitcher, and Spotify.

I can be contacted on Twitter: @ZubinPratap



Source link