> For the complete documentation index, see [llms.txt](https://marcylabschool.gitbook.io/swe/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://marcylabschool.gitbook.io/swe/mod-0-command-line-interfaces-git-and-github/cheatsheet.md).

# Cheat Sheet

**Table of Contents**

* [Review Branching](https://github.com/The-Marcy-Lab-School/marcy-curriculum-docs/blob/main/mod-0-command-line-interfaces-git-and-github/git-cheatsheet.md#review-branching)
* [Creating Branches](https://github.com/The-Marcy-Lab-School/marcy-curriculum-docs/blob/main/mod-0-command-line-interfaces-git-and-github/git-cheatsheet.md#creating-branches)
* [Commit Changes](https://github.com/The-Marcy-Lab-School/marcy-curriculum-docs/blob/main/mod-0-command-line-interfaces-git-and-github/git-cheatsheet.md#commit-changes)
* [Merge before you push](https://github.com/The-Marcy-Lab-School/marcy-curriculum-docs/blob/main/mod-0-command-line-interfaces-git-and-github/git-cheatsheet.md#merge-before-you-push)
* [Push and make a PR](https://github.com/The-Marcy-Lab-School/marcy-curriculum-docs/blob/main/mod-0-command-line-interfaces-git-and-github/git-cheatsheet.md#push-and-make-a-pr)

## Review Branching

<details>

<summary><strong>When should we create a new branch?</strong></summary>

Branches are used to diverge from the main code base. They are useful because they create a copy of existing code without modifying the existing code. Think of it as your very own sandbox where you can create anything new.

Therefore, a new branch should be created for any new change to any of the files in the project. This includes but is not limited to creating a new feature in the repo and/or fixing a bug in the repo.

</details>

<details>

<summary><strong>When do merge conflicts occur?</strong></summary>

Merge conflicts occur when we have code that could possibly overwrite code that was already there. They are bound to happen if multiple people are working on the same file.

</details>

<details>

<summary><strong>Things to avoid</strong></summary>

The `main` branch should always have working code so as a best practice...

* Don't work off of the `main` branch.
* Avoid merging code that hasn't been tested or reviewed into the `main` branch.

</details>

## Creating Branches

Each team member should own their own branch and work exclusively on that branch.

```
git branch ben-feature-A
git checkout ben-feature-A
```

Or, create a branch and switch to it in one command:

```
git checkout -b ben-feature-A
```

Then see all branches:

```sh
git branch
```

You should then see: (the `*` indicates the current branch)

```
  main
* ben-feature-A
```

> TIP: Always check to make sure that you are NOT working in the `main` branch

To switch back and forth between `main` and your branch, run:

```sh
git checkout main
git checkout ben-feature-A
```

## Commit Changes

When you're ready to save your current changes, create a *local commit*.

In your feature branch, run:

```sh
git add -A
git commit -m 'a message describing your changes'
```

## Merge before you push

While you have been working on your code, your teammates may have pushed changes.

First, switch to the `main` branch and `pull` the changes:

```sh
git checkout main
git pull
```

Then, switch back to your feature branch and merge the changes from `main` *into your branch*.

```sh
git checkout ben-feature-A
git merge main
```

You may need to resolve merge conflicts at this point. To resolve a conflict, delete the `<<<<<<< HEAD`, `=======`, and `>>>>>>> main` markers and keep the code that you want.

![](/files/0C082wQQr5Pzr3uCkiWa)

Finally, make a new commit to finish resolving these conflicts:

```
git add -A
git commit -m 'merging main into my branch'
git push
```

## Push and make a PR

Once you have merged `main` into your branch, go ahead and `git push`.

> If it is your first time pushing from this branch, you will be told to use the `--set-upstream` flag.

* Go to Github.com and open up your repository.
* Then, click on the Pull Requests tab to create a new pull request to merge your branch into `main`.
* Ask your teammates to review your code and then merge!
* If you want to continue working on your branch, do NOT delete the branch.

Your teammates can then follow the steps listed in [merge before you push](https://github.com/The-Marcy-Lab-School/marcy-curriculum-docs/blob/main/mod-0-command-line-interfaces-git-and-github/git-cheatsheet.md#merge-before-you-push) to update their local repositories.
