33
1 / 33 Git - distributed VCS Oleg Batrashev April 8, 2009

Git - distributed VCSds.cs.ut.ee/courses/previous/seminar-materials/git-vcs.pdf · Thoughts on git Overview Tutorial way, not my way today Thoughts on git Clarification Git architecture

  • Upload
    hathien

  • View
    264

  • Download
    0

Embed Size (px)

Citation preview

1 / 33

Git - distributed VCS

Oleg Batrashev

April 8, 2009

Overview

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

2 / 33

■ architecture

◆ working directory, index, repository◆ objects: blobs, trees, commits, tag objects◆ refs: heads, tags, remote heads

■ local usage

◆ init or clone, status, add, rm, commit◆ log, diff, ls-files, ls-tree◆ branch, checkout, merge, reset, revert

■ remote usage

◆ remote, fetch, pull (fetch +merge), push

■ advanced usage: cherry-picking, rebase, blame■ quick look at GUI

Tutorial way, not my way today

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

3 / 33

■ I could start like this

$ mkdir newproject$ cd newproject/newproject$ git initInitialized empty Git repository in /home/olegus/newproject/.git/newproject$ echo "Hello git" > newfilenewproject$ git add newfilenewproject$ git status# On branch master## Initial commit## Changes to be committed:# (use "git rm --cached <file>..." to unstage)## new file: newfile#newproject$ git commit -m "First commit"Created initial commit baefc43: First commit1 files changed, 1 insertions(+), 0 deletions(-)create mode 100644 newfile

Thoughts on git

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

4 / 33

■ Linus Torvalds created git for linux kernel

◆ support tools and workflows he needed◆ did not care much about “a typical developer”

■ as the consequence git has

◆ clean, simple, and powerful underlying model◆ ad-hoc high-level interface

■ I had 2 difficulties while learning git

◆ many new terms and ideas

■ refs, remotes, trees, index, pull, push, fetch

◆ many commands (git-〈TAB〉〈TAB〉 shows 132)

I present git “bottom-up”, starting from architecture

Clarification

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

5 / 33

■ I use graphical git gui and gitk 90% of the time

◆ 5% it is easier to type git status◆ 5% I need something tricky

■ new git version have only ~20 commands

◆ these look like git <command> notgit-<command>

◆ others (low-level) are in /usr/lib/git-core

■ I start with architecture details because

◆ there are many git tutorials◆ most of the confusion can be eliminated◆ want to try different way :)

Git architecture

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

6 / 33

Working dir, index, repo

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

7 / 33

working dir

index

repository

stage

commit

■ working directory■ repository in .git/

1. objects in .git/objects/

◆ files and commit data

2. refs in .git/refs/

◆ named pointers to data

■ index (aka cache) in .git/index

Commit workflow

■ stage – add files to index for commit■ commit – use index to create new commit

Objects (1)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

8 / 33

■ an object is a data file, named by its SHA1 (object id).git/objects/0d/cb39fa2c8708eb1c6f98f6cdac6e14495a1fd2

. . . . . . file1

dir1file2

file3

file4

■ commit , consisting of

◆ tree id (representing full state after commit)◆ parent ids, message, author, time

■ tree – maps names to trees/blobs

◆ typical directory

■ blob – just file contents

Objects (2)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

9 / 33

■ Show commit object

newproject$ git log --pretty=rawcommit baefc43bc36f916d7d346851f8e837739896e57ctree 6b9ca16d16a94ce72cfec460ce7620f62dbf42beauthor Oleg Batrashev <[email protected]> 1238354162 +0300committer Oleg Batrashev <[email protected]> 1238354162 +0300

First commit

■ Show tree object, that commit points to

newproject$ git ls-tree 6b9ca1100644 blob 0dec2239efc0bbfabe4078f5357705ca93b5475e newfile

■ Show blob object, that newfile is mapped to

newproject$ git cat-file blob 0dec2Hello git

Refs

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

10 / 33

■ A ref is just a text file with commit id

◆ commit of a branch or a tag

master

branch-1

tag-1

■ Branches (heads) are in .git/refs/heads/

newproject$ cat .git/refs/heads/master0b761457f3d2476619c1b0e540ebaef3b5fa8ccc

■ Tags are in .git/refs/tags/

Remote refs

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

11 / 33

■ Refs that are only updated from remote repository

◆ new objects are transfered as needed

master

branch-1

remotes/origin/master

master

last fetch

original clone

remote repository

■ Remote branches are in .git/refs/remotes/

Specifying revisions (1)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

12 / 33

git-rev-parse --help

■ SHA1, unique prefix of SHA1: dae86e■ symbolic ref name: master or

ref/remotes/usb/master

1. if $GIT_DIR/<name> exists, that is what you mean(HEAD, MERGE_HEAD, ORIG_HEAD,FETCH_HEAD)

◆ HEAD is current branch

