Git and GitHub
Joel Ross
Starting with
Git and GitHub
Joel Ross
Manual Version Control
Version Control Systems
A version control system (VCS) is a tool for managing a collection of program code that provides you with three important capabilities: reversibility, concurrency, and annotation.
A command line application used for software version control. It provides line-by-line change tracking and allows for simultaneous off-line editing.
Note that git does not automatically keep track of changes, you will need to manually "snapshot" different versions you wish to save.
A web service that stores git-managed projects online. You can use git to upload or download projects from GitHub.
git is the application that manages versions of your code. GitHub is just a website where those versions can be stored remotely.
Installing Git
Step 1. Install Git (see textbook)
# configure git to know who you are
git config --global user.email "your_email_address"
git config --global user.name "Your Full Name"
# check on command line that git is installed
git --version
Repository
(or "repo")
repo
projectX
projectY
Repository
(or "repo")
projectX
projectY
Repository
(or "repo")
Repos on GitHub
GitHub hosts repositories, each of which can be found at a different address.
Example: https://github.com/dpla/black-womens-suffrage(source code for https://blackwomenssuffrage.dp.la/)
Cloning
You clone (download) a repository to your local machine. This creates a copy of that repo on your computer.
git clone
repo on your
machine
repo on GitHub
Personal Access Tokens
In order to clone repos from Github, you will need to set up an access token (like a password) for authentification. Do this under Github's Settings > Developer Settings
Git Commands
git clone url
Download repo to local machine. Creates a local copy.
DO NOT PUT ONE REPO INSIDE OF ANOTHER!
Git Commands
git clone url
Download repo to local machine. Creates a local copy.
git status
Check status of repo
Branches
Branches allow for non-linear development and for naming different versions of code in the same repo. More on this later!
main
Ada
Remotes
origin
A remote is a repository on another machine that a repository can upload and download code from.
Git gives each remote a name (an "alias") to easily refer to it. By convention, the "primary" remote (where you cloned from) is named
origin
Tracking Changes with Git
Joel Ross
How do we "save" our changes?
The Staging Area
Put changes in temporary storage before committing.
git add .
Add everything in directory
Add file to staging area
git add FILENAME
use this
Committing Changes
Store current snapshot of files in repository!
git commit -m "message"
Commit changes
If you forget the -m option,
use :q (colon then q) to quit vi
Commit Message Etiquette
Use informative commit messages; they document changes for others. Messages should complete the sentence:
If applied, this commit will ________________.
Local Process
files
staging area
git add .
git commit -m "message"
repo
Commit Hashes
8ac18e2
a16a4f9
2f7de03
2f7de03
$ git log --oneline
2f7de03 Fourth
8417290 Third
a16a4f9 Second
8ac18e2 First
Each commit has a unique commit hash that refers to it
"First"
"Second"
"Third"
"Fourth"
HEAD
Undoing Changes
View commit history
git log [--oneline]
Restore previous version of a file
git checkout COMMIT_HASH FILENAME
Working with GitHub
Joel Ross
A web service that stores git-managed projects online. You can use git to upload or download projects from GitHub.
git is the application that manages versions of your code. GitHub is just a website where those versions can be stored remotely.
Repos on GitHub
GitHub hosts repositories, each of which can be found at a different address.
Example: https://github.com/dpla/black-womens-suffrage(source code for https://blackwomenssuffrage.dp.la/)
Forking
Forking creates a copy of a repo on GitHub's computers, but under a different user/organization account.
info340/repo
student/repo
GitHub Classroom
Assignments use a tool called GitHub Classroom to automatically create private copies (forks) of repos for assignments.
DO NOT "FORK" ASSIGNMENT REPOS
Push to GitHub
Upload commits to the GitHub cloud repo.
git push -u origin main
Upload to the origin remote, main branch
The -u sets branch tracking so that future push commands go to correct place.
git push
Push to the remote
Git Commands II
Copy repo to local machine
git push
Upload commits
git pull
Downloads and merges commits
git clone url
Using GitHub (Assignments)
edit files
staging area
git add
git commit
git clone
your machine
git push
your copy
follow assignment
link
Collaboration
Multiple people's local repositories can be linked to the same remote repository, allowing them to push and pull to the same central location.
Git and Branching
Joel Ross
Commit History
"First"
git commit -m "First"
"Second"
"Third"
"Fourth"
git commit -m "Second"
git commit -m "Third"
git commit -m "Fourth"
Git history has been a linear sequence of commits.
HEAD
HEAD
HEAD
HEAD
Branches
Branches allow for non-linear commits.
main
main
main
main
experiment
bugfix
experiment
experiment
Branch Commands
git branch
List available branches
git branch [my_branch]
Create a new branch called "my_branch"
git checkout [my_branch]
Switch to branch "my_branch"
git checkout -b [my_branch]
Create and switch to branch "my_branch"
git branch -d [my_branch]
Delete branch "my_branch"
Branches
main
main
experiment
experiment
HEAD
HEAD
HEAD
HEAD
git branch experiment
git checkout experiment
git commit
git commit
git checkout main
git commit
git checkout experiment
HEAD
HEAD
experiment
HEAD
Merging
We can
merge
two branches back together, producing a commit that contains the combined changes from both branches
main
main
experiment
HEAD
HEAD
Merging
git merge [other_branch]
Merges changes from other_branch into the current branch.
A new commit is created on the current branch containing the merged content.
What happens if branches have conflicting code changes?
Merge Conflicts
A merge conflict occures when two commits from different branches include different changes to the same code. Git does not know which version to keep, so makes you choose.
Merge conflicts must be resolved manually
Merge conflicts are a common and normal part of merging. They are the "typos" and "bugs" of using git.
Resolving Merge Conflicts
In order to resolve a conflict, you need to edit the file (code) so that you pick which version to keep. git will add marker content where you need to make a decision:
<<<<<<< HEAD
// This is the code from the "local" version (the branch you merged INTO)
// a.k.a the version from the HEAD commit
const message = "I am an original";
const lyric = "I've got no strings to hold me down";
// There can be multiple lines that conflict, including lines being deleted
=======
// This is the code from the "remote" version (the branch you merged FROM)
const message = "I think I'm a clone now...";
// The lines need not be related in content, they've just changed in a way
// that git can't figure out which to keep!
>>>>>>> f292a3332aedc8df3e8e8cf22ca3debc214c6460
the two versions to pick from
a divider between the versions
end conflict area
git add .
git commit -m "Merge branch 'other'"
- Use
git status
to see which files have merge conflicts. Note that files may have more than one!
- Fix the code so that it reflects the "final" version you want.
- Remember to delete the
<<<<<<<
and=======
and>>>>>>>
!!
- Once you're satisfied that the conflicts are all resolved,
add
andcommit
your changes (the code you "modified" to resolve the conflict):
Resolving Merge Conflicts
Merge conflicts are normal and expected
Pulling and Merging
git pull
Shortcut for
git fetch
then
git merge
downloads commits
The git pull command also will perform a merge; any commits from the remote "branch" will be merged into the local branch.
That means that you may need to resolve merge conflicts when pulling!
may have conflicts!
Pushing and Merging
Pushing commits to Github may require merging if the commits are interwoven. Git will not allow such pushes. Instead, you need to pull changes in order to merge them, and then push the new combined/merged commits.
# attempt to push, may be blocked
git push
# IF BLOCKED, pull down the remote commits
git pull
# IF ANY CONFLICTS, resolve them!
git add .
git commit -m "Resolve merge conflict"
# now push combined commit history
git push
Branches on GitHub
Joel Ross
GitHub and Branches
Because GitHub just hosts normal repositories, GitHub has branches as well! These can (but need not) correspond with the branches on your local machine.
Remote Branch Cmds
git branch -a
List all branches (including remote ones)
git pull [remote] [branch]
Shortcut for
git fetch
then
git merge
git fetch
Import remote branches into local repo
Are still listed as "remote" branches that need to be merged
Can cause conflicts!
git push [remote] [branch]
Remote Branch Cmds
Upload commits to specific remote and branch.
git push [remote] --all
Push commits from all branches to all branches
GitHub Pages
A GitHub service that provide (free) web hosting for repository content.
The content of the gh-pages branch will be available at https://username.github.io/reponame.
To "publish" your webpage, just push your content to that branch.
Development vs Production
# do all your coding on `main`!
# switch to gh-pages ("production" branch)
git checkout gh-pages
# merge the changes from main
git merge main
# push to GitHub to publish
git push origin gh-pages
# switch back to main branch for more coding
# DON'T FORGET THIS STEP!!
git checkout main
# remember to push your souce code
git push origin main
IMPORTANT: The gh-pages branch is your "production" branch. Never make changes on production. All changes should be made on the main branch, and then merged into production gh-pages branch.
info340-git
By Joel Ross
info340-git
- 166