How to push a new project to GitHub

If you’ve never used Git or GitHub before, you need to understand one of the most important tasks you’ll use with the service: How to push a new project to a remote repository.


Image: Jack Wallen

GitHub is one of the most widely-used software repositories for the Git Version Control system. With GitHub, you can create new repositories, share those repositories, and collaborate with teams on projects.

For every developer, one of the most important things you can do is interact with GitHub from the command line. By doing this, you can create an empty repository on GitHub and then connect a local repository to that remote. With this setup, you can do everything you need from the local command line.

How do you do this? I’m glad you asked. What I’m going to do is walk you through the process of creating a new remote repository on GitHub and then creating a local repository on your desktop machine and connect the two. This is very basic Git/GitHub stuff, so if you’re already up to speed on how this works move along as there’s nothing for you here.

However, if you’re new to Git/GitHub, keep reading, as this is something you’ll be using quite a lot in your future as a developer.

SEE: VPN usage policy (TechRepublic Premium)

What you’ll need

The first thing you’ll need is a GitHub account, so head over and sign up. You’ll also need git installed on your platform of choice. I’ll be demonstrating with Ubuntu Server 18.04, but the operating system doesn’t matter, so long as it supports Git.

How to create a new repository on GitHub

The first thing to be done is the creation of a new repository on GitHub. Log in to your GitHub account and go to the Dashboard. From that page click the Repositories tab. In this new window, click New (Figure A).

Figure A


The repository tab in GitHub.

In the resulting window, give the project a name and an optional description (Figure B). Depending on what this project is used for, you might make it private.

Figure B


Creating a new repository on GitHub.

Click Create Repository and GitHub will do it’s thing. The address of the new repository will be:​

Where USER is your GitHub username and PROJECTNAME is the name you gave your new project. You’ll need that address in a bit.

How to connect your local project

Now the fun begins. On your local machine, create a new project folder with the command:

mkdir myproject

Change into that newly-created directory with the command:

cd myproject

Initialize the repository with the command:

git init

Now let’s create a readme file with the command:

touch readme.txt

Add the new file to the staging area with the command:

git add .

Now we’re going to create our first commit. If you’re not sure what a commit is, it’s simple: A commit adds the latest changes to the source code to the newly-created repository. These changes will then be part of the head revision of the repository.

To create the commit, issue the command:

git commit -m "added readme"

You can change the text in quotes to be whatever you want, such as “my first commit.” Make sure the text in quotes describes what’s been done for the commit.

The next step will make use of the GitHub repository address. What we need to do is add the local repository to the origin (the name of the remote repository where you want to publish your commits) of the remote repository. This is done with the command:

git remote add origin

Where USER is your GitHub username and PROJECTNAME is the name you gave your new project. 

At this point you can then push your work to the remote with the command:

git push -u origin master

When you run this command, you’ll be asked for your GitHub username, followed by your GitHub user password. Upon successful authentication, your local repository will be connected to the remote GitHub repository and the readme.txt file pushed to the remote.

Let’s add another file and push it to the remote repository. Issue the command:


Add the file to the staging area with the command:

git add .

Issue a new commit with the command:

git commit -m "added license file"

Push the changes to the master branch of the GitHub repository with the command:

git push -u origin master

Now, if you look at the repository on GitHub, you’ll see both the readme.txt and the LICENSE files are there (Figure C).

Figure C


We’ve successfully pushed our files to the remote GitHub repository.

Congratulations, you’ve just pushed a new project to GitHub from the command line. Yes, this is very basic stuff, but it’s a task you’ll need to understand as you embark on a career as a developer. Make sure you are well-versed in Git and GitHub, so your path toward developer nirvana is as clear as possible.

Also see

Source link