2. $GIT_DIR/refs/<name> if exists3. $GIT_DIR/refs/tags/<name> if exists4. $GIT_DIR/refs/heads/<name> if exists;5. $GIT_DIR/refs/remotes/<name> if exists;6. otherwise,

$GIT_DIR/refs/remotes/<name>/HEAD if exists

Specifying revisions (2)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

13 / 33

■ A ref followed by the suffix @ with a date specification:master@{yesterday} or branchx@{2 days ago}

■ A ref followed by the suffix @ with an ordinalspecification: master@{3}

■ ^<n> suffix means the <n>th parent of that commit:master^1 and master^2 – two parents of a merge

◆ master^ is a shorthand for master^1

■ ~<n> suffix means the commit object that is the <n>thgeneration grand-parent, following only the first parent

◆ master^^^ is the same as master~3

■ A suffix : followed by a path: tree-ish object found onpath

◆ master@{1 day ago}:widgets/frames.py

Specifying revision ranges

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

14 / 33

Ranges are used by some commands like git log

■ Just a revision means all commits reachable (ancestors)■ A prefix ^ means to exclude commits reachable

◆ a ^b – all reachable from a but not b◆ alternative b..a

■ r1...r2 is called symmetric difference of r1 and r2

◆ set of commits that are reachable from either one of r1or r2 but not from both

◆ git log master...refactor

■ what is different on branch refactor

■ r1^! includes r1 but excludes all of its parents

Index

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architectureWorking dir,index, repo

Objects (1)

Objects (2)

Refs

Remote refsSpecifyingrevisions (1)

Specifyingrevisions (2)

Specifyingrevision ranges

Index

Local usage

Remote usage

Advanced usage

GUI

15 / 33

. . . . . . file1

dir1file2

file3

file4

file2

dir11:file4

2:file4

Index

■ Index is just another tree object (kind of)

◆ may contain <n>:<file> names

■ files being merged + their common ancestor■ created when conflict during merge

Local usage

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Basic operations(1)

Basic operations(2)

Accessing history

Branching andmerging (1)

Branching andmerging (2)

Remote usage

Advanced usage

GUI

16 / 33

Basic operations (1)

17 / 33

■ init, status – initialize directory, check current branch and changes

$ git initInitialized empty Git repository in /home/olegus/git-test/.git/$ git status# On branch master## Initial commit#nothing to commit (create/copy files and use "git add" to track)

■ change/add files in the directory

$ echo "hello git" > file.txt$ git status...# Untracked files:# (use "git add <file>..." to include in what will be committed)## file.txtnothing added to commit but untracked files present

Basic operations (2)

18 / 33

■ add, rm – stage/unstage files to index

$ git add file.txt...# Changes to be committed:# (use "git rm --cached <file>..." to unstage)## new file: file.txt

■ commit – commit index to repository

$ git commit -m "First commit"[master (root-commit) 4ac4252] First commit1 files changed, 1 insertions(+), 0 deletions(-)create mode 100644 file.txt$ git status# On branch master nothing to commit (working directory clean)

Accessing history

19 / 33

■ log, diff – latest commits, changes

$ git log --pretty=rawcommit 4ac4252fedb778f9f7b60bc543b04e1016e1b952tree c8bcfef1da123a980537a5fa4cf9b7c4f387d451author Oleg Batrashev <[email protected]> 1239193509 +0300committer Oleg Batrashev <[email protected]> 1239193509 +0300

First commit

■ ls-files, ls-tree – explore repository and index

◆ low level commands, almost never used

$ /usr/lib/git-core/git-ls-tree c8bcfe100644 blob 8d0e41234f24b6da002d962a26c2495ea16a425f file.txt$ /usr/lib/git-core/git-ls-files --stage100644 8d0e41234f24b6da002d962a26c2495ea16a425f 0 file.txt

Branching and merging (1)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Basic operations(1)

Basic operations(2)

Accessing history

Branching andmerging (1)

Branching andmerging (2)

Remote usage

Advanced usage

GUI

20 / 33

■ branch – add, list, rename, remove branches (heads)■ checkout – checkout branch

$ git branch feature-X$ git branchfeature-X

* master$ git checkout feature-XSwitched to branch "feature-X"$ git status# On branch feature-Xnothing to commit (working directory clean)

■ commit some changes to the branch

$ echo "new file" > file2.txt$ echo "more info" >> file.txt$ echo "another line" >> file.txt$ git add file.txt file2.txt$ git commit -m "Commit on branch-X"

Branching and merging (2)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Basic operations(1)

Basic operations(2)

Accessing history

Branching andmerging (1)

Branching andmerging (2)

Remote usage

Advanced usage

GUI

21 / 33

■ merge feature-X branch into master branch

$ git merge feature-XUpdating 4ac4252..1835142Fast forwardfile.txt | 2 ++file2.txt | 1 +2 files changed, 3 insertions(+), 0 deletions(-)create mode 100644 file2.txt

