Setting up Git and GitHub

Written by Anish Ahuja and Ferhawn Shaheen (5/7/2021)

Setting up Git

What is Git?

Git is a way to store and monitor changes to files on your directory and save the new changes. Git stores a series of snapshots of your files when you push new changes and monitors updates to them. For our purposes, we will be using Git to communicate with GitHub, where we can store our projects and their files. GitHub is a standard for developing, sharing, and collaborating on projects.

Downloading Git on Linux

To install Git on Linux, there are a few different commands based on your specific distribution. If you are working from a Debian-based system, such as Ubuntu, you would type the following command into your terminal (Ignore $):

$ sudo apt install git-all

If you are working from Fedora or a similar distribution, use the following command:

$ sudo dnf install git-all

For other distributions, look at this list

Downloading Git on macOS

There are 2 main ways to get Git on macOS. The first way is to use this link to download it from the git website. The second way is to use Xcode and the terminal. First, install Xcode with the following command:

xcode-select --install

After this, check if Git is on your device like so:

git --version

If Git is not installed, it will prompt you to install it. Confirm the installation and you will have Git on macOS.

Downloading Git on Windows

To download Git on Windows, use the following link. I also recommend using GitBash to work with Git rather than the Windows Command Line because it emulates a Unix environment and generally is easier to use.

Introduction to GitHub

GitHub is a platform to host, share, and collaborate on code. It's important to have an account to show off your projects, especially if you plan to major in computer science in the future.

Repositories

A repository is what stores all of your project's files and images, including their update history.

Branch

A branch is a version of the project with different edits. You can merge a branch into the main branch to finalize the edits as the main version of the project.

Using Git with GitHub

Setting up your initial commit

First, make your project locally. After you have your project ready (it doesn't have to be perfect, you can always push new changes), you need to make a local repo on Git. Use the following commands to do so.

$ git init
$ git add [file name] //Use git add * to add all your files
$ git commit

When using git commit, you can use git commit -m "message here" to commit with a message. Next we have to push this repo to GitHub. To do so, first log into your GitHub account. On the top right, there is a plus sign. Click on this, and then create a new repository. Fill in the name and description. On the next screen, there will be instructions on how to import a repository. The commands should look as follows:

$ git remote add origin https://github.com/username/new_repo
$ git branch -M main
$ git push -u origin master

Now you should have your initial repository committed to GitHub.

Pushing new changes

Now that you have an initial project made, we will go over updating your code/ pushing new changes. If you create new files that you want to add, use git add with the name of your file. For example,

$ git add file.py

After this, you still need to commit the change and the push it like so

$ git commit -m "Added file.py" //the message is still optional
$ git push

If you don't have any new files to add, but some have been changed, you can still use git add, but there is a shortcut to commit all changed files at once. Simply use -a when committing:

$ git commit -am "Updated code in some way"
$ git push

Other important commands

Cloning a repository copies all of the files from a repo and downloads it to your computer.

$ git clone [link]

If you are collaborating with a partner, you may wish to access their changes to the repository. If you fetch their changes, you merely get a copy of them as a branch, which you can access through git checkout. If you pull their changes, you merge them into your own code. Here is an example of fetch:

$ git fetch

Last updated