40
© 2013 Cambridge Technology Partners 1 Copyright © 2013 Cambridge Technology Partners All Rights Reserved. Cambridge, its logo, and Get IT Right are trademarks of Cambridge Technology Partners. Get IT right Git Essentials Bartosz Majsak (@majson), Thomas Hug (@gizmoo360) http://ctp.com http://github.com/ctpconsulting

JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

Embed Size (px)

DESCRIPTION

http://guide13.jazoon.com/#/submissions/156

Citation preview

Page 1: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 1 Copyright © 2013 Cambridge Technology Partners All Rights Reserved. Cambridge, its logo, and Get IT Right are trademarks of Cambridge Technology Partners.

Get IT right

Git Essentials

Bartosz Majsak (@majson), Thomas Hug (@gizmoo360) http://ctp.com http://github.com/ctpconsulting

Page 2: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 2

Bartosz Majsak Java Developer by day

Open source junkie by night (Arquillian core team member)

Conference speaker by passion (Devoxx, Jazoon ...)

10/25/2013

About Us

Thomas Hug With Cambridge Technology Partners since 2002

Java Developer, TTL, Solution Architect

Apache Committer, OSS contributor and aficionado

Page 3: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 3 10/25/2013

Why do we recommend Linux?

Page 4: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 4

Git a British slang term meaning a contemptible person, a bastard.

10/25/2013

Page 5: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 5

Founded 2005 as a replacement of BitKeeper

VCS of Linux Kernel

…not just Linux anymore

10/25/2013

Git History

Page 6: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 6

No Central Server – Distributed VCS

Performance and Efficiency

Robustness

10/25/2013

Git Concepts

Page 7: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 7

Disclaimer when we say repository we actually

mean local repository (no network connectivity)

10/25/2013

Page 8: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners

Installing and Configuring Git

Page 9: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 9

msysgit

cygwin

Atlassian SourceTree

10/25/2013

Installation

XCode

Homebrew

MacPorts

Package Manager

Page 10: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 10

Playground Objectives: getting familiar with essential commands and making your life easier

touch

cat / less

mkdir

ls

tree

cp / rm / mv

nano

history / ctrl+shift+r

10/25/2013

Command line essentials

Page 11: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 11

Your contact details

$ git config --global user.name “Bruce Wayne”

$ git config --global user.email “[email protected]

$ less ~/.gitconfig

SSH Key generation

$ ssh-keygen -t *dsa -C [email protected]

*Using SHA-2 underneath. Approved by NSA

10/25/2013

User Identity

Page 12: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 12

Color output

$ git config --global color.ui auto

Aliases. Useful for stuff impossible to remember…

$ git config --global alias.showlog "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

10/25/2013

Presets

Three levels of configuration: --local (default, per repo) --global (per user) --system (machine)

Page 13: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 13

Git References http://git-scm.com/ - official Git Home

http://git-scm.com/book - Pro Git (Apress) online version

http://git-scm.com/docs - Reference Documentation

https://www.atlassian.com/git/tutorial - Git Tutorial

http://gitready.com/ - Git Tutorial

Workflows https://www.atlassian.com/git/workflows - Tutorial on common Git workflows

http://yakiloo.com/getting-started-git-flow/ - About Git Flow (advanced topic)

Getting Help http://stackoverflow.com/ - All things programming

https://help.github.com/ - Git Recipies

10/25/2013

Reference Material

Page 14: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners

First Repository

Page 15: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 15

$ mkdir myrepo

$ cd myrepo

$ git init

$ git ls -la

10/25/2013

Creating a Repository

Do this in one swoop with $ git init myrepo

git init

Page 16: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 16

$ touch index.html

$ git status

$ git add index.html

$ git status

10/25/2013

Adding Files

git add works also with patterns: $ git add ‘*.java’ $ git add . $ git add folder/ You can even stage parts of a file $ git add –p|-i Stage all changes (including deleted files) in the working directory with $ git add -A .

git status

git add

Page 17: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 17

$ git commit

$ git status

$ git log --oneline --decorate

Committing Files

Commit directly with commit message: $ git commit -m ‘Been there, done that’ $ git commit -am ‘Add also modified files directly’

git commit

Need a different commit editor? export EDITOR=vim

Page 18: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 18 10/25/2013

Three Stage Thinking

Working Directory

Staging Area

Repository History

add

commit

checkout

Page 19: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 19

$ touch test1.log test2.log

$ git add test1.log

$ git commit

$ vim .gitignore

