> ## Documentation Index
> Fetch the complete documentation index at: https://calcs.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Working with Git

> Practical Git workflows for Calcs.com development

## Checking Out

### Working with Existing Branches/Changing Between Branches

Remember, we never make changes directly to the Master branch, but we create branches off Master to do our work. We can swap between Master and feature branches by using `git checkout {branchName}`.

**Examples:**

* `git checkout master` - Move to the Master branch
* `git checkout retainingWallsUSA` - Move to the retaining walls branch

When you swap between branches, you should `git pull` before and after you swap to ensure you have the latest copy of everything on your local PC.

**Command line workflow:**

```bash theme={null}
git pull
git checkout retainingWallsUSA
git pull
```

<Tip>
  You don't explicitly need to pull beforehand, however if it's been a while since you used Github or you have never checked this branch out before, this can ensure VScode knows what you are talking about when you try to check out the new branch. It also means you can use tab completion to type things faster!
</Tip>

<img src="https://mintcdn.com/clearcalcs/0BKfHfJWdoXOtU8o/images/migrated/d44163a628d6-image.png?fit=max&auto=format&n=0BKfHfJWdoXOtU8o&q=85&s=1e7b0dcc11c8a3ad135b88a635f2568c" alt="Branch Checkout" width="671" height="174" data-path="images/migrated/d44163a628d6-image.png" />

To avoid issues when changing branches or creating new branches, we recommend regularly going back to master.

### Creating a New Branch

If you are not looking at someone else's work or swapping into a branch you've already been working on, you will need to create a new branch.

<Warning>
  You should *always* create a new branch from Master, not from another feature branch (no branches off branches!).
</Warning>

To ensure this, and to avoid headaches later because you were working off old data:

1. Checkout master: `git checkout master`
2. Pull master: `git pull`
3. Create your new branch

Creating a new branch is very similar to checking out an existing branch, with one small exception. We add a `-b` to our checkout command to tell Git it's new:

```bash theme={null}
git checkout master
git pull
git checkout -b {newbranchName}
```

## Making Changes and Submitting Your Work

### Staging & Committing

Once you have successfully checked out a new or existing branch, you'll be ready to start making changes. Provided your VScode and Git have been set up correctly, as you make changes, they will appear in the sidebar under 'Changes'.

<img src="https://mintcdn.com/clearcalcs/E_1kb1vSSHohKQJH/images/migrated/623d5dfc667d-image.png?fit=max&auto=format&n=E_1kb1vSSHohKQJH&q=85&s=b048c004ef1a54ebb5729a513ef83b3e" alt="VS Code Changes" width="496" height="156" data-path="images/migrated/623d5dfc667d-image.png" />

<Warning>
  Until you stage, commit, and push these changes, *they are only on your PC*. This makes committing and pushing regularly a very important habit.
</Warning>

#### Understanding Commits

Github thinks of version control in terms of **Commits** - these are bundles of changes, with a reason behind them. Each branch can have multiple commits, all it means is we are grouping (Staging) each set of changes we make, and naming them.

<img src="https://mintcdn.com/clearcalcs/XO7fzDjaQ7IYQ3qi/images/migrated/11b9835f9022-image.png?fit=max&auto=format&n=XO7fzDjaQ7IYQ3qi&q=85&s=75ebbf406eb2ed779ecfb6876b5dddbc" alt="Commit History" width="871" height="218" data-path="images/migrated/11b9835f9022-image.png" />

#### How to Stage Changes

To stage some changes, you can either:

* Click the + icon next to the file you want to stage
* Right click the file and click 'Stage Changes'

Do this for each file you want to stage.

<img src="https://mintcdn.com/clearcalcs/-3TA4BOzcQ3BrOZT/images/migrated/86713b970fe3-image.png?fit=max&auto=format&n=-3TA4BOzcQ3BrOZT&q=85&s=27133f2b8df6cc39768f7c92a500fd26" alt="Staging Changes" width="416" height="532" data-path="images/migrated/86713b970fe3-image.png" />

#### How to Commit

As you stage your changes, they will move into a 'Staged Changes' dropdown, meaning they are ready to be **Committed**.

To commit:

1. Enter a message in the dialog box above describing your changes (e.g., `Amended calc count in trusted by section`)
2. Click Ctrl + Enter (or Cmd + Enter on Mac) to submit and commit your changes

<img src="https://mintcdn.com/clearcalcs/0BKfHfJWdoXOtU8o/images/migrated/dff2c3a60883-image.png?fit=max&auto=format&n=0BKfHfJWdoXOtU8o&q=85&s=8853b70c5aeba1c42dabd4f4424e1682" alt="Committing Changes" width="330" height="172" data-path="images/migrated/dff2c3a60883-image.png" />

<Note>
  You can also right click and Unstage changes if you make a mistake, or click 'Discard Changes' if you want to irreversibly delete the new changes you've made and go back to the version you most recently committed.
</Note>

<Warning>
  Your work is not yet on Github after committing! Once you commit, you need to `git push` to send it upstream to the server.
</Warning>

#### Pushing to Github

If you are working on a new branch that has never been pushed before, you will get a fatal error (this is fine). Just copy and paste the message into the terminal and it will push properly.

<img src="https://mintcdn.com/clearcalcs/E_1kb1vSSHohKQJH/images/migrated/5563d315fce5-image.png?fit=max&auto=format&n=E_1kb1vSSHohKQJH&q=85&s=87da45e79704b4f1f0a44b6b9882af68" alt="First Push Error" width="555" height="173" data-path="images/migrated/5563d315fce5-image.png" />

Once you have successfully pushed your work, you can either:

* Keep working and create new changes and commits
* Change to another branch without fear of issues

### Submitting Your Work Back to Master (Pull Requests)

When you're ready to submit your work back to master, you need to create a pull request. We will cover that in volume two of our git for dummies series when chris isn't tired and thirsty.

## Common Issues

<Accordion title="Coming Soon">
  * Merge conflict
  * Git reset/bad commit
  * Can't find branch (git fetch)
  * Bad line endings (bad git for windows setup)
  * Branching off a branch
</Accordion>
