--- tags: Documents --- # Github Guide ## What is Github? For that matter, what is Git? Git is a version control system that lets you keep track of your software projects in *repositories*, which hold all the files, folders, and revision history of their associated projects. It also allows you to collaborate asynchronously with many other people. Github is one particular service that we’ll be using to host our code repositories. Think of it like Google Drive, but specifically for code. ## Getting started Git provides many useful features, but we’ll stick to the basics that you need for CS200. Of course, you'll need a Github account, if you don't already have one. Just sign up with the email of your choice at [Github.com](https://github.com). You may also need to [install `git`](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) on your terminal. If you are not comfortable with using the terminal, you can also download [GitHub Desktop](https://desktop.github.com). This guide will have instructions on using both. ### Downloading code For each assignment, you’ll be given a Github repository link. Once you accept the assignment, you'll be taken to the main page of the repository, which will look something like this: ![](https://i.imgur.com/17AVwJ8.png) #### Downloading with git on the terminal To download the code to your machine, first copy the HTTPS clone link, as seen in the screenshot. Then, navigate to the appropriate directory in terminal, then use the command ``` git clone <link> ``` (e.g. `git clone https://github.com/MyRepositoryLink.git`). Doing so will create a local copy of the repository in that directory. #### Downloading with GitHub Dekstop When you first open GitHub Desktop, you'll see a screen similar to this one: ![](https://i.imgur.com/xFuRZ2U.png) If you've accepted the Github classrooms assignment, it should appear somewhere in the right dropdown, under the **brown-cs200-spring-2022** header. Click on "clone [assignment name]" to open the repository in GitHub Desktop. You can also access this menu with File->Clone Repository: ![](https://i.imgur.com/CCgCKj3.png) The URL field is the address of the GitHub repo that was created when you accepted the assignment. **Note:** Make sure that the local path that you clone the repository to is somewhere that you can easily access! You'll need to open your project from that directory when you launch it in IntelliJ. - **This is especially important if you are on a Mac.** If you are, we suggest that you make a folder in your `/Users/[username]` folder (the one with a :house: icon next to it in Finder), something like `cs200`. Then, make sure that when you clone a repository, you clone it into `/User/username/cs200` (and for Github desktop, make sure that you set your Local Path to that folder). Another alternative is to store your files in `User/username/IdeaProject` folder that IntelliJ generates for you. ### Saving and uploading code (while working alone) #### Terminal There are three steps to updating your repository: staging, committing, and pushing. Before you perform these actions, make sure you are in the base directory of the repository. You should see something in your terminal ending with `(master)` or `(main)`, e.g. ``` Desktop ~/documents/brown/homework/cs200/hw1 (master) $ ``` If this doesn't show up, you can also run `git status` in your terminal, which will let you know if the current directory is a git repository. To stage your changes, you can use `git add -A`. There are many options you can use with `add` but usually doing `git add -A` suffices. By staging, you are essentially telling Git, "These are the changes I want to save to my repository" (`-A` means stage *all* changes, which is usually what we want). To commit your changes, use `git commit -m "<some informative message>"`. This command saves all the changes you specified in staging, and files it in the version history with the (hopefully) informative message you wrote. For instance: `git commit -m "Added feature X to the code."` Now your code is saved in local history, but you need to let the remote repository (Github) know what has changed. Do `git push` to upload your changes to Github. #### GitHub Desktop ![](https://i.imgur.com/ycpbo7f.png) When you make changes to the repository that you have loaded on GitHub Desktop, you will be able to see all of the new changes that you've made to your code. To upload those changes to GitHub (to the repository that was created by GitHub Classrooms), click on the "Commit to master" button in the bottom left. And that's it! Now you have saved your local changes to GitHub. ### Git commands reference | Command | Effect | -------- | -------- | | `git clone <url>` | Copies repository at `<url>` to a new folder in the current directory. | | `git add -A` | Stages all changed files (prepares them for commit) | | `git commit -m "message"` | Commits all of your staged changes. **Note:** this does not upload your changes to GitHub. | | `git push` | "Pushes" all of your code to your online GitHub repository. That is, it updates your online GitHub repo with what you have stored on your local filesystem | | `git pull` | "Pulls" anything new from the online GitHub repository and copies it to your local repository. That is, it will update your local repository with any new code that is on the GitHub repository. | | `git stash` | Erases all changes that you made since your last commit. | | `git status` | Reports what files you have changed since your last commit. | ### Advanced Git Skills Many of the more advanced Git commands are useful or necessary when collaborating with other people on a project. In CS200, since pair programming is required for all group projects (that is, a given set of changes should be made on the same machine), this is not as much of a concern. If you are interested in learning more about Git anyway (as you will almost certainly need it in future classes/careers), this is a good starting resource: [Github Git Handbook](https://guides.github.com/introduction/git-handbook/) --- *Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CSCI0200 document by filling out the [anonymous feedback form](https://docs.google.com/forms/d/e/1FAIpQLSdFzM6mpDD_1tj-vS0SMYAohPAtBZ-oZZH0-TbMKv-_Bw5HeA/viewform?usp=sf_link).*