GitHub¶
Version Control is an important component in software development which manages changes to documents, websites, code, or other digital information. Version control can save you when code changes break things. Web hosting of your code repositories lets you share and work on code together and save your work in the event of a hardware failure.
The most commmon version control software in data science are git
, svn
, cvs
, and bzr
.
Given the limited amount of time we have this week, we are only going to cover Git and one web-based hosting service (GitHub) in this camp.
In this lesson you will learn how to:
- Interact with
git
via GitHub - Using a browser
- Using the command line
- Interact with
- Add collaborators to your organization
- Initialize a
git
repository on the command line and push it to GitHub - Push/pull files
- Commit to GitHub
- Initialize a
- Learn about versions
- Forks
- Branches
- Merges
- Revert
- Releases
- Badging
Important
GitHub is not really intended for storing or manipulating data
Navigating & Interacting with GitHub¶
Basic Layout¶
The basic layout includes:
- Profile
- Organizations
- Teams
- Repositories
- Projects
- Followers
- Contributions
- Watch / Unwatch repos
Add a Collaborator¶
Online only
- Go to your Organization
- Click “people”
- Invite someone!
OR
- Can add collaborators under “Settings” -> “Collaborators”
Create a Repo Online & Locally¶
Online
- Go to your dasboard
- Click “New”
- Name the repository
- Initialize with “README” - form of metadata - this tells you and the those looking at the repository what it is about
- Go to “Clone or Download” and clone to local folder / directory titled “Lab”
- Can delete repo under “Settings” -> “Options”
Command Line
- Locate or create a local folder / directory structure titled “Lab”
cd
mkdir -p github/lab
cd github/lab
- Initialize folder as a repository with
git
git init
echo "This is a test repo" >> README.md #append to file
git add . #add all the files in the current directory
git commit -m "initial project"
Note
Documentation is vital You will be doing this for YOU 6 months from now
Interact with GitHub¶
Clone a repo
This is used to work locally rather than online.
Online
- Click the down arrow “Clone or download”
- Click “Open in Desktop”
- Select where to save it - Create a folder for GitHub repos on your computer locally
Command Line
cd path/to/location
git clone <url> [rename]
#URL of thte repository on GitHub
#rename the directory (optional)
Commit
Online
- Create a file by clicking “Create a new file”
- Name file (/name)
- Write commit message
- Press “Commit”
OR
- Create a file locally
- Click “Upload files”
- Select file(s) within a folder
Command Line
- Create a file
cd path/to/repo
touch file.txt
- Add file(s)
git add -A #adds all the new files
git push
git commit -m "added file" #-m initiates a message
Tip
It is good practice to write commmit messages to remember what you added or fixed.
Create an Issue
Issues are great for tracking decisions made or to-do lists
Online only
- Click on the repository you just created.
- Click on “Issues”
- Click on “New issue”
- Create a title (# and issue number for reference)
- Assign to someone, or create a label
- Submit new issue
- Close issue
Version Control
Online
- Click on file
- Click “History”
Command Line
git log
git log --stat #gives abbreviated stats for each commit
git log --pretty=oneline #can also add options: short, full, fuller
git log --pretty=format:"%h - %an, %ar : %s #lots of options for pretty=format
q #to quit
Create a branch
Branches are useful to working on code etc. without disturbing the master branch.
Online
- Select the down arrow on the repository page that says “Branch:master”
- Create a new branch name
OR
- Add new file or edit existing file.
- Write a commit message.
- Select “Create a new branch”
Command Line
- Create a new branch
cd path/to/repo
#common practice to pull before commiting anything
git pull #does a fetch for you
git checkout -b new-branch #creates a new branch and puts you on that branch
#set new branch upstream
git push --set-upstream origin new-branch
- Edit some files
#edit files
vi path/to/file
q #to quit
- Commit changes
git pull
git add
commit -am "changed a file" #stage changes and write a message
git push
Make a pull request
Pull requests are useful to have another set of eyes to review changes before merging them with the master branch.
Online Only
- From your branch, create a new file
- Commit file to your branch
- Hit “Compare & pull request”
- Go to pull requests
- Set it on the master branch
Merge
Online
- Go to “Pull requests”
- Select down arrow of “Merge pull request” (if no conflicts)
- Delete branch
- Leave comment if need be
- Close pull request
Command Line
cd path/to/repo
git pull
git checkout new-branch
git merge master #testing to see if merging breaks anythin
git checkout master
git merge new-branch #now repull it all into master
Revert
Command Line
git log
#copy tag for last working commit
git revert <tag to last working commit>
#will make it look like a new commit
git add -A
git commit -m "changed things back to <commit tag>"
git push
Advanced¶
Fork
Online
- Go to a new repository
- Click fork
- Save to personal repository.
- Clone to Desktop.
- Interact via online or in Desktop.
- If want to make suggestions, can create a pull request.
Command Line
git close <github-repo>
cd <new-folder>
git fork
Etc.¶
These are Online only
- Versioning
- Go to “Releases”
- Click “Create a new release”
- Tag version: Version #
- Release title: I usually put the date of the release, but any system can work
- Reactions:
- Create a new pull request
- Looking at the messages, click the smiley face to give a reaction
- Badges
- Go find a badge!
- Copy badge code into README:
Git cheat sheet¶
Here is a list of the most important commands in Git:
Git Task | Command | Description |
---|---|---|
Set up your profile locally | git config --global user.name "Cy Unicorn" |
Set your user name |
git config --global user.email Cy1@cyverse.org |
Set your email address | |
Create a Repository locally | git init |
Initialize a folder as a git repository |
Get an existing repository from a web service | git clone ssh://git@github.com/[username]/[repository-name].git |
Create a local copy of a remote repository |
Branching & Merging | Description |
---|---|
git branch |
List branches (the asterisk denotes the current branch) |
git branch -a |
List all branches (local and remote) |
git branch [branch name] |
Create a new branch |
git branch -d [branch name] |
Delete a branch |
git push origin --delete [branch name] |
Delete a remote branch |
git checkout -b [branch name] |
Create a new branch and switch to it |
git checkout -b [branch name] origin/[branch name] |
Clone a remote branch and switch to it |
git checkout [branch name] |
Switch to a branch |
git checkout - |
Switch to the branch last checked out |
git checkout -- [file-name.txt] |
Discard changes to a file |
git merge [branch name] |
Merge a branch into the active branch |
git merge [source branch] [target branch] |
Merge a branch into a target branch |
git stash |
Stash changes in a dirty working directory |
git stash clear |
Remove all stashed entries |
Sharing & Updating Projects | Description |
---|---|
git push origin [branch name] |
Push a branch to your remote repository |
git push -u origin [branch name] |
Push changes to remote repository (and remember the branch) |
git push |
Push changes to remote repository (remembered branch) |
git push origin --delete [branch name] |
Delete a remote branch |
git pull |
Update local repository to the newest commit |
git pull origin [branch name] |
Pull changes from remote repository |
git remote add origin ssh://git@github.com/[username]/[repository-name].git |
Add a remote repository |
git remote set-url origin ssh://git@github.com/[username]/[repository-name].git |
Set a repository’s origin branch to SSH |
Inspection & Comparison | Description |
---|---|
git log |
View changes |
git log --summary |
View changes (detailed) |
git diff [source branch] [target branch] |
Preview changes before merging |