git in action

42
Git in Action

Post on 22-Oct-2014

1.358 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Git in Action

Agenda Introduction

Action Q&A

Introduction

Distributed Version Control

Repository

Repository

Local

Remote(public or privat)

Repository

Repository

RepositoryRepository

Repository

Workspace Workspace Workspace

Alice Bob Charlie

push

pull

Git repositoryrepository = object database

.git/objects

Git repositoryid(object) = SHA1(object content)

34da

3daa

12df

45f5

675b

aabc

456c

90ff

78dd

aaccddaa

4afd

98738810

57aa

.git/objects/45/6c...

Git repositorycontent is stored in blobblob objects => File

34da

3daa

12df

45f5

675b

aabc

456c

90ff

aaccddaa

4afd

98738810

57aa

78dd

Git repositorystructure is stored in treetree objects => Directory

34da

3daa

12df

45f5

675b

aabc

456c

90ff

aaccddaa

4afd

98738810

57aa

78dd

Git repositoryHistory is stored in commitcommit objects =>

Authenticated hierarchical snapshots

34da

3daa12df

45f5

675b

aabc

456c

90ff

aacc

ddaa

4afd

9873 8810

57aa78dd

Git repository

Git repositoryreferences are stored as tagtag objects =>

(Signed)symbolic link

34da

3daa12df

45f5

675b

aabc

456c

90ff

aacc

ddaa

4afd

9873 8810

57aa78dd

Git repositoryrepository entry point =>

symbolic link

34da

3daa12df

45f5

675b

aabc

456c

90ff

aacc

ddaa

4afd

9873 8810

57aa78dd

tags

masterbranches

Action

The most important commands

add

bisec

branch

checkout

clone

commit

config

diff

fetch

grep

init

log

merge

mv

pull

push

rebase

remote

reset

rm

show

stash

status

tag

Configuration in $HOME/.gitconfig

git config --global user.name "Some name"

git config --global user.email some@email

git config --global color.branch auto

git config --global color.diff auto

git config --global color.interactive auto

git config --global color.status auto

git config --global merge.tool meld

git config --global core.editor vim

Initializing a repository Create a new repository

git init

Clone an existing repository

git clone

Possible URLs:

local directory, ssh://, git://, rsync://, ftp:// etc.

Show the status Simply call git status

This will show the lifecycle of the files

A file which is indexed and modified afterwards, must be added explicitly to the index again!

The Index

RepositoryIndexWorking Copy

git add,rm, mv git commit

Buffer between working copy and repository

Describes what's commited next

Shortcut: git commit -a

Commit Changes stored in the index are written into the repository and get a new SHA1 checksum

The HEAD-reference points to the new commit

The last commit can be changed and re-committed using the --amend parameter, e.G. typos in the comment, missed changes to be commited etc.

git commit --amend

Show differences Between workspace and index

git diff

Between index and the last commitgit diff --staged

Between the workspace and the last commitgit diff HEAD

Between two commits git diff $commit $commit

Between current branch and another onegit diff branch_name

Object references SHA1: d37f32a8058b2c4b5d1b1c55c4cab41611899cb3

Short SHA1: d37f32a

Tags: v1.5.1

Local branch: master

Remote branch: origin/master

Checkout: HEAD

Last Fetch: LAST_FETCH

Previous Head: ORIG_HEAD

Object references Parents: Name^, Name^^^, Name~10, Name^2,

What was yesterday? Name@{yesterday}

What was on ...? Name@{1 June}

What was days before?Name@{3}

What happened since...? --since=2 weeks ago

What was until ...? --until=1 week ago

Who has...? --committer=pattern

What was between...? name1..name2

Show me the commit Prints the diffs in a commit

Shows diffs as well as statistics

git show git show --stat

Can be used with all object references

Reset git reset modifies different elements:

Variant 1: --hardThe HEAD, the index and all local modifications in the workspace will be erased!

Variant 2: --softThe HEAD will be overwritten. Previous commits will change to changes to be committed

Variante 3: --mixed (default)The HEAD and the index are overwritten

The HEAD is just a reference to a specific commit

Logs Shows the commit logs

git log git log -10

Can be used with all object references

git log -3 master@{15 July} git log --author=Max Mustermann git log --grep=pattern

Search in files git provides a grep integration

MUCH faster than standard grep

git grep -e pattern -- some/file

git grep -e pattern branch -- some/file

Tags Tags are named references to commits

There are annotated and lightweight tags

Creation of a tag

Lightweight: git tag Annotated: git tag -a -m message

Show all tags: git tag

Create and change branches

Branches are references to commits

The default-branch is called master

Create: git branch name [commit]

Change: git checkout name

Create and change: git checkout -b name

The HEAD will be adapted accordingly

Delete branches Branches can be deleted every time

To delete a merged branchgit branch -d branch_name

To delete a non-merged branchgit branch -D branch_name

Show all branches Local ones

git branch

Remote onesgit branch -r

All branchesgit branch -a

All non-merged branchesgit branch --no-merged

Conflicts and merging Merge as many branches as you want at the same time

git merge branch_a branch_b

Merged branches can also be deleted

Conflict markers indicates auto-merge problems

Show with git status or git mergetool

Use editor or mergetool to fix the conflict Stash the resolved conflict on the index Do a commit

Cherry-pick Sometimes you only want to merge specific commits into another branch

Git allows this cherry picking

git cherry-pick

Rebase Alternative to git merge

Simple rebase pushes the branch onto the HEAD

a

b

c

d

e

f

master

test

a

b

c

d

e

e'

master

testgit merge master

d'

git rebase master

Interactive rebase git rebase -i master

Handles multiple commits:

delete ignore edit squash together

You can squash multiple commits into one to have a clean history a

b

c

b

s(d)

e

master

d

f t(e+f)

Remote Branches Clone creates a new local branch within a namespace:

remotes/origin/

Default-name for remote server is origin

Can be changed using git remote Checkout a remote branch using

git checkout -b /

Remote branches are Tracking Branches

Adding remotes Unlimited number of remotes

Modifications with git remote

Typing git remote will list all remotes

Team members can add other team members remotes for easy code exchange

Pull the changes Tracking-branches know the SHA1 from the remote since the last pull

Changes are fetched usinggit fetch remote:branch

Note, these changes are not merged yet! Shortcut: Do a pull

git pull remote:branch

= git fetch + git merge

Provide your changes Store the commits on a central repository server

Not all local branches must be pushed private stuff keeps private!

git push remote:branch

NEVER change the history of distributed changes using rebase!

Stashing Common problem:

Current work must be pushed asside but the changes shouldn't be commited yet

Changes in the workspace/index must be transfered in another branch

Solution: The index and changed files are cached and the workspace and index is resetted

git stash save descriptiongit stash applygit stash pop # apply and drop

Stashing episode II Advantage: Stashing indexes the files and write them into the reflog

Even if these changes are never commited, you can get them back

git stash listgit log stash@{10}git show stash@{26}git checkout -b old_work stash@{32}

Use stashes simply like branches

The reflog exists independent from the commits

Blame Problem: You don't know who implemented the code snippet

Solution: git blame -- file shows a list of changes for a file and when and who modified it

Q&A

TitleSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42