How to publish your local project on GitHub using command line

Github Publish Using Command line

I have been currently learning how to use Git using the command line. Git is one of the most popular version control systems (VCS) and if you use any kind of VCS for your development, I am sure you should be familiar with the benefits of using Git.

Along with Git, another useful platform that enhances the code sharing abilities and remote working possibilities is GitHub – a popular Git repository hosting service. Using Git and Github can be an essential skill to learn if you want to write code, work in a group, and contribute to open source software such as WordPress.

The other part of the equation in this post is using the command line. There are plenty of GUI tools for using Git and although they are continuously improving to accommodate more and more features, you will still need to use the command line to get the full git experience.

While the command line surely looks intimidating for beginners like us, I would seriously recommend putting in the time and effort to learn it as it makes so many things in programming easy. Also, sometimes command line can be the only possible way to interact with an application.

In this short tutorial, I will show you how to publish your first local project on GitHub using the command line. You can see how easy it can be using the command line, Git, and the GitHub.

I assume you have some understanding and deeper interest in learning the command line and Git to follow these steps.

Step 1: You will need to create a new repository on GitHub website.

Select New repository from the menu on your right once you are logged into your GitHub account.

Create new repository Github website

Add the Repository name and description of your project.

New Github Repository details

Do not initialize the new repository with README, licence, or add any .gitignore files yet. You can add these files later.

Step 2: Now go to the Terminal on your computer. While I prefer using iTerm, the default terminal on Mac works fine.

CMD + Space should bring up the search bar where you can search for Terminal to quickly open it.

Open Mac Terminal

Step 3: Use cd to navigate to the local project directory that you want to publish on GitHub.

CD to navigate in Command line

Step 4: If you haven’t initialised a Git repository in the project directory, use the below command to initialise the local directory as Git repository.

$ git init

Step 5: Add all the files in the local directory to staging using the command below.

$ git add .

This command stages all the files in the directory, ready for commit.

Step 6: You can now commit the staged files using the command below. Don’t forget to add a good commit message that is explanatory and helpful.

$ git commit -m "First commit Message"

Step 7: Now, copy the remote repository URL provided to you when you published your repository on GitHub.

Copy GitHub repository URL button

Step 8: Back in the terminal, add the copied URL for your GitHub repository as remote repository using the code below. (Replace the GitHub-repository-URL with your Github repository URL you copied in the step above)

$ git remote add origin GitHub-repository-URL

This command adds our GitHub repository as a remote that you can then push your local repository changes.

Step 9: Finally, use the below command in your terminal to push the local repository to GitHub.

git push origin master

This command says to push your commits to the remote repository we added earlier called “origin” in its branch named master.

If you use -u in the command, it will remember your preferences for remote and branch and you can simply use the command git push next time.

git push -u origin master

Alternatively, you can directly find the complete command of step 8 and 9 listed on the Github page after you have created the repository.

Push repository using command line

You can now see all the changes to your local repository published on the GitHub repository!

GitHub Repository Published Command Line

I hope the tutorial was useful to learn basic Git commands and in publishing your first local project on GitHub.

Watch the video below if you are not familiar with basic Git commands.

You can learn more about Git going through this free ebook from official Git website.

If your WordPress host supports git, check to see if it helps in your development process. One of the hosting providers, Pantheon, for example, makes it easy to visualise and understand version control by integrating the git process within its dashboard. Check out their guide here.

Also, check out this interactive tryGit exercise from CodeSchool that walks you through the basics steps of learning Git and pushing to remote repositories like GitHub within 15 minutes. You can also use the exercise to refresh your learning of Git.

Let me know if you have any questions or suggestions about Git and Github in the comments below.

Pradeep Singh

Pradeep Singh

Pradeep Singh is the founder and your host here at WPism. He is an entrepreneur and blogger living his startup life based in London and Cambridge. Follow him on Twitter or like his page on Facebook.
Share on facebook
Share on twitter
Share on reddit
Share on linkedin
Share on email

8 Responses

  1. Thanks, I have wasted about two hours searching for –> how to create repository from command line, and it seems that it can be only done on github webiste. Some even provided a command create but there is no such command in a reference of github project.


Leave a Reply

Your email address will not be published. Required fields are marked *

Join our list

Our Exclusive Deals

Share this post

Share on facebook
Share on twitter
Share on pinterest
Share on linkedin
Help us share this post with your network?
Share on facebook
Share on twitter
Share on linkedin
Share on email
Want to keep updated with WordPress?

Join our monthly newsletter to receive best curated WordPress resources.