$ git status

$ git rm test1.log

$ git commit

Deleting and ignoring Files

.gitignore

git rm

A shell script for easily accessing - gitignore boilerplates https://github.com/simonwhitaker/gitignore-boilerplates

$ gibo Java Eclipse >> .gitignore

Page 20: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 20

git log gives you and overview of your repository

structure.

$ git log

$ git log -p

$ git log --oneline --decorate

$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

10/25/2013

How does my repo look like?

Page 21: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 21 10/25/2013

The Git File Workflow

add edit

add

ignore

commit rm

ignored untracked unmodified modified staged

Page 22: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners

Branching and Merging

Page 23: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 23

$ git branch mybranch

$ git branch

$ git checkout mybranch

Delete the branch with

$ git branch -d mybranch

$ git branch -D mybranch # if unmerged

Branching

Create a branch and check it out in one swoop $ git checkout -b mybranch

git branch

Page 24: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 24

Objectives: Getting familiar with branching and tagging.

Create new branch and modify repository

Switch between branches

Delete branch

Tag commits

$ git branch

$ git checkout

$ git tag

10/25/2013

Time for some serious work

Page 25: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 25

$ git status # staged stuff

$ git stash

$ git status

$ git stash list

$ git stash apply [--index]

$ git stash drop stash@{0}

I’m not done yet

git stash

Apply and remove stash in one swoop $ git stash pop

Page 26: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 26

Featu

re

$ git checkout master

$ git branch mybranch

$ git showlog

$ git branch

Merging

Deactivating fast-forward merges per branch $ git config branch.master.mergeoptions "--no-ff"

git merge

Fast-forward is default

$ git merge --no-ff Featu

re F

eatu

re

Page 27: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 27

$ git diff mybranch master

Diff works also on the branch history

$ git diff # unstaged

$ git diff HEAD^^ HEAD # from to

$ git diff hash1...hash2 # from to

Diffs

git diff

Page 28: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 28

$ git checkout master

$ git rebase mybranch

Rebasing

Rewriting history: Interactive rebase last four commits $ git rebase --i HEAD~4

git rebase

Page 29: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 29

Objectives: Learn how rebase works.

Make changes on selected branch and rebase it with master

$ git rebase <BRANCH>

10/25/2013

All your base are belong to us

Page 30: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners

Going Remote

Page 31: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 31

$ git clone [#remote]

Cloning Repositories

git clone

Clone into a specific or existing (empty) folder $ git clone [#remote] myclonedrepo

Page 32: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 32

ssh / git: Securely connect to remote machines git clone [email protected]:ctpconsulting/jazoon-git-workshops.git

HTTPS: Firewall friendly git clone https://github.com/ctpconsulting/jazoon-git-workshops.git

File – simple. Can be used with e.g. a shared drive git clone file:///home/thug/repo/chopen-workshop-git

10/25/2013

Git Protocols

Cloning directly without the file protocol will use hard links $ git clone /home/thug/repo/jazoon-git-workshops

Page 33: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 33

$ git init myremoterepo

$ cd myremoterepo

$ … # commit something

$ git remote add origin [#remote]

$ git remote -v

Remotes

git remote

Git is distributed – you can have more than one remote! $ git remote add https-origin https://myrepo.com/repo.git

Page 34: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 34

$ git push -u origin master

$ …

$ git push

Forced push

$ git push --force

Submitting Changes

git push

By default, Git always tries to push all matching branches. Configuration to push only current to upstream: $ git config push.default upstream

Page 35: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 35

$ git fetch

$ git merge origin/master

Or short-hand

$ git pull

Retrieving Changes

git fetch

Resolution strategy for merge conflicts $ git pull -Xours $ git pull -Xtheirs

git pull

Page 36: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 36

$ git pull --rebase

During a regular daily workflow where several team members sync a single branch often, the timeline gets polluted with unnecessary micro-merges on a regular git pull. Rebasing ensures that the commits are always re-applied so that the history stays linear.

Retrieving Changes

Make git pull on master always use rebase $ git config branch.master.rebase true Or make it a default for every tracking branch strategy $ git config --global branch.autosetuprebase always

Page 37: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 37

Page 38: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 38

Page 39: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 39

Get IT right

Thank you!

Page 40: JAZOON'13 - Thomas Hug & Bartosz Majsak - Git Workshop -Essentials

© 2013 Cambridge Technology Partners 40

Icons provided by Icons8: http://icons8.com/

10/25/2013

Credits