Version control with git and Github

Nick George

[email protected]

Contents

  • Why do we need version control?
  • How do you use it (basics)
  • more resources

http://nickgeorge.net/github-lab-setup/getting_started_git.html

Why do we need version control?

phdFinal.gif

by Jorge Cham, http://www.phdcomics.com

Multiple code versions

  • Natural tendency to happen when coding
  • programming is an iterative process
  • main.m
  • main_nick.m
  • old_main.m
  • etc…

our_code.png

What's wrong with this?

This can be dangerous when running programs!

  1. Difficult to stay organized
  2. Hard to develop (dropbox will overwrite local changes if you update)
  3. What if you want to go back to a previous version to re-run an old analysis?

version control workflow

Bryan_2017figure1.png Bryan2017

  • Base version is saved
  • New versions get comments and diffs are recorded

Version control is a must in software development

Still… why?

But we aren't software developers…

cd ~/Dropbox/m\ new/drg
wc -l *.m

We develop software

cd ~/Dropbox/m\ new/drg
wc -l *.m
      38 drgBehaviorbyBlock.m     
    1142 drgMaster.m
     323 drgPercentLick.m
     194 drgProbabilisticTimecourse.m
...
     137 drgSpikePhase.m
     270 drgThetaAmpPhaseLick.m
     189 drgThetaAmpPhaseNew.m
     234 drgThetaAmpPhaseTrialRange.m
      23 drg_ecdf.m
      17 drg_pcolor.m
      26 drg_play_filters.m
     253 drgdFvsBFREncRetr.m
      22 getComputerName.m
     162 shadedErrorBar.m
    9401 total

9,000+ Lines of code in one directory!

Version control is a must in software development

"I don't need version control, I work alone"

hackernews-comment.png

What is it version control?

Version control as an "Oh ** I broke it" insurance

git checkout old-version

Done.

Version control as a record

  • all changes
  • who made them
  • your helpful commit message.
  • Example video coming soon. see software carpentry in the meantime

What is git?

  • git is a version control system (there are many others…)
  • written by Linus Torvalds, the creator of the Linux kernal
  • git lives locally on your machine
  • git stores your code in a repository
  • with a few commands, you can use git to track your work

GitHub_Logo.png

  • GitHub is a cloud hosting service for git repositories
  • GitHub copies your local repositories to the cloud
  • Allows you to collaborate with others on the same code base

Interface for collaboration

compare_github_example.png

  • With git, you can easily compare differences between files on a commit by commit basis.

Interface for collaboration

  • Git keeps track of who made the changes and when they were made.
  • git and Github make working on code as a team easier.

How will you use it?

  1. Install git
  2. Configure git
  3. Make a github accound
  4. Clone
  5. Sync
  6. Contribute

1. Install git

Follow the Github guide

install git

2. Configure it

git config --global user.name "Nick George"
git config --global user.email "your-email"

3. Make a github account

4. Fork and clone

diego_git_page.png

4. Fork and clone

  • Click the repository (collection of code files) you want and click clone or open in desktop on the top right corner
  • Clone in Github desktop or with ssh/https

clone_download.png

4. Fork and clone

Clone

  • Cloning copies from Diego's page.

Fork

  • forking makes a local copy of Diego's repository
  • Now go to your copy and hit clone or open in desktop

5. Staying up to date

Github Desktop

desktop_sync.png

5. Staying up to date

Developers will have to set the remote… more coming soon

In a terminal

cd ~/Github/drta
git pull

6. Contribute

For developers!

developer guides coming soon

  • Code normally, but…
  • stay up to date with git pull
    • details in developer guide
  • use git commands
    • git add
    • git commit
    • git push
  • Create pull requests to add your changes to the main code base

COMMIT OFTEN!

As a team

Personal vs organization account

We can set this up a few different ways…

  • Diego's personal account
    • we fork, push, and pull as guests

Organization account

git-organization2.png

Questions?

  • More advanced guides coming as we need them
  • the most important part to staying up to date is merging

Resources

Highly recommend: https://peerj.com/preprints/3159/

How is this different from dropbox or google versions?

  • you can see diffs
  • Pick the files or changes to go back to
  • Previously discussed metadata

Sources of error

Merge conflicts

  • If the remote (main version from Diego) and your version both have changed files and you try to merge…
  • You will get a merge conflict.
  • git will not guess which change you want, it will just say you have a problem and make you fix it.
  • See the upcoming lab-developer-guide for avoiding these errors
  • https://team-coder.com/avoid-merge-conflicts/

Hide

Version control

Version control systems save the base file, then just records changes

Sorry, your browser does not support SVG.

  • Space efficient (only saving diffs)
  • ONE base version

https://swcarpentry.github.io/git-novice/01-basics/

Changes

Sorry, your browser does not support SVG.

https://swcarpentry.github.io/git-novice/01-basics/

Merge

Sorry, your browser does not support SVG.

https://swcarpentry.github.io/git-novice/01-basics/

References

Bibliography