the basics and essentials of version controlshinstitute.org › wp-content › uploads › 2019 ›...

36
The Basics and Essentials of Version Control David Sondak February 1, 2019

Upload: others

Post on 09-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Basics and Essentials of Version Control

David Sondak

February 1, 2019

Page 2: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Outline

1 What is Verison Control?

2 git

The Bare EssentialsWorking with Other PeopleBeyond the Bare Essentials

1 / 27

Page 3: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Version Control: Motivation

Version control is a non-negotiable component of any project.

Why?

• Reproducibility

• Maintainability

• Project longevity

2 / 27

Page 4: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Examples of Version Control

• Mercurial

• Git

• Concurrent Versions System (CVS)

• Apache Subversion (SVN)

• GoogleDrive

• Dropbox

Distributed Version Control

Centralized Version Control

Don’t use these for software

3 / 27

Page 5: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Centralized Version Control

Central Repository

Working Copy Working Copy Working Copy

com

mit

chec

kou

t

comm

it checkout

4 / 27

Page 6: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Distributed Version Control

Full Repo Full Repo

Full Repo

Central Repository

5 / 27

Page 7: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Distributed Version Control

Full Repo Full Repo

Full Repo

Central Repository

5 / 27

Page 8: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Plan

1 What is Verison Control?

2 git

The Bare EssentialsWorking with Other PeopleBeyond the Bare Essentials

6 / 27

Page 9: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Setting up

Pre-requisite: You should already have a Github account.

• Create a new directory called SHI Webinar

• You will do all your work in that directory

• Go to the Github website: https://github.com/

• In the upper right corner click on the + symbol and choose New

repository

7 / 27

Page 10: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Creating a New Repo

• Create a new repository just for this webinar

• You will name the repo <firstname lastname> playground

8 / 27

Page 11: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 12: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 13: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 14: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 15: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 16: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 17: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 18: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

The Bare Essentials of git1

1Adapted from Robert Adolph

9 / 27

Page 19: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Cloning a Repository

Copy the repo address andclone:

10 / 27

Page 20: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Poking Around: git log

• Enter the new repository

• From the command line, type git log

• log tells you all the changes that have occured in the project as ofnow

• Each one of these ”commits” is an SHA hash• Uniquely identifies all actions that have happened to this repo

previously

11 / 27

Page 21: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Poking Around: git status

• From the command line, type git status

• Status is your window into the current state of your project

• Use this all the time

12 / 27

Page 22: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

A Few Special Files and Directories

.gitignore• Tells git what files to ignore when:

• Adding files to the index• Committing to the local repo

• Currently specialized to Python

.git/config• Tells us about a remote called origin

• Simply the Github repo we clonedfrom

• Tells us about a branch called master• “Tracks” a remote branch calledmaster at origin

13 / 27

Page 23: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Making Changes

• Say you want to add a new file to the project• The canonical sequence is edit—add—commit—push

• Change the README.md file

• You’ve edited README.md in the working directory, but it hasn’t beenstaged yet

• Let’s use git status to check the current state of our local repo

14 / 27

Page 24: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Staging: git add

• First, you need to add them to the staging area (a.k.a. the index)

• NEVER use git add .

• (unless you’re a git pro)

• The staging area is useful for working toward a full commit

15 / 27

Page 25: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Updating Local Repo: git commit

• You can use git commit -m "<Good commit message.>" tocommit changes to your local repo

• Those changes will now have a permanent record in the repository

• Every commit has two features you should be aware of:

1 A hash — Unique identifier for all of the info about that commit

2 A commit message — Good commit messages are critical!16 / 27

Page 26: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Some Notes on git commit

git commit has a few handy options that you might like:

• git commit -m

• A way to specify a commit message without opening a text editor

• e.g. git commit -m "An informative commit message."

• git commit -a

• Shorthand to stage and commit a file which is already tracked

• Will not stage a file that is not yet tracked!

• e.g. git commit -a

• You can put both of these commands together• e.g. git commit -a -m "Yet another informative commit

message."

Many more options: https://git-scm.com/docs/git-commit

17 / 27

Page 27: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Updating Your Remote Repo: git push

• git push attempts to update the remote repo

• If you’re the only person working on both repos, then git push

should always work

• Go to your remote repo and see the changes!

18 / 27

Page 28: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Exercise: A First Collaboration

1 You will be put into breakout rooms of 4 people

2 Find 1 partner and clone their repo• You must add your partner as a collaborator in your repo via theSettings tab [Hint: Ask them for their Github ID]].

• Two ways to find your partner’s repo:• Simply type user:<user name> in the Github search field

• Have them send you the link to their repo

3 Edit their README and update the repo• Use the edit—add—commit—push flow

4 Wait until both partners have pushed changes to each others’ repos

5 Now go into your repo and type git pull

6 Open README.md — you should see some changes!

19 / 27

Page 29: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

fetch, merge, & pull

• The git pull step actually represents 2 combined commands1 git fetch — Update your local repo with changes from remote

2 git merge — Combine changes from your local repo and the remoterepo

• Beware merge conflicts!

• The proper explanation requires a discussion of branches and remotes• See https:

//www.atlassian.com/git/tutorials/syncing/git-fetch andhttps://longair.net/blog/2009/04/16/git-fetch-and-merge/

20 / 27

Page 30: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Demo: Merge Conflicts

A common pattern is as follows:

• Sally and Joe are working on the same repo

• Sally makes a change to the code, commits those changes locally, andpushes to the remote

• Joe makes a different change to the same piece of code, commitsthose changes locally, and pushes to the remote

• git complains to Joe because it doesn’t want to overwrite Sally’schanges on the remote

21 / 27

Page 31: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Resolving Merge Conflicts

• Now Joe tries to pull in Sally’s changes to his local repo, but he getsa merge conflict

• The HEAD is what is in Joe’s local repo

• The origin/master is what is on the remote

• Joe must decide which to keep• Maybe he needs to call up Sally and ask

22 / 27

Page 32: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Beyond the Bare Essentials

• git is very powerful, but do not be intimidated!

If you “break” your repo, do not simply delete it and reclone!

• There are many wonderful git resources out there:• git - the simple guide• Atlassian git tutorial

• Other things to be aware of:• Basic terminology (e.g. HEAD)• Remotes: git syncing• Branches: git branch• Resolving merge conflicts: git merge conflicts• Reset and revert repo: git reset

23 / 27

Page 33: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

A Basic Workflow (Using git)

1 Host the software project somewhere• GitHub, Bitbucket, GitLab, in-house server

2 Check out the software to your workstation• git clone ...

3 Make your changes and add your features• Write code• git add changed file• git commit changes

4 Run some tests to make sure you didn’t break anything• This can be automated (e.g. TravisCI)

5 Merge your new features to the main code base• git push

24 / 27

Page 34: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Version Control Summary

1 Always use version control in your projects

2 Never delete a repo to fix it

3 Be patient in learning good version control practices

↪→ It will pay off

4 Commit early and commit often• Use atomic commits• Take advantage of the staging area (if you’re using git)

5 Commit code that compiles (it doesn’t have to be correct yet)

6 Always run tests!

25 / 27

Page 35: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Project Management

We’ve discussed a basic version control workflow using git and Github.

For real projects,

you should use some actual project management.

• Use the issue tracker

• Create and manage pull requests

• Start a Github project

26 / 27

Page 36: The Basics and Essentials of Version Controlshinstitute.org › wp-content › uploads › 2019 › 02 › SHI_Webinar... · Apache Subversion (SVN) GoogleDrive Dropbox Distributed

Thank you!

Questions?