# Setting up Git and GitHub

## 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](https://git-scm.com/download/linux)

### Downloading Git on macOS

There are 2 main ways to get Git on macOS. The first way is to use [this link](https://git-scm.com/) to download it from the git website. The second way is to use Xcode and the terminal. First, install Xcode with the following command:&#x20;

```
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](https://git-for-windows.github.io/). 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.&#x20;

### 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,&#x20;

```
$ 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://programclub-mchs.gitbook.io/python-lessons/setting-up-git-and-github.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
