An Introduction to Git and Github

Some basics

Phil Lamond

Disclaimer: I'm not a Git guru

If you want to hear it from the horse's mouth, watch Git by a git (Linus Torvalds who also created and maintains the Linux kernel project)

Or read the git manual page/tutorial

Getting Going with Git

  • A vanilla project

    
    git init
    								
  • Inform git about who you are

    
    git config --global user.name  "BBC Developer"
    git config --global user.email bbc.developer@bbc.co.uk
    								
  • And set up fancy colours in your terminal

    
    git config --global color.ui true
    								

Getting Going with Git

  • Get git to tell you what it thinks of your project

    
    git status
    								
  • Add the files you want under version control

    
    git add file1 file2
    								

Getting Going with Git

  • Commit your added files

    
    git commit
    								

    Or

    
    git commit -m "My awesome code changes"
    								
  • Or add and commit at the same time (catches all unversioned files)

    
    git commit -a
    								

Key Concepts

  • stage (commits)
  • origin (working copy)
  • attach to a remote
  • push to remote repo
  • pull from remote repo
  • branch which can be local and remote
  • master is remote head
  • merge a branch with one or more others...

Example

Imagine there are two developers working on the same project

  • Where am I at now?

    
    git status
    								
  • Better get changes from the remote master

    
    git pull
    								

    (combines git fetch and git merge)

Example (continued)

Good, right those changes merged ok

  • Now commit and push my work

    
    git status
    git commit -a -m "These changes will get me kudos, man"
    git push origin master
    								

Example (continued)

That pushed up alright.
Ruh oh... those changes were not cool, I forgot to write unit tests and I've b0rkened the app!
Should've made a branch coz I wasn't finished!

  • Quick, rewind! Ok, make a branch first

    
    git branch coolfeature
    								
  • Rewind the remote master back by one changeset (push) and switch to the new branch (with my bad changes preserved)

    
    git reset --hard HEAD^
    git checkout coolfeature
    								

Example (continued)

Push up the new branch

  • Now commit and push my work

    
    git status
    git commit -a -m "This will get me doughnuts and crisps, coz I now have unit tests"
    git push origin coolfeature
    								

Github

Github

  • 'Social coding'
  • Suits open source projects
  • Supports 'organisations'
  • Main remote git repository on the internet
  • Supports tickets (issues), documentation (pages), and notes/code snippets (gists)
  • Can 'fork' other projects at the click of a button

Github (continued)

  • Provides easy way to start a new project with git
  • Make a new Github repo with a README.md (description), then locally
    
    git clone git://github.com/phillamond/mycoolrepo
    								

    Checks out and attaches your local working directory to the remote master

Github (continued)

Killer feature: pull requests

  • Creating a pull request creates an issue
  • Can be assigned to another user for code review and merging
  • Clean merges can be done on github.com
  • Really granular commenting (even on lines of code commits)
  • Powerful vetting system for code contributions
  • Demo

    Conclusion

    Caveat: I've only covered a core subset of git commands and github.com usage

    Git is popular in the software development industry because of its really rich feature set that sets it apart from other (older) VCS systems like Subversion and CVS. It is used in earnest by big players like Google and Facebook and it seems a welcome move that the BBC are adopting it.