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.
Host Git repository on GitHub
To host (or share) a Git repository on GitHub, follow the steps below:
Step 1: Signup for a GitHub account
The first step to begin hosting on GitHub is to create a personal account. Hence, visit the official registration page to sign up.
Step 2: Create a remote repository in GitHub
After signing up for an account, create a home (i.e., a repository) in GitHub, for the Git repository you want to share.
Step 3: Connect 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/yourusername/yourreponame.git
yourusernamein the code above with your GitHub username.
yourreponamewith 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
.gitdirectory can interact.
originoption in the command above is the default name (a short name) Git gives to the server hosting your remote repository.
That is, instead of the server’s URL, Git uses the short name
- It is not compulsory to stick with the server’s default name. If you prefer another name rather than
origin, simply substitute the
originname in the
git remote addcommand above with any name you prefer.
- Always 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 renamecommand like so:
git remote rename theCurrentURLName yourNewURLName
- 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 yourPreferredNamecommand.
- To see the exact URL stored for nicknames like
git remote -vcommand.
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.
- See the “Connecting with SSH” article if you wish to connect using the SSH URL instead of the HTTPS URL.
- However, if you are not sure of the remote URL to use? Check out the “Which remote URL should I use?” article.
- Do you wish to change your remote URL? Changing a remote’s URL is an excellent guide.
Step 5: Push a 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.
Whenever you are ready to share your project elsewhere — on any remote repo, simply instruct Git to push all your commits, branches, and files in your local
.git directory to the remote repository.
The code syntax used to upload (push) a local Git directory to a remote repository is
git push -u remoteName branchName.
That is, to push your local
.git directory, and assuming the remote URL’s short name is “origin”, run:
git push -u origin master
- The command above implies that git should push your local master branch to the remote master branch located at the URL named origin.
- Technically, you can substitute the
originoption with the remote repository’s URL. Remember, the
originoption is only a nickname of the URL you’ve registered into your local
-uflag (upstream/tracking reference flag) automatically links the
.gitdirectory’s local branch with the remote branch; thus, allowing you to use
git pullwithout any arguments.
Step 6: Confirm the upload
Lastly, go back to your GitHub repository page to confirm that Git has successfully pushed your local Git directory to the remote repository.
- You may need to refresh the remote repository’s page for the changes to reflect.
- GitHub also has a free optional facility to convert your remote repository into a functional website. Let see “how” below.
Publish your website with GitHub pages
After pushing your project to your remote repository, you can easily publish it on the web like so:
- Make sure that the name of the main HTML file of your project is
- On GitHub’s website platform, go into the repository of the project you want to publish and click the repository’s settings tab.
- Scroll down to the GitHub Pages section and change the Source branch from none to master.
- Afterward, a notification saying, “Your site is published at https://your-username.github.io/your-github-repo-name/” would display.
- Now you can view — and publicize — your project at the specified URL!
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.
GitHub is an online platform for hosting (or sharing) Git repositories. It helps you create an avenue to collaborate easily on projects with anyone, at any place, at any time.
Below are links to other content containing valuable information on using GitHub.
- GitHub Hello World project
- A free Introduction course, by Learning Lab
- Learn more about working with GitHub pages
Team building work photo by Tumisu
Last Updated on January 10, 2021 by Oluwatobi Sofela