1 of 31

Introduction to Git and Github

2 of 31

Two stages to saving changes:

3 of 31

The art of a good commit message

Short (<50 characters)

Purpose, not description

Specific

4 of 31

Exercise:

Create a directory on your Desktop called BigDataNotes.

Initialize a git repository in that directory.

Use Vim to create a file called gitNotes.txt.

Add a few lines about what we have done so far and save them.

Use git add and git commit to save them in your repository.

Use git status to verify each step.

Compare commit messages with your partner.

5 of 31

Exercise:

Create sub-directories in BigDataNotes:

CommandLine

PythonInterpreter

RemoteAccess

PackageManagement

Vim

Git

Move gitNotes.txt into Git directory.

Move your vimNotes.txt (from yesterday) into Vim directory.

Use git add and git commit to commit changes

Try to git add RemoteAccess – what happens?

6 of 31

Checkout: Revert to previous commit

To revert to LAST commit:

git checkout HEAD <filename>

Use git log to show commit IDs for other commits.

Use HEAD~<#> for a certain number of commits in the past.

e.g.

HEAD ~2

7 of 31

Exercise:

You have been working on an important Python script for weeks, dataCruncher.py.

Unfortunately, your last change broke your script!

Which of the following commands would allow you to reverse your most recent change?

git checkout HEAD

git checkout HEAD dataCruncher.py

git checkout HEAD~1 dataCruncher.py

git checkout <unique ID of last commit> dataCruncher.py

8 of 31

Exercise:

You have been working on an important Python script for weeks, dataCruncher.py.

Unfortunately, your last change broke your script!

Which of the following commands would allow you to reverse your most recent change?

git checkout HEAD

git checkout HEAD dataCruncher.py

git checkout HEAD~1 dataCruncher.py

git checkout <unique ID of last commit> dataCruncher.py

9 of 31

Workflow and History Exercise:

cd BigDataNotes/Git

echo "Git allows for efficient collaboration" >> gitNotes.txt

git add gitNotes.txt

echo "Git is like an unlimited un-do button!" >> gitNotes.txt

git commit -m "Notes on Git"

git checkout HEAD gitNotes.txt

cat gitNotes.txt

What will be in the file?

Why?

10 of 31

Workflow and History Exercise:

cd BigDataNotes/Git

echo "Git allows for efficient collaboration" >> gitNotes.txt

git add gitNotes.txt

echo "Git is like an unlimited un-do button!" >> gitNotes.txt

git commit -m "Notes on Git" Not added, so not committed

git checkout HEAD gitNotes.txt

cat gitNotes.txt

11 of 31

How to ignore files with git

Files that do not change (e.g. data)

Log files

.gitignore

12 of 31

Exercise:

Edit the .gitignore that we just created.

Add all of the files that start with “log” to this file.

Add tracked files and .gitignore

Commit

13 of 31

Why Github?

Centralized for collaboration

Remote backup

External visibility

Find and use “bleeding edge” software

Open Science

14 of 31

Set up your free account on Github.com

  • Similar considerations as a professional email address:
    • Best to include your name
    • Best to avoid your school, graduation year, interests, etc.

15 of 31

Set up your personal access token

  • Verify your email address if you have not done so already
  • On github, click on your profile picture and then Settings:

16 of 31

Set up your personal access token

  • Scroll to the bottom and click on Developer settings
  • Click on personal access tokens

17 of 31

Set up your personal access token

  • Scroll to the bottom and click on Developer settings
  • Click on personal access tokens

18 of 31

Set up your personal access token

  • Click generate new token

  • Give your token a descriptive name
  • Give your token an expiration (default is fine)
  • Under permissions, select repo
  • Click generate token
  • CRITICAL: save the string that is displayed – this will be your password

19 of 31

Remote repository

mkdir <repoName>

cd <repoName>

git init

20 of 31

Exercise

What would have happened if the remote repository were initialized with a README.md and/or license file?

git pull --allow-unrelated-histories origin master

21 of 31

Collaborating on Github

22 of 31

Collaborating on Github Exercise

-1- Both partners give one another access to remote repository.

-2- Partner B clones Partner A’s repository:

git clone <URL> <path to local>

-3- Partner B edits a file, commits to local repository, and pushes to Partner A’s remote.

-4- Partner A pulls changes from partner B.

-5- Partner A repeats steps 2-3.

23 of 31

Advanced Topics!

24 of 31

What about conflicts?

Person 1

Person 2

25 of 31

26 of 31

Branches

27 of 31

Branches

git checkout –b newFeature

git push origin newFeature

28 of 31

Branches

git merge <branchName>

29 of 31

Branches

git merge <branchName>

30 of 31

Branches

git diff <sourceBranch> <targetBranch>

31 of 31

Branches

git diff <sourceBranch> <targetBranch>