11.8. GitHub and the git push Command¶
Although Git repos can exist entirely on your computer, many free websites can host clones of your repo online, letting others easily download and contribute to your projects. The largest of these sites is GitHub. If you keep a clone of your project online, others can add to your code, even if the com- puter from which you develop is turned off. The clone also acts as an effec- tive backup.
NOTE
Although the terms can cause confusion, Git is version control software that main- tains a repo and includes the git command, whereas GitHub is a website that hosts Git repos online.
Go to https://github.com and sign up for a free account. From the GitHub home page or your profile page’s Repositories tab, click the New button to start a new project. Enter wizcoin for the repository name and the same project description that we gave Cookiecutter in “Using Cookiecutter to Create New Python Projects” on page 200, as shown in Figure 12-6. Mark the repo as Public and deselect the Initialize this repository with a README checkbox, because we’ll import an existing repository. Then click Create repository. These steps are effectively like running git init on the GitHub website.

Figure 12-6: Creating a new repo on GitHub You’ll find the web page for your repos at https://github.com/<username>/ <repo_name>. In my case, my wizcoin repo is hosted at https://github.com/ asweigart/wizcoin. ## Pushing an Existing Repository to GitHub To push an existing repository from the command line, enter the following:
C:UsersAlwizcoin>git remote add origin https://github.com/<github_ username>/wizcoin.git C:UsersAlwizcoin>git push -u origin master Username for 'https://github.com': <github_username> Password for 'https://<github_username>@github.com': <github_password> Counting objects: 3, done. Writing objects: 100% (3/3), 213 bytes | 106.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/<your github>/wizcoin.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
The git remote add origin https://github.com//wizcoin. git command adds GitHub as a remote repo corresponding to your local repo. You then push any commits you’ve made on your local repo to the remote repo using the git push -u origin master command. After this first push, you can push all future commits from your local repo by simply run- ning git push . Pushing your commits to GitHub after every commit is a good idea to ensure the remote repo on GitHub is up to date with your local repo, but it’s not strictly necessary.
When you reload the repo’s web page on GitHub, you should see the files and commits displayed on the site. There’s a lot more to learn about GitHub, including how you can accept other people’s contributions to your repos through pull requests. These, along with GitHub’s other advanced fea- tures, are beyond the scope of this book. ## Cloning a Repo from an Existing GitHub Repo It’s also possible to do the opposite: create a new repo on GitHub and clone it to your computer. Create a new repo on the GitHub website, but this time, select the Initialize this repository with a README checkbox.
To clone this repo to your local computer, go to the repo’s page on GitHub and click the Clone or download button to open a window whose URL should look something like https://github.com//wizcoin. git. Use your repo’s URL with the git clone command to download it to your computer:
C:UsersAl>git clone https://github.com/<github_username>/wizcoin.git Cloning into 'wizcoin'... remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (3/3), done. remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0 Unpacking objects: 100% (5/5), done.
You can now commit and push changes using this Git repo just as you would if you had run git init to create the repo.
The git clone command is also useful in case your local repo gets into a state that you don’t know how to undo. Although it’s less than ideal, you can always save a copy of the files in your working directory, delete the local repo, and use git clone to re-create the repo. This scenario happens so often, even to experienced software developers, that it’s the basis of the joke at https://xkcd.com/1597/.