■ reset – forget commit(s) and move branch head

$ git log --pretty=oneline HEAD1835142fb02d50f38e94698cff418edf0694d3d0 Commit on branch-X4ac4252fedb778f9f7b60bc543b04e1016e1b952 First commit$ git reset HEAD^file.txt: locally modified$ git log --pretty=oneline4ac4252fedb778f9f7b60bc543b04e1016e1b952 First commit$ git reset master@{1}

■ revert – reapply reversed commit(s) (creates new commit)

Remote usage

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Push to remotePull from remote(1)

Pull from remote(2)

Advanced usage

GUI

22 / 33

Push to remote

23 / 33

■ create shared repository (ssh)

$ ssh dougdevel.orgdougdevel$ cd /srv/git/oleg/uni/; mkdir git-vcs.gitdougdevel$ cd git-vcs.git; git init --bare

Initialized empty Git repository in /srv/git/oleg/uni/git-vcs.gitdougdevel$ echo "My shared repo" > descriptiondougdevel$ exit

■ add remote central and push branch master

$ git remote add central ssh://dougdevel.org/srv/git/oleg/uni/git-vcs.git$ git push central master

Counting objects: 7, done.Compressing objects: 100% (3/3), done.Writing objects: 100% (7/7), 517 bytes, done.Total 7 (delta 0), reused 0 (delta 0)refs/heads/master: 000000 -> 183514To ssh://dougdevel.org/srv/git/oleg/uni/git-vcs.git

* [new branch] master -> master

Pull from remote (1)

24 / 33

■ create another working directory (this time clone from remote repo)

$ git clone ssh://dougdevel.org/srv/git/oleg/uni/git-vcs.git ../gt2Initialized empty Git repository in /home/olegus/gt2/.git/...remote: 100% (7/7) doneremote: Total 7 (delta 0), reused 0 (delta 0)Receiving objects: 100% (7/7), done.

■ make some changes in gt2 and push (origin is a remote after cloning)

$ cd ../gt2/; echo "changes from gt2" >> file2.txtgt2$ git commit -a -m "Changes from gt2"[master 0cbd9bc] Changes from gt21 files changed, 1 insertions(+), 0 deletions(-)gt2$ git push originCounting objects: 5, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 301 bytes, done.Total 3 (delta 0), reused 0 (delta 0)refs/heads/master: 183514 -> 0cbd9bTo ssh://dougdevel.org/srv/git/oleg/uni/git-vcs.git

1835142..0cbd9bc master -> master

Pull from remote (2)

25 / 33

■ pull (fetch + merge)

gt2$ cd ../git-test/$ git pull central masterFrom ssh://dougdevel.org/srv/git/oleg/uni/git-vcs

* branch master -> FETCH_HEADUpdating 1835142..0cbd9bcFast forwardfile2.txt | 1 +1 files changed, 1 insertions(+), 0 deletions(-)

■ we could do fetch and merge separately

◆ allows to inspect remote changes before merge

Advanced usage

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

Cherry-picking

Blame (1)

Blame (2)

GUI

26 / 33

Cherry-picking

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

Cherry-picking

Blame (1)

Blame (2)

GUI

27 / 33

■ cherry-pick

◆ pick and apply separate commits from another branch

■ rebase

◆ move branch root◆ uses cherry-pick to reapply all branch commits

Blame (1)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

Cherry-picking

Blame (1)

Blame (2)

GUI

28 / 33

■ blame (same as annotate)■ move lines “hello git” and “more info” from file.txt to

file2.txt

$ emacs -nw file.txt file2.txt...$ git commit -a -m "Move 2 lines"[master fddff34] Move 2 lines2 files changed, 2 insertions(+), 2 deletions(-)

■ show diff for file1.txt

$ git diff HEAD^ file.txtdiff --git a/file.txt b/file.txtindex 2ae9a28..af28d38 100644--- a/file.txt+++ b/file.txt@@ -1,3 +1 @@-hello git-more infoanother line

Blame (2)

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

Cherry-picking

Blame (1)

Blame (2)

GUI

29 / 33

■ blame:

◆ -s -f – supress author and date, show file names◆ -C – recognise copied lines across files in commits

■ decrease threshold to 10 characters

◆ -M – recognise moved lines within a file in commits

$ git blame -s -f -C10 -M file2.txt1835142f file2.txt 1) new file^4ac4252 file.txt 2) hello git1835142f file.txt 3) more info0cbd9bca file2.txt 4) changes from gt2

■ as we can see

◆ if lines are copied from another file, git finds it

GUI

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

git gui

gitk

git gui blame

30 / 33

git gui

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

git gui

gitk

git gui blame

31 / 33

gitk

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

git gui

gitk

git gui blame

32 / 33

git gui blame

OverviewTutorial way, notmy way today

Thoughts on git

Clarification

Git architecture

Local usage

Remote usage

Advanced usage

GUI

git gui

gitk

git gui blame

33 / 33