Skip to main content

GitHub Tutorial for Beginners – How to Use GitHub

GitHub is a web-based platform where users can host Git repositories. It helps you facilitate easy sharing and collaboration on projects with anyone at any time.

GitHub also encourages broader participation in open-source projects by providing a secure way to edit files in another user's repository.

How to Host a Git Repository on GitHub

To host (or share) a Git repository on GitHub, follow the steps below:

Step 1: Signup for an account

The first step to begin hosting on GitHub is to create a personal account—you can sign up on the official registration page.

Step 2: Create a GitHub remote repository

After signing up for an account, create a home (a repository) in GitHub, for the Git repository you want to share.

Step 3: Connect your project's Git directory to the remote repository

Once you've created a remote repository for your project, link the project's .git directory—located locally on your system—with the remote repository on GitHub.

To connect to the remote repository, go inside the root directory of the project you want to share via your local terminal and run:

git remote add origin https://github.com/your-username/app-repo-name.git

Note the following:

  • Replace your-username in the code above with your GitHub username. Likewise, replace app-repo-name with the name of the remote repository you want to connect to.
  • The command above implies that Git should add the specified URL to the local project as a remote reference with which the local .git directory can interact.
  • The origin option in the command above is the default name (a short name) Git gives to the server hosting your remote repository. In other words, instead of the server's URL, Git uses the short name (origin).
  • It is not compulsory to stick with the server's default name. If you prefer another name rather than origin, substitute the origin name in the git remote add command above with any name you prefer.
  • Remember that a server's short name (e.g., origin) is nothing special! It only exists—locally—to help you easily reference the server's URL. So, feel free to change it to a short name you can easily reference.
  • To rename any existing remote URL, use the git remote rename command like so:
git remote rename theCurrentURLName yourNewURLName
  • To remove an existing remote URL from your local repo, use the git remote remove theRemoteURLName command. For instance, you can remove the origin remote URL from your local directory with the following command:
git remote remove origin
  • Whenever you clone (download) any remote repo, Git automatically names that repo's URL origin. However, you can specify a different name with the git clone -o yourPreferredName command.
  • To see the exact URL stored for nicknames like origin, run the git remote -v command.

Step 4: Confirm the connection

Once you've connected your Git directory to the remote repository, check whether the connection was successful by running git remote -v on the command line.

Afterward, check the output to confirm that the displayed URL is the same as the remote URL you intend to connect to.

note

Step 5: Push your local Git repo to the remote repo

After successfully connecting your local directory to the remote repository, you can then begin to push (upload) your local project upstream.

The code syntax to upload (push) a local .git directory (your commits, branches, and files) to a remote repository is git push -u remoteName branchName.

In other words, to push your local .git directory, and assuming your remote URL's short name is "origin", run:

git push -u origin main

The command above implies that Git should push your local main branch to the remote main branch located at the URL named origin.

In other words, the command instructs Git to push your .git directory to the remote origin branch from your local main branch.

note
  • You can substitute the origin option with the remote repository's URL. Remember, the origin option is only a nickname of the URL you've registered into your local .git directory.
  • The -u flag (upstream/tracking reference flag) automatically links the .git directory's local branch with the remote branch. Such linkage allows subsequent usage of the git push (or git pull) command without any arguments. In other words, you can run only git push instead of git push origin main.

Step 6: Confirm the upload

Lastly, return to your GitHub repository page to confirm that Git has successfully pushed your local Git directory to the remote repository.

note
CodeSweetly ads

Master NPM Package Creation

Elevate your skills, boost your projects, and stand out in the coding world.
Learn more

How to Publish Your Website with GitHub Pages

After pushing your project to your remote repository, you can publish it easily on the web by following these steps:

Step 1: HTML file name

Make sure that the name of the main HTML file of your project is index.html.

Index HTML
file

A highlight of a project file's name

Step 2: Go to the Settings tab

On GitHub's website platform, go into the repository of the project you want to publish and click the repository's Settings tab.

GitHub project repo
setting

A highlight of a GitHub repo's settings tab

Step 3: Go to the Pages tab

Click the Pages tab located towards the bottom of the side menu.

GitHub settings
page

A highlight of a GitHub repo's pages menu

Step 4: Change the Source

Under the Build and deployment section on the GitHub Pages, change the branch from None to master (or main, if main is your remote branch's name).

GitHub source
option

A highlight of GitHub pages' source dropdown menu

Step 5: See your site live!

A notification saying, "Your site is published at https://your-username.github.io/your-github-repo-name/" would display.

Website published success
notification

A highlight of a GitHub pages' success notification

You can now view—and publicize—your project at the specified URL!

note

By default, GitHub pages look for the index.html file in your project's root folder. However, suppose your project's index.html is in a subdirectory; you can still deploy it by following this GitHub gist's instructions.

Overview

GitHub is an online platform for hosting (or sharing) Git repositories. It helps you create an avenue to collaborate easily on projects with anyone, anywhere, at any time.

Useful Resources

Below are links to other valuable content on GitHub.

FREE Git Cheat Sheet!

Get your free PDF Git cheat sheet.

Use it to easily remember the Git commands you need for your projects.

Download Now

Your support matters: Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.

Tweet this article