+ - 0:00:00
Notes for current slide
Notes for next slide

Fork me on GitHub

Git, GitHub and Working Together

Ian Bull (irbull)

This presentation was created for
CSC 485E + SENG 480B at the
University of Victoria

Startup Programming

1 / 32

Agenda

Git basics

Advanced Git

Oh shit, git!

GitHub

Workflows

Continuous integration

2 / 32

Introduction

R. Ian Bull [irbull]

Senior Software Architect (EclipseSource)

Eclipse Architecture Council, Planning Council,
RT Project Management Committee

Currently: JavaScript and Java Integration

3 / 32

What is Git

An unpleasant or contemptible person.

A mature, well maintained, distributed version control system.

  • Branching and merging with ease
  • Small and fast
  • Distributed
  • Data assurance
  • Free and open source
4 / 32

What is Git

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.

5 / 32

What is Git

Git basics

Several Git clients available:

  • IDE Integration (Eclipse, IntelliJ IDEA, etc...)
  • gitk, git-gui, gitkraken, SourceTree
  • GitHub for Mac and Windows
  • Command line

We will use the command line to teach the basics.

Start by creating a Git repository.

$ git init
$ git add a.text
$ git commit
  1. Create a Git repository
  2. Add file a.text
  3. Commit the change-set



Checkout https://try.github.io to learn Git.

6 / 32

What is Git

Git basics

- Commits

Write a detailed commit message:

Default-aligned image

Git takes care of the:

  • Who: author who committed the change-set
  • When: timestamp of the change-set
  • How: The code you committed

You should document:

  • What: What does this change-set do
  • Why: Why is this important
7 / 32

What is Git

Git basics

- Commits

- Status

The main tool you use to determine which files are in which state is the git status command. The status tool tells you:

  • The files that have changed
  • The files that have been staged (changed, and ready for commit)
  • The files that are untracked (not part of the repository)
  • The files that have been deleted

8 / 32

What is Git

Git basics

- Commits

- Status

- Logs

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.

9 / 32

What is Git

Git basics

- Commits

- Status

- Logs

- Branches

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.

10 / 32

What is Git

Git basics

- Commits

- Status

- Logs

- Branches

- Merging

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.

11 / 32

What is Git

Git basics

- Commits

- Status

- Logs

- Branches

- Merging

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 will tell you which files have conflicts (and where)
  • Manually resolve the conflicts
  • run git add <file_with_conflict> to mark as resolved
  • run git commit to generate the merge commit
12 / 32

What is Git

Git basics

- Commits

- Status

- Logs

- Branches

- Merging

- Remotes

90% 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.

13 / 32

What is Git

Git basics

- Commits

- Status

- Logs

- Branches

- Merging

- Remotes

Fetching and Pulling from Remotes

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, use
git push [remote-name] [branch-name].

  • You must have write access to the remote repository
  • You must be up to date

If you are not up to date you will need to fetch their work and incorporate it into yours before you can push.

14 / 32

What is Git

Git basics

Advanced

  • Git is often considered overly complex
  • Makes easy things easy and hard things possible?
  • Easy is objective
15 / 32

What is Git

Git basics

Advanced

- Stash

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.

16 / 32

What is Git

Git basics

Advanced

- Stash

- Rebase

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'.

17 / 32

What is Git

Git basics

Advanced

- Stash

- Rebase

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.

18 / 32

What is Git

Git basics

Advanced

- Stash

- Rebase

- Reset

  • Work on files in your current working directory
  • git add adds the file to the index
  • git commit commits index
  • git reset can be used to reset where HEAD points

Reset is a very powerful command, but make sure you understand what it's doing! Read Reset Demystified.

19 / 32

What is Git

Git basics

Advanced

- Stash

- Rebase

- Reset

- Reflog

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.

20 / 32

What is Git

Git basics

Advanced

- Stash

- Rebase

- Reset

- Reflog

- GitIgnore

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.

21 / 32

What is Git

Git basics

Advanced

Oh Shit!

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/

22 / 32

What is Git

Git basics

Advanced

Oh Shit!

Oh shit, Git! has solutions to several common problems:

  • Oh shit, I did something terribly wrong, please tell me git has a magic time machine!?!
  • Oh shit, I committed and immediately realized I need to make one small change!
  • Oh shit, I need to change the message on my last commit!
  • Oh shit, I accidentally committed something to master that should have been on a brand new branch!
  • Oh shit, I accidentally committed to the wrong branch!
  • Oh shit, I tried to run a diff but nothing happened?!
23 / 32

What is Git

Git basics

Advanced

Oh Shit!

GitHub

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.

  • Code hosting
  • Issue tracking
  • Project management
  • Access control
  • Wiki and other documentation tools
24 / 32

What is Git

Git basics

Advanced

Oh Shit!

GitHub

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.

25 / 32

What is Git

Git basics

Advanced

Oh Shit!

GitHub

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!

26 / 32

What is Git

Git basics

Advanced

Oh Shit!

GitHub

Issues

Use GitHub Issues to track work items. Create issues for the work you need to do, and assign them to members of your group.

  • Start a discussion about the work item
  • Use @mention to directly reach another member of your team
  • Link to commit IDs directly from the conversation
  • Use commit messages with syntax such as fixes #123 to automatically close issues

Closing issues via commit messages

27 / 32

What is Git

Git basics

Advanced

Oh Shit!

GitHub

Issues

Projects

GitHub Projects seamlessly integrates project management into your development process.

Create a project for a specific milestone or sprint and organize your work-items.

28 / 32

What is Git

Git basics

Advanced

Oh Shit!

GitHub

Workflows

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.

29 / 32

What is Git

Git basics

Advanced

Oh Shit!

GitHub

Workflows

C.I.

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.

  • Does the code compile
  • Do the tests pass
  • Do the linters (code style checker) produce any warnings

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.

30 / 32

About

This presentation was prepared using remark.js and authored in Markdown. Markdown is rendered as HTML in-browser (no compile step).

  • Markdown formatting, with smart extensions
  • Style it using regular CSS
  • Host it wherever you want (including locally)

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.

32 / 32

Agenda

Git basics

Advanced Git

Oh shit, git!

GitHub

Workflows

Continuous integration

2 / 32
Paused

Help

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