Git and github - Verson Control for the Modern Developer

Post on 18-Jan-2017

876 views 5 download

Transcript of Git and github - Verson Control for the Modern Developer

Git & Github - version control for the modern developerby John Stevenson @jr0cket

git.practical.li

Why Git / Version controlHelping us manage and collaborate

We learn from history, so don't delete it

Those who cannot remember the past are condemned to repeat it

- George Santayana

History at your fingertipsgit log --oneline --decorate

Version control should be easy

Every developer should be able to manage changes without a dedicated version control team

- makes developers more likely to version their software more often- smaller commits- meaningful commit messages - easy merging of changes- easy to experiment & throw away code

What is Git...it's not just me when I am being grumpy...

Distributed vs Centralised model

Distributed vs Centralised modelCentralised

- only 1 server, single version of the truth- typically a locking approach - requires central server setup- single point of failure in central server- only use when connected to the network

Distributed

- only requires git init to start versioning- have the whole project to work with- multiple repositories give redundancy- commit changes offline & fast- easy to share changes & collaborate- need to decide canonical source of truth

Distributed model in action

Getting Started with Git...it's not just me when I am being grumpy...

Install a Git Client

Install your prefered Git Client

git-scm.com/downloads/guis

Identifying yourself with Git

Labels every commit you make with your identity

git config --global user.name “John Stevenson”

git config --global user.email “john@jr0cket.co.uk”

Git graphical clients also allow you to set your identity and usually save this into your ~/.gitconf file

Tools to Gitchoose your tools and learn them well...

- Windows & MacOSX

desktop.github.com

Emacs Magit - very powerful & easy to use

SourceTree - Atlassian

GitKraken.com

Git Command Line

Git local workflowversion your changes locally, before sharing them

Create a local repository: git init

Edit your source code (working copy)

Add changes you want to capture in the next commitgit add filename or file pattern

See what files you have changed / addedgit status

Compare changes in working copygit diff filenamegit diff --cached filename

Create a commit from the added files: git commit -m “meaningful message”

See the current history of commitsgit log --oneline --decorate

See the contents of a commitgit show commit-number

Dont delete the .git directory or you loose all the history of the project

Git Visual Cheatsheet ndpsoftware.com/git-cheatsheet.html

Collaborating with Githubadditional services around Git

What is Github

Github is a cloud service for managing & sharing changes

- provides private & public repositories- repository forks & pull requests- collaborative code review- Gists for single file sharing

Github pages website publishing

- markdown driven content

Authentication methods - SSH Keys vs PasswordsSSH authentication

- uses a public/private key to authenticate, so no passwords - keys needs to be added to each computer you use

Password authentication

- account can be cached to minimise typing of credentials

Creating a repository

Cloning a Github RepositoryClone (copy completely) an existing repository, giving an optional directory name

git clone URL directory-name

Forking a Github RepositoryYou can fork to your account or any Github Organisation you belong too

Submit a pull request

Github Pull Request - Accepted & Merged

Github Pull Request - Code Review

Visualising Github - ghv.artzub.com

Branching & Mergingbranches should allow experimentation, merging should be simple

BranchingBranches allow you to work on

- specific features- bug fixes - ideas / experiments

Branches can

- merged into another branch- have commits cherry picked- be discarded easily- be attached to another branch or be

stand alone (eg. gh-pages)

Creating a branchgit branch branch-name ;; create a branch attached to the current branch

git checkout branch-name ;; checkout branch so new commits are added to it

git checkout -b branch-name ;; both commands above in one command

git branch ;; lists all branches

Creating a stand alone (orphan) branch

git branch -o branch-name ;; branch independent of others & shares no history

Branching - discarding

Branches can live forever, the longer they live become harder to merge git branch -d branch-name

If this branch is also on a remote git repository, you need to push this changegit push remote branch-name

Or simply delete a branch reference on the remote git repositorygit push remote --delete branch-name

Merging

Checkout branch to receive the merge commits

git checkout master

git merge feature

Rebasing - only with consent!

Note: Anything that affects the history should be done with consent,especially once commits are shared

Checkout branch to receive the merge commits

git checkout feature

git rebase master

Cherry Picking

Checkout branch to receive the cherry picked commit

git checkout master

git cherry-pick commit-number

Deploying with GitGit can be an important part of your release management

Example: Heroku

git push heroku master

Example: Heroku

git push heroku master

Learning Git in-depthwe learn by example...

Books on Git

progit.org

gitforteams.com

atlassian.com/git/tutorials

Take your own journey into Git

Thank you

@jr0cketjr0cket.co.uk