This presentation was created for
CSC 485E + SENG 480B at the
University of Victoria
Git basics
Advanced Git
Oh shit, git!
GitHub
Workflows
Continuous integration
R. Ian Bull [irbull]
Senior Software Architect (EclipseSource)
Eclipse Architecture Council, Planning Council,
RT Project Management Committee
Currently: JavaScript and Java Integration
An unpleasant or contemptible person.
A mature, well maintained, distributed version control system.
Have you ever done the following:
essay.txt
essay-reviewed.txt
essay-final.txt
essay-final-2.txt
essay-final-midnight.txt
Git is a version control system so you can manage the different revisions of a set of files.
Git is distributed, so several people can work simultaneously on the same project.
Several Git clients available:
We will use the command line to teach the basics.
Start by creating a Git repository.
$ git init $ git add a.text $ git commit
a.text
Checkout https://try.github.io to learn Git.
Write a detailed commit message:
Git takes care of the:
You should document:
The main tool you use to determine which files are in which state is the
git status
command. The status tool tells you:
Some advice on writing good commit messages:
http://chris.beams.io/posts/git-commit/
Commit logs should tell the story of how your software was developed.
Work on commits until they are perfect. Don't be afraid to amend commits, but once they are public, they are immutable.
Use git log
to see the history of your project. Use the --graph
argument
to see the merge paths.
Branching is the way to work on different versions of a repository at one time.
By default, your repository will have one branch named master
.
When you create a branch off master
, you're making a copy or a snapshot of
master
as it was at that point in time.
Create a new feature branch for each feature you work on.
$ git branch new-awesome-feature
Creates a new branch, but does not check it out. Use
git checkout -b <branch_name>
to create and checkout the branch.
Move between branches with git checkout
.
Merging is Git's way of putting a forked history back together again.
Once you've finished developing a feature, it's important to be able to get it back into the main code base.
$ git merge <branch>
Merge the specified branch into the current branch. Git will determine the merge algorithm.
Two common merging algorithms: Fast Forward or 3 Way Merge.
Fast Forward: Used when there is a linear path from the current branch tip to the target branch. Instead of actually merging, Git just integrates the histories.
3 Way Merge: Used when two branches have diverged. In this case, a new commit is created to tie the two branches together.
If two commits change similar parts of the same file, Git won't be able to figure out which one to use. Git will stop the merge so you can resolve the conflicts manually.
git add <file_with_conflict>
to mark as resolvedgit commit
to generate the merge commit90% of all activity you perform will be on your own Git Repository. However, to be able to collaborate, you need to manage your remote repositories.
GitHub is an example of a remote repository, but it's just one example. Remote repositories are versions of your project that are hosted on the Internet or network somewhere. You can have multiple remote repositories.
Use git remote -v
to list your remotes.
Use git remote add <shortname> <url>
to add a new remote.
If you clone
a repository, Git will automatically add a remote named
origin
for you.
git fetch [remote-name]
will pull down all the data from that remote.
git pull [remote-name]
will automatically fetch and merge for you. This
requires your current branch to be configured to track a remote branch.
When you are ready to push data, usegit push [remote-name] [branch-name]
.
If you are not up to date you will need to fetch their work and incorporate it into yours before you can push.
If you want to switch branches to work on something else, but are not ready
to commit your current work yet, use the git stash
command.
Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any time.
Use git stash pop
to apply the most recent stashed change and then
drop it from your stack.
Use git stash list
to see the stack of stashes you have.
Use git stash apply
to apply a specific stash.
Rebasing is an alternative to merging when integrating two branches. Unlike
merging, rebasing takes all the changes that were committed on one branch and
applies them to another.
Consider change-set C4. Instead of merging C4 and C5, C4 can be reapplied after C5. This results in a new commit C4'.
Often you'll rebase to make sure your commits apply cleanly on a remote branch.
rebase origin/master
Do not rebase commits that exist outside your repository.
Should I rebase or should I merge:
Rebasing tells the story of how your software was made. This often produces more readable commit histories. Merging tells the story of exactly what happened. This shows every path that was taken, and every wrong turn that was made.
git add
adds the file to the indexgit commit
commits indexgit reset
can be used to reset where HEAD pointsReset is a very powerful command, but make sure you understand what it's doing! Read Reset Demystified.
Every data storing action (commit, reset, pull, etc...) is stored in the
reflog. Use git reflog
to see this log. Use this as your safety net.
Reflog combined with checkout and reset can be used to surgically fix your git repository.
Add a .gitignore
file to your repository to intentionally specify files
to ignore. Good candidates are binary files, log files, temporary files, etc...
Checkout Git Ignore Templates for a
collection of .gitignore
files.
Git can be hard sometimes, and with teams you may find your repository in an inconsistent state. However, almost everything can be recovered.
Git is really just a Directed Acyclic Graph (DAG) of commits, and most git commands simply modify that graph. If you get into trouble, you can usually re-modify the graph as you need.
Since you are all computer scientists, checkout:
http://eagain.net/articles/git-for-computer-scientists/
And if your really stuck, checkout:
http://ohshitgit.com/
Oh shit, Git! has solutions to several common problems:
GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.
Create a new repository for your project, or fork an existing repository.
A fork is a personal copy of another user's repository that lives on your account. Forks allow you to freely make changes to a project without affecting the original.
Pull requests are proposed changes to a repository submitted by a user and accepted or rejected by a repository's collaborators.
Pushing refers to sending your committed changes to a remote repository such as GitHub.com.
With GitHub you will each have a fork of your repository. Designate one as the master. You can work together or individually on some feature. When you are ready, you can push your feature to your repository and issue a pull request to synchronize with the master repository.
GitHub signup is free, and repositories are free for Open Source projects. If you don't have a GitHub account yet, signup now!!!.
GitHub also provides a Student Developer Pack filled with goodies for students like you!
Use GitHub Issues to track work items. Create issues for the work you need to do, and assign them to members of your group.
@mention
to directly reach another member of your teamfixes #123
to automatically close issuesGitHub Projects seamlessly integrates project management into your development process.
Create a project for a specific milestone or sprint and organize your work-items.
GitHub supports several different workflows. We already discussed the Fork and Pull model with separate repositories. You can also work in a shared repository model where everyone shares a single repository.
In this model, you should create feature branches for each feature you are working on. Iterate on the feature (either collaboratively or on your own), and when you are satisfied with the feature, issue a pull request.
Tip: Anything in master
should always be deployable!
Tip: Pull requests are just the beginning. Use the pull request to discuss / review the code and improve it.
Tip: Test everything in master. If changes cause problems, revert them.
Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day.
Each commit (or at least each push) to a feature branch should trigger a build.
If any of these fail, disallow any merging / pull requests!
This gives you great confidence that you are not introducing any regressions
and that master
is always deployable. However, this only works if you
write tests!
Checkout https://travis-ci.org/ a free Continuous Integration service that can be integrated with GitHub.
This presentation was prepared using remark.js and authored in Markdown. Markdown is rendered as HTML in-browser (no compile step).
I hosted this presentation using GitHub sites, and used a Git Repository to manage it. Feel free to fork it, contribute pull requests or use it as a template for your next presentation.
Git basics
Advanced Git
Oh shit, git!
GitHub
Workflows
Continuous integration
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |