Introduction to Git and Github
Two stages to saving changes:
The art of a good commit message
Short (<50 characters)
Purpose, not description
Specific
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.
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?
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
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
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
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?
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
How to ignore files with git
Files that do not change (e.g. data)
Log files
.gitignore
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
Why Github?
Centralized for collaboration
Remote backup
External visibility
Find and use “bleeding edge” software
Open Science
Set up your free account on Github.com
Set up your personal access token
Set up your personal access token
Set up your personal access token
Set up your personal access token
Remote repository
mkdir <repoName>
cd <repoName>
git init
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
Collaborating on Github
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.
Advanced Topics!
What about conflicts?
Person 1
Person 2
Branches
Branches
git checkout –b newFeature
git push origin newFeature
Branches
git merge <branchName>
Branches
git merge <branchName>
Branches
git diff <sourceBranch> <targetBranch>
Branches
git diff <sourceBranch> <targetBranch>