Note that this site is in currently in version 1.0.0-alpha.   Some functionality may be limited.

8. Pushing to GitHub

Now, you may want to backup or share and collaborate around a file on the Internet. Let’s connect the directory you created on your local computer to GitHub’s cloud service, which you can access through the web. Remember, GitHub is a service that allows you to host files, collaborate, and find the work of others. Once our syllabus is on GitHub, it will be publicly visible. (Repositories on GitHub can also be private but are public by default.) Go to GitHub in your browser and click the plus sign in the upper right hand corner. You can find the plus sign button to add a repo on the top right of github After clicking the plus button, select New repository from the dropdown menu. The dropdown menu where you select New Repository After clicking New repository, you’ll have to enter some information, including a name and description for your repository. Screen on GitHub where you enter your repository information

  • Choose a name, such as git-practice. (This does not need to match your folder name although it may be less confusing if you choose the same name here.)
  • Enter a description, such as Test syllabus for learning Git and GitHub.
  • Keep the Public — Anyone can see this repository selector checked. (If you choose the Private option, you will need additional steps, not covered in this workshop, to synchronize your GitHub repository with the folder on your computer.)
  • Do not select Initialize this repository with a README since you will be importing an existing repository from your computer.
  • Click Create repository. You should end up inside your newly created repository. It will look like a set of instructions that you might want to use to connect your GitHub repository to a local repository. The instructions we want consist of two lines underneath the heading ...or push an existing repository from the command line. The hand in this screenshot points to where these directions are on the page: The commands you need to copy from the new repo page on GitHub Use the copy button in the top right corner of the code box to copy all three lines of code. They will look something like this:
$ git remote add origin https://github.com/<username>/<repository-name>.git
$ git branch -M main
$ git push -u origin main

You’ll need the command copied from your new repository, since it will contain the correct URL. Paste them into your command line and press enter. You may need to press enter multiple times to run all three lines of code.

Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/<username>/<repository-name>.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

If you see output like this, go back to your new repository page in the browser and click the Refresh button. You should see your syllabus.md file on GitHub!

Review

We have covered the basic steps of creating a file and tracking changes within a file on your local machine and on GitHub.

This has involved coordinating across three different environments, so let’s go through that one more time. Note that this process is very slightly different. I’ll highlight it when it comes up.

To start, let’s add something to our syllabus. Another week of materials or a new reading.

Save that file.

Use git add via the command line to stage the file—tell Git what document you want it to pay attention to.

Use git commit via the command line to save the changes you’ve just made as a snapshot or new version of you file. Remember to use the -m flag and include a message about the change you just made.

So far, we have not done anything with GitHub or on the Internet. We have used Git, installed on our local machine, to save a version of file as it stands now. We could stop here if we only had an interest in using Git for version control. But if we also wanted to use GitHub to back up our files, or to share our project with a team or publicly, we want to upload, or push, that repository to GitHub on the Internet.

Use git push origin main to upload, or push, that file to your repository on GitHub. After refreshing the webpage, your file should appear online. The difference I noted above appears here. Note the absense of the -u flag from the command. That flag was used the first time to establish the connection between the repository on your local machine and on GitHub. Now that that connection has been established, that flag is not needed.

Challenges for lesson 8

Assignment: Challenges

  1. Go through the process a few more times by adding additional readings and weeks of course material. Remember to commit changes intentionally so your commit messages make sense. Use git log to review your changes.

  2. Also try creating a new file and adding an assignment. Rewrite the assignment using Markdown, or edit and add in the markers. Go through the process of staging and commiting that file, and pushing it to your repository on GitHub.

  3. Test your understanding by thinking through the following questions:

  • Do you need to push the file to GitHub each time you commit changes to the file, or can you make several commits to a file and push them all to GitHub at once?
  • Do you need to use git init after after adding a new assignment file to your folder?
  • What about the -u flag in the git push origin main command? Does this flag need to be used to add the assignment to your repository on GitHub?

In response to Challenge 3:

  • No, you don’t need to push to GitHub every time, or at all even, if you didn’t want to share your changes publicly. Git is the software that tracks the changes, and you review them on your local machine using Git as well.
  • No, you don’t need to initialize the folder after adding a new file for an assignment or otherwise. In this case, we’ve already initialized the process; Git is tracking the folder. After the file is added, we just need to notify Git to take a snapshot of the additions and changes using the git add, git commit sequence. We can also use git push to share those changes on GitHub.
  • No, the -u flag does not need to be used again. This flag is only necessary when setting up the original connection between the folder on your local machine and the folder on GitHub.

Questions

Try again!

Which best describes what you're doing when you use the command git push?

(Select one of the following)

Try again!

How does the process of pushing differ from the processes of staging and committing discussed in the previous lesson?

(Select one of the following)

Try again!

What happens if you use git push without staging and committing files?

(Select all that apply)

Try again!

What happens if you stage and commit files, but not push the changes?

(Select all that apply)

Terms Used in Lesson

Can you define the terms below? Hover over each of them to read a preview of the definitions.

Push

When you push your files, or use the command git push master origin, you are communicating with GitHub to update the connected project folder stored there. You are pushing the changes, say, to your syllabus.md …

See term page

Repository

Git uses the term repository to refer to a folder that is being tracked. Colloquially, this term is often shortened to “repo”.

See term page

Workshop overall progress