2) Tools of the Developer Trade Lesson

How to use GitHub

7 min to complete · By Ryan Desmond

Once you know how to use Git, GitHub is an excellent remote repository for collaboration and constant access to your code.

How to Configure and Push to GitHub?

To use GitHub, you must have an account and create a repository. Here are the steps to do so:

1. Create an Account on GitHub

If you have not already created an account on GitHub, please click here and create a free account.

2. Create a New Repository

Once logged into GitHub, click on the + icon in the top right corner of the screen and select "New Repository".

Next, give the new repository a name. For this project, you can name it java_fundamentals. You can leave the description blank for now. You can also leave the repository set to public.

Do not check the Initialize this repository with a README option.

Click Create Repository. After which you'll see (something very similar to) the following:

The default screen show in GitHub after clicking Create Repository

Move Back to Your Local Machine

After you have a repository in GitHub, you need to connect it to your local repository (project) as a remote. This is done through the CLI on your local machine.

This is called adding a remote repository to your local Git repository. By adding a remote repository, you can now push your code up to the shared repository, where you can share your work and collaborate with others. This

3. Open Your Project's Directory on Your Local Machine

Make sure you're in the correct directory. The correct directory is the one that contains the "README.md" file and the "src" folder. If you followed along in the previous sections, this will be "~/Documents/CodingNomads/labs/online-java-fundamentals" directory.

$ cd ~/Documents/CodingNomads/labs/online-java-fundamentals

4. Check Current Remotes

Check to see what remotes you already have connected. At this point, you should not see any.

$ git remote -v

5. Add the New Remote

Add the new remote repository you created in GitHub.

$ git remote add <NICKNAME> https://github.com/<YOUR GITHUB USERNAME>/java_fundamentals.git

Be sure to replace <NICKNAME> and <YOUR GITHUB USERNAME> with your GitHub username as shown in the image above under the section labeled, "…or push an existing repository from the command line". The <NICKNAME> will be the local "nickname" of your remote repository.

You will use this nickname to push and pull your work from remote repos, so you don't have to type out the full URL every time.

6. Confirm the Newly Added Remote

Check (again) to see what remotes you have connected. At this point, you should see that you have one remote repository called main that points to your GitHub repository titled java_fundamentals with the nickname you provided.

$ git remote -v

Note: the -v means "verbose". Adding -v to the command will print out the URL to which the remote is connected. The -v will print out the names of the connected remotes.

7. Push to GitHub

Now that you have connected a remote to your local Git repo, you can push your code to the remote (GitHub).

$ git push -u <NICKNAME> main

Note: the -u is an argument to the push command that sets the following remote (NICKNAME) and the branch main as the default upstream remote and branch to track. The next time you push your work, you only need to run git push.

Output:

CLI output after doing a git push to main in java_fundamentals GitHub repo

Now, return to your GitHub account in your web browser, select the java_fundamentals project (if not already selected), and refresh the page. You should see the labs project and the README displayed below.

Each time you make a commit, you should also push your code to GitHub. This is not entirely necessary, but it's great practice. At a minimum, you should push your code to GitHub at the end of every coding session. Every time you push code to GitHub, you'll get a "green dot" on your GitHub profile showing you pushed code that day. As you're getting started with coding, your goal is to have a green dot just about every day of every week moving forward.

Summary: How to Use GitHub

This article taught you how to configure GitHub and push your first code to a remote repository.