# What is version control

<div style="text-align: center;">
  <table>
    <col width="40%">
    <col width="40%">
    <tr>
      <td style="text-align: left;">
      [I]s the <strong>management of changes</strong> to documents [...] <strong>Changes are usually identified</strong> by a number or letter code, termed the "revision number", "revision level", or simply "revision". For example, an initial set of files is "revision 1". When the first change is made, the resulting set is "revision 2", and so on. <strong>Each revision is associated with a timestamp and the person making the change</strong>. Revisions can be <strong>compared</strong>, <strong>restored</strong>, and with some types of files, <strong>merged</strong>. -- <a href="https://en.wikipedia.org/w/index.php?title=Version_control&oldid=948839536" target="_blank">Wiki</a>
      </td>
      <td>
        <img src="https://upload.wikimedia.org/wikipedia/commons/a/af/Revision_controlled_project_visualization-2010-24-02.svg" alt="Diagram of version control" width="35%">
      </td>
    </tr>
  </table>
</div>



---

# Why do we care

Have you ever:

- Made a **change to code**, realised it was a **mistake** and wanted to **revert** back?

- **Lost code** or had a backup that was too old?

- Had to **maintain multiple versions** of a product?

- Wanted to see the **difference between** two (or more) **versions** of your code?

- Wanted to prove that a particular **change broke or fixed** a piece of code?

- Wanted to **review the history** of some code?

- Wanted to submit a **change** to **someone else's code**?

- Wanted to **share your code**, or let other people work on your code?

- Wanted to see **how much work** is being done, and where, when and by whom?

- Wanted to **experiment** with a new feature **without interfering** with working code?

In these cases, and no doubt others, a version control system should make your life easier.

-- [Stackoverflow](https://stackoverflow.com/a/1408464/2097171) (by [si618](https://stackoverflow.com/users/44540/si618))

---

# Git: The stupid content tracker

<div style="text-align: center;">
  <figure>
  <a href="https://commons.wikimedia.org/wiki/File:Git-logo.svg" target="_blank"><img style="width: 200px;vertical-align: middle;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Git-logo.svg/500px-Git-logo.svg.png" hspace="20px" alt="Git logo"></a>
  </figure>
</div>

- During this class (and perhaps, the entire program) we will be using [Git](https://git-scm.com).

- Git is used by [most developers in the world](https://insights.stackoverflow.com/survey/2018#work-_-version-control).

- A great reference about the tool can be found [here](https://git-scm.com/book)

- More on what's stupid about git [here](https://en.wikipedia.org/wiki/Git#Naming).

---

# How can I use Git

There are several ways to include Git in your work-pipeline. A few are:

- Through command line

- Through one of the available Git GUIs:

  - RStudio [(link)](https://rstudio.com/products/rstudio/features/)
  
  - Git-Cola [(link)](https://git-cola.github.io/)
  
  - Github Desktop [(Link)](https://desktop.github.com/)

More alternatives [here](https://git-scm.com/download/gui).

---

# A Common workflow

<div style="text-align: center;">
  <figure>
    <img style="width:600px;vertical-align:middle;" hspace="20px" src="https://github.com/USCbiostats/PM566/raw/b9ca589471562011f57d08845825282ee19d6acb/static/slides/02-version-control/fig/git.svg" alt="Git workflow">
  </figure>
  <figcaption><b>A common git workflow</b></figcaption>
</div>


1. Start the session by pulling (possible) updates: `git pull`

2. Make changes
  
    a. (optional) Add untracked (possibly new) files: `git add [target file]`
  
    b. (optional) Stage tracked files that were modified: `git add [target file]`
  
    c. (optional) Revert changes on a file: `git checkout [target file]`
  
3. Move changes to the staging area (optional): `git add`

4. Commit:

    a. If nothing pending: `git commit -m "Your comments go here."`
  
    b. If modifications not staged: `git commit -a -m "Your comments go here."`

5. Upload the commit to the remote repo: `git push`.


