This tutorial introduces you to using GitHub.
After reading this tutorial, you should be comfortable...
git push
This tutorial assumes that ...
git
and are familiar with the basicsGitHub is a service for hosting and managing remote Git repositories.
One strategy to prevent data loss is to have at least one copy of the data (preferably on anothe machine). Git refers to each of these external copies of a repository as a remote. Each remotes includes the history of changes for the repository.
While it's most common for a repository to have a single remote, it's certainly possible to have multiple (or even zero). For example, you might have a remote called company-a
and another for customer-a
. By convention, the authorative remote is typically called origin.
Each remote is associated with an address (URL) which tells Git where the remote can be reached to send and receive information about the repository.
You can list all existing remotes using git remote -v
. If you haven't yet configured a remote, the list will be empty.
You can remove a remote using git remote remove <origin-name>
. For example ...
git remote remove origin
... will remove a remote named origin
.
You can add a remote using git remote add <origin-name> <origin-url>
. For example ...
git remote add origin https://address/for/origin.git
... will add a remote named origin
that points to https://address/for/origin.git.
If you haven't already, create a GitHub account.
To avoid repeatedly entering your password, you may want to cache your credentials with GitHub. One option is to use an SSH key. Another option is to use the HTTPS protocol and cache your credentials using the GitHub CLI. We'll walk through this second option.
homebrew
Perhaps the easiest method for installing the GitHub CLI is to use the homebrew
package manager (available on MacOS and Linux).
Before installing homebrew
on Ubuntu, you'll need to install a few dependencies. Run the following command in your terminal:
sudo apt-get install build-essential procps curl
To install homebrew
, run the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Select the default options. At the end of the installation, you may be prompted to enter two commands in your terminal. Do so.
Next, we'll install the GitHub CLI using homebrew
:
brew install gh
Now that we've installed the GitHub CLI, we're ready to cache our credentials for using the HTTPS protocol to clone and push to GitHub.
In your terminal, run the following command:
gh auth login -h "github.com" -p "https"
You'll be prompted to answer a series of questions:
? You're already logged into github.com. Do you want to re-authenticate? Yes
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser
! First copy your one-time code: ???-???
Press Enter to open github.com in your browser...
Take note of the one-time code displayed in the terminal and hit ENTER
. A browser window should launch. Enter your one-time code. After doing so, you may be prompted to authorize GitHub and to enter your GitHub password. After completing these steps, you can close your browser tab and return to the terminal.
Let's start by creating an empty repository on GitHub.
From your GitHub profile page, click the +
icon in the upper righthand corner and select the New repository option from the dropdown menu:
Alternatively, you can just navigate to the following URL:
On the next screen, you'll be prompted to name your new repository. For this example, I've elected to call it git-demo
:
Click the Create repository button.
On the next page, you'll be presented with instructions for cloning this repository or using it as a remote for an existing repository.
Let's assume you don't have a local repository already. Open your terminal, create the directory you wish to use for your repo (ex. ~/repos/git-demo
), and navigate to the directory. Then enter the commands provided by GitHub:
echo "# git-demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com:myedibleenso/git-demo.git
git push -u origin main
myedibleenso
in the snippet above to your GitHub username.
Next, refresh the page on GitHub. Your pushed changes should have appeared on GitHub:
You've successfully pushed to your remote!
Time to practice what you've learned!
Download a git
cheatsheet (quick reference) in your preferred language: https://github.github.com/training-kit/
In many of the examples used in these tutorials, you may have seen a file name with the .md
extension (ex. README.md
). This extension denotes a Markdown file. Markdown provides a simple syntax for formatting documents. It also supports inclusion of HTML. For an optional introduction to Markdown, see this tutorial.
When working collaboratively on a Git-managed repository, you're likely to eventually run into a merge conflict. For some targeted practice in resolving such conflicts, see this tutorial.
As you grow proficient in using GitHub, you may find youself wanting to create and manage remote GitHub repositories from the comforting embrace of the Linux command line. GitHub developed a command line utility called hub
to support CLI workflows. For usage and installation instructions, see the official documention.