Now that you have a git repository, we walk through the process for using Git locally on your personal computer, and using GitHub to back it up. We walk through creating your personal GitHub account, setting up Git on your computer, starting your first Git repository, and connecting that repository to a GitHub repository.
What is GitHub?
GitHub is a popular hosting service for Git repositories. GitHub allows you to store your local Git repositories in the cloud. With GitHub, you can backup your personal files, share your code, and collaborate with others.
In short, GitHub is a tool for working with Git. There are other services to host Git repositories, but GitHub is a trusted, free service used by organizations across the world, big and small.
Create a GitHub Account
To use GitHub, you will need a GitHub account.
In your own browser:
- Open a new browser tab
- Navigate to https://github.com/
- Create an account
If you already have a GitHub account, continue to the next section.
After you sign up, you will receive a verification email. Be sure to verify your email address to GitHub by following the instructions in that email.
Git Setup for Mac
Next, we will set up Git on your mac. Follow the instructions for your operating system.
- Launch the Terminal application. You can find it in /Applications/Utilities/. You can also use the Spotlight search tool (the little magnifying glass in the top right of your screen) to search for Terminal. Once Spotlight locates it, click on the result that says Terminal.
- When Terminal opens, type in git and press enter.
- If you don’t already have Git installed, a dialog will appear saying that “The ‘git’ command requires the command line developer tools. Would you like to install the tools now?” Click “Install”.
Then click “Agree to the Terms of Service” when requested.
- When the download finishes, the installer will go away on its own signifying that Git is now installed! Click “Done” to finish the installation process.
- Navigate to GitHub’s articles on setting up your Git username and email and follow the instructions for each using Terminal.
- GitHub offers two authentication options, HTTPS and SSH, to keep your work secure. This is a security measure which prevents anyone who isn’t authorized from making changes to your GitHub repository. Here, we will use HTTPS. Navigate to GitHub’s article on caching your password and follow the instructions to configure your computer to be able to use HTTPS.
Now skip down to the “Try it Out!” section below.
Git Setup for Windows
Next, we will set up Git on your windows. Follow the instructions for your operating system.
This portion of the guide assumes you have already installed a program called Git Bash which allows us access to Git on Windows. If you have not installed Git Bash, install Git Bash on Windows from https://gitforwindows.org/. Once you complete that you can continue with this guide.
- Open the Start menu and search for the app, git bash. You should see ‘Git Bash Desktop app’ appear. Press Enter or click on the Git Bash icon to open the app.
A new window will open that looks like this:
This window is our CLI, where we will use our Git commands.
- If you want to make sure that Git is installed, run git –version in the CLI. You should see a response that gives you the version of Git installed. It will look like this:
Git can now be used in the Git Bash app!
- Navigate to GitHub’s articles on setting up your Git username and email and follow the instructions for each using Git Bash.
- GitHub offers two authentication options, HTTPS and SSH, to keep your work secure. This is a security measure which prevents anyone who isn’t authorized from making changes to your GitHub repository. For https, Navigate to GitHub’s article on caching your password and follow the instructions to configure your computer to be able to use HTTPS.
Connect to GitHub through ssh
To connect to GitHub through ssh you need to follow these steps:
If you don’t already have an SSH key, go to generate a new SSH key section. If you’re unsure whether you already have an SSH key, read check for existing keys section.
Check for existing keys
- Open Git Bash.
Enter ls -al ~/.ssh to see if existing SSH keys are present:
|$ ls -al ~/.ssh|
- # Lists the files in your .ssh directory, if they exist
- Check the directory listing to see if you already have a public SSH key.
By default, the filenames of the public keys are one of the following:
If you don’t have an existing public and private key pair, or don’t wish to use any that are available to connect to GitHub, then jump to generate a new SSH key (next section).
If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub, you can add your SSH key to the ssh-agent (next 2 section).
Generating a new SSH key
- Open Git Bash.
- Paste the text below, substituting in your GitHub email address.
|$ ssh-keygen -t rsa -b 4096 -C “email@example.com”|
This creates a new ssh key, using the provided email as a label.
|> Generating public/private rsa key pair.|
- When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
|> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]|
- At the prompt, type a secure passphrase. Leave it empty.
|> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
It is done. You have a new SSH key in the specified folder. You need it for next 2 sections.
Adding SSH key to the ssh-agent
To add your SSH key to the ssh-agent follow these steps:
- Start ssh-agent in the background.
|$ eval $(ssh-agent -s)
> Agent pid 59566
- Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
|$ ssh-add ~/.ssh/id_rsa|
Great. Now you have added SSH key.
Adding the SSH key to your GitHub account
1.Copy the SSH key to your clipboard. If your SSH key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.
|$ clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
Tip: If clip isn’t working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.
- In the upper-right corner of any page, click your profile photo, then click Settings.
- In the user settings sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.
- Paste your key into the “Key” field.
- Click Add SSH key.
- If prompted, confirm your GitHub password.
Try It Out!
Now you have everything you need to practice your Git skills on your local computer. Take a moment to run the commands below to initialize a Git repository. We will use this Git repository again later in this tutorial so make sure you complete these steps exactly as described.
- mkdir git_practice to make a new directory to practice.
- cd git_practice to make the new directory your working directory.
- git init to turn the current, empty directory into a fresh Git repository.
- echo “Hello Git and GitHub” >> README.txt to create a new README file (more on this later) with some sample text.
- git add README.txt to add the new file to the Git staging area.
- git commit -m “First commit” to make your first commit with the new README file.
Your First Remote Repository on GitHub
Finally, we’ll create a repository on GitHub and then link it to a local repository on your computer. This allows you to backup your work constantly and safely, so you never need to worry about losing your work again!
Now, let’s connect our local Git repository to GitHub.
- In your Command Line Interface, make sure your current working directory is your new Git repository. Navigate there if not.
- Check the status of which files and folders are new or have been edited. There should be no files modified.
|$ git status|
- On GitHub, create a new repository by clicking the New repository button on the home page.
- On the new repository page, give your repository a name. It’s not necessary, but it would be convenient to name it the same as the directory, git_practice. After naming the repository, click Create repository.
- After creating a repository, GitHub displays the repository page. At the top of the page, make sure “HTTPS” is selected.
6.The repository is empty, so it’s time to connect it to your existing work. Copy the Git commands on the GitHub page, under the title “…or push an existing repository from the command line”, and paste them into your Command Line Interface. Running these commands will add a remote repository, and then push your local repository to the remote repository.
When asked for a username and password, type in your GitHub username and password and press enter after each. Don’t be alarmed if you can’t see the characters you are typing, they are intentionally hidden as a security measure.
Note: If you set up two-factor authentication with GitHub (don’t worry if you didn’t), follow these instructions to generate an OAuth token to be used instead of your password in bash. By default, GitHub does not set up two-factor authentication. If you are not familiar with two-factor authentication, you don’t have to generate an OAuth token.
- Once your Command Line Interface reports that the push is complete, refresh the page on GitHub. You should now see the text you wrote earlier in the README file, “Hello Git and GitHub.”
GitHub automatically displays the contents of a file named README.txt if it exists in the repository. The README file is the perfect place to write a description of your project.
There you have it! Your first GitHub repository, linked to your local Git repository. You’ve taken some huge leaps, so be proud! Now you can use your knowledge of Git to track progress on your local computer, and push that progress to GitHub whenever you want. You can rest easy knowing that each step of your progress is safely stored in GitHub.