SlideShare a Scribd company logo
1 of 120
Git Basic
Learn via Practice
luke.jf.luo@gmail.com
tmux cheat sheet
config file: ~/.tmux.conf
tmux new -s luke
tmux attach -t luke
tmux new -s luke-1 -t luke
key-bind: <ctrl-a> +
help:
? : list all key binds
sessions:
s : list and switch session(return)
d : detach session
layout:
[space]: switch layout
Alt 1-5 : select layout 1-5
windows:
c : new window
k: close window
p/n : previous/next window
0-9: select windows 0/9
w: list and select window
panes:
v: vertical pane
h: horizontal pane
z: zoom pane
x: close pane
up/down/lef/right: select pane
Alt-[up/down/left/right]: resize pane
copy/paste:
[ : enter copy mode. [space] to begin
select, [up/down/left/right] to select text,[return] to end
select
]: paste
logging:
ctrl-p : output current pane to log
config:
r : reload config file
ssh: git bash
[luke@rmbp ~]$ ssh osoci
Last login: Sat Jul 25 18:31:58 2015 from
203.187.164.42
git@osoci:~$ pwd
/home/git
git@osoci:~$ tmux
[luke@rmbp ~]$ cat .ssh/config
Host osoci
HostName 192.168.88.28
Port 22
User git
IdentityFile ~/.ssh/topdemo.pem
ServerAliveInterval 15
ServerAliveCountMax 2
ssh : bitvise client
luke@LUKE-WIN7 ~
$ scp osoci:~/BvSshClient-Inst.exe ./
BvSshClient-Inst.exe
100% 10MB 1.7MB/s 00:06
git init
git@osoci:~$ pwd
/home/git
git@osoci:~$ mkdir luke
git@osoci:~$ cd luke
git@osoci:~/luke$ git init
Initialized empty Git repository in
/home/git/luke/.git/
git@osoci:~/luke (master)$
git@osoci:~/luke (master)$ tree -a
.
└── .git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ …………..
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
10 directories, 13 files
Exercise: create an example model
eaa5 1f48 66a3
b.txt c.txta.txt
43b2
2170
c830 d.txt
d.txt
subdir/
24b1
90c6
54ed
4819
v1.0
v2.0
a.txt
b.txt
c.txt
b2
b1HEAD Legends:
blob
tree
commit
tag
reference
Questions: Porcelain versus
Plumbing
● Porcelain commands are implemented via plumbing commands.
● Please explain how porcelain commands in the left are implemented via
plumbing commands in the right.
1. git add/mv/rm
2. git commit
3. git checkout [-b]
4. git reset [--soft] [--hard]
5. git branch
6. git format-patch/am
7. git diff [--cached] [commit]
8. git status
1. git hash-object
2. git cat-file
3. git update-index
4. git checkout-index
5. git read-tree
6. git write-tree
7. git commit-tree
8. git update-ref
9. git symbolic-ref
10. git ls-tree
11. git mktree
12. git ls-files
13. git diff-files/diff-index/diff-tree
Tools: Three Areas
ls-files -s
update-index
checkout-index
read-tree
write-tree
commit-tree
ls
cd
rm -rf
mkdir
nano a.txt
cat
tree
hash-object
cat-object
show
tag
ls-tree
log
Working Tree Index DB
update-index
checkout-index
write-tree
read-tree
commit-tree
hash-object
add/rm/mv
Commands: create
● git hash-object -t blob -w a.txt
○ write a.txt into DB as a blob
● git update-index --cache-info 100644,hashid,a.txt
○ add a object as an entry in index tree with path name “a.txt”
● git checkout-index -a
○ checkout all files in index into working tree
● git write-tree
○ write index tree into DB and generate a tree object
● git read-tree --prefix pathname tree-hashid
○ read an existing tree in DB and generate an entry in index tree with name “pathname”
● git-commit-tree -m “commit index tree” -p commit-hashid tree-hashid
○ write index tree and generate a commit object that points to this tree with commit message
and parent commit
● git tag -a -m “generate v1.0 tag” v1.0 commit-hashid
○ generate an annotated tag with name “v1.0” which point to a commit
Commands: Retrieve
● git ls-files -s
○ display content of index
● git hash-object -p hashid
○ display the content of object with hashid
● git show hashid
○ display object with hashid
● git log commit-hashid
○ display a commit
● git ls-tree tree-hashid
○ display a tree
● git tag -l
○ list all tags
● git tag -v v1.0
○ display contents of tag “v1.0”
objects: more commands
objects: blob create
[git@osoci luke]$ tree -a .git/objects
.git/objects
├── info
└── pack
[git@osoci luke]$ ls -l
total 12
-rw-r--r-- 1 git git 6 Jul 26 08:15 a.txt
-rw-r--r-- 1 git git 6 Jul 26 08:15 b.txt
-rw-r--r-- 1 git git 6 Jul 26 08:16 c.txt
[git@osoci luke]$ git hash-object -t blob a.txt
eaa5fa8755fc20f08d0b3da347a5d1868404e4
62
[git@osoci luke]$ git hash-object -t blob b.txt
1f482482efa7cc35d1dbab733e23a10c97a936
4f
[git@osoci luke]$ git hash-object -t blob c.txt
66a370d7c2b2035179287c876fbbc59a615c6
d42
objects: blob create
[git@osoci luke]$ tree -a .git/objects
.git/objects
├── 1f
│ └──
482482efa7cc35d1dbab733e23a10c97a9364f
├── 66
│ └──
a370d7c2b2035179287c876fbbc59a615c6d42
├── ea
│ └──
a5fa8755fc20f08d0b3da347a5d1868404e462
├── info
└── pack
5 directories, 3 files
[git@osoci luke]$ git hash-object -t blob -w a.txt
eaa5fa8755fc20f08d0b3da347a5d1868404e462
[git@osoci luke]$ git hash-object -t blob -w b.txt
1f482482efa7cc35d1dbab733e23a10c97a9364f
[git@osoci luke]$ git hash-object -t blob -w c.txt
66a370d7c2b2035179287c876fbbc59a615c6d42
objects: blob structure
[git@osoci luke]$ file .git/objects/66/a370d7c2b2035179287c876fbbc59a615c6d42
.git/objects/66/a370d7c2b2035179287c876fbbc59a615c6d42: zlib compressed data
[git@osoci luke]$ zpipe -d < .git/objects/ea/a5fa8755fc20f08d0b3da347a5d1868404e462 > a.txt.blob
[git@osoci luke]$ hexdump -C a.txt.blob
00000000 62 6c 6f 62 20 36 00 61 2e 74 78 74 0a |blob 6.a.txt.|
0000000d
[git@osoci luke]$ shasum a.txt.blob
eaa5fa8755fc20f08d0b3da347a5d1868404e462 a.txt.blob
[git@osoci luke]$ zpipe < a.txt.blob > a.txt.blob.compressed
[git@osoci luke]$ cmp a.txt.blob.compressed
.git/objects/ea/a5fa8755fc20f08d0b3da347a5d1868404e462
objects: blob retrieve
[git@osoci luke]$ git cat-file -t eaa5fa8755fc20f08d0b3da347a5d1868404e462
blob
[git@osoci luke]$ git cat-file -p eaa5fa8755fc20f08d0b3da347a5d1868404e462
a.txt
[git@osoci luke]$ git show eaa5fa8755fc20f08d0b3da347a5d1868404e462
a.txt
objects: blobs
eaa5 1f48 66a3
b.txt c.txta.txt
objects: indx tree create (1)
[git@osoci luke]$ git update-index --add 
--cacheinfo 100644,eaa5fa8755fc20f08d0b3da347a5d1868404e462,a.txt 
--cacheinfo 100644,1f482482efa7cc35d1dbab733e23a10c97a9364f,b.txt 
--cacheinfo 100644,66a370d7c2b2035179287c876fbbc59a615c6d42,c.txt
[git@osoci luke]$ git ls-files -s
100644 eaa5fa8755fc20f08d0b3da347a5d1868404e462 0a.txt
100644 1f482482efa7cc35d1dbab733e23a10c97a9364f 0b.txt
100644 66a370d7c2b2035179287c876fbbc59a615c6d42 0 c.txt
objects: index tree create(1)
[git@osoci luke]$ file .git/index
.git/index: Git index, version 2, 3 entries
[git@osoci luke]$ tree -a .git/objects
.git/objects
├── 1f
│ └── 482482efa7cc35d1dbab733e23a10c97a9364f
├── 66
│ └── a370d7c2b2035179287c876fbbc59a615c6d42
├── ea
│ └── a5fa8755fc20f08d0b3da347a5d1868404e462
├── info
└── pack
5 directories, 3 files
objects: index tree create(2)
[git@osoci luke]$ rm a.txt b.txt c.txt
[git@osoci luke]$ git ls-files -s
100644 eaa5fa8755fc20f08d0b3da347a5d1868404e462 0a.txt
100644 1f482482efa7cc35d1dbab733e23a10c97a9364f 0b.txt
100644 66a370d7c2b2035179287c876fbbc59a615c6d42 0 c.txt
[git@osoci luke]$ git ls-files -s > index.tree
[git@osoci luke]$ rm .git/index
[git@osoci luke]$ git ls-files -s
[git@osoci luke]$ cat index.tree | git update-index --index-info
[git@osoci luke]$ git ls-files -s
100644 eaa5fa8755fc20f08d0b3da347a5d1868404e462 0a.txt
100644 1f482482efa7cc35d1dbab733e23a10c97a9364f 0b.txt
100644 66a370d7c2b2035179287c876fbbc59a615c6d42 0 c.txt
objects: index tree create(2)
[git@osoci luke]$ ls
a.txt b.txt c.txt index.tree
[git@osoci luke]$ rm *.txt index.tree
[git@osoci luke]$ ls
[git@osoci luke]$ git checkout-index -a
[git@osoci luke]$ ls
a.txt b.txt c.txt
objects: tree create
[git@osoci luke]$ git cat-file -t 
43b2935d64c7fdb71dd299337ef0a9a6d95e48bf
tree
[git@osoci luke]$ git cat-file -p 
43b2935d64c7fdb71dd299337ef0a9a6d95e48bf
100644 blob eaa5fa8755fc20f08d0b3da347a5d1868404e462 a.txt
100644 blob 1f482482efa7cc35d1dbab733e23a10c97a9364f b.txt
100644 blob 66a370d7c2b2035179287c876fbbc59a615c6d42 c.txt
[git@osoci luke]$ git show 
43b2935d64c7fdb71dd299337ef0a9a6d95e48bf
tree 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf
a.txt
b.txt
c.txt
[git@osoci luke]$ git write-tree
43b2935d64c7fdb71dd299337ef0a9a6d95e48bf
[git@osoci luke]$ find .git/objects/ -type f
.git/objects/66/a370d7c2b2035179287c876fbbc59a615c6d42
.git/objects/43/b2935d64c7fdb71dd299337ef0a9a6d95e48bf
.git/objects/1f/482482efa7cc35d1dbab733e23a10c97a9364f
.git/objects/ea/a5fa8755fc20f08d0b3da347a5d1868404e462
[git@osoci luke]$ git ls-tree 
43b2935d64c7fdb71dd299337ef0a9a6d95e48bf
100644 blob eaa5fa8755fc20f08d0b3da347a5d1868404e462
a.txt
100644 blob 1f482482efa7cc35d1dbab733e23a10c97a9364f
b.txt
100644 blob 66a370d7c2b2035179287c876fbbc59a615c6d42
c.txt
objects: blob+tree
eaa5 1f48 66a3
b.txt c.txta.txt
43b2
objects: tree with subdir
[git@osoci luke]$ echo "d.txt" > d.txt
[git@osoci luke]$ git hash-object -w d.txt
c830ea98d189c62bb530f061e2467e52da775b33
[git@osoci luke]$ git read-tree --empty
[git@osoci luke]$ git update-index --add --cacheinfo 
100400,c830ea98d189c62bb530f061e2467e52da775b33,d.txt
[git@osoci luke]$ git ls-files -s
100644 c830ea98d189c62bb530f061e2467e52da775b33 0
d.txt
objects: tree with sub dir
objects: blob+tree
eaa5 1f48 66a3
b.txt c.txta.txt
43b2
2170
c830 d.txt
d.txt
subdir/
objects: commit
commit-tree
author name/email
committer name/email
index
tree
date
commit message
parent commits
commit
objects: commit config
[git@osoci luke]$ git config --global user.name "Luke Luo"
[git@osoci luke]$ git config --global user.email "luke.jf.luo@gmail.com"
[git@osoci luke]$ git config --list
user.name=Luke Luo
user.email=luke.jf.luo@gmail.com
ui.color=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
objects: commit
[git@osoci luke]$ git commit-tree -m "parent commit" 43b2
24b1c6cb1d93933fd3eaf9af28e4bad180d19265
[git@osoci luke]$ git commit-tree -m "child commit" -p 24b12170
90c6523c6f71522b0f884f250af2aae2e941ee43
[git@osoci luke]$ git cat-file -p 24b1
tree 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf
author Luke Luo <luke.jf.luo@gmail.com> 1437897133 +0800
committer Luke Luo <luke.jf.luo@gmail.com> 1437897133 +0800
parent commit
[git@osoci luke]$ git cat-file -p 90c6
tree 21702cd074d61a5b5581142e66a8db8154bb161f
parent 24b1c6cb1d93933fd3eaf9af28e4bad180d19265
author Luke Luo <luke.jf.luo@gmail.com> 1437897181 +0800
committer Luke Luo <luke.jf.luo@gmail.com> 1437897181 +0800
child commit
objects: commit
[git@osoci luke]$ cd .git/refs/heads/
[git@osoci heads]$ echo 90c6523c6f71522b0f884f250af2aae2e941ee43 > master
[git@osoci heads]$ git log
commit 90c6523c6f71522b0f884f250af2aae2e941ee43
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Sun Jul 26 15:53:01 2015 +0800
child commit
commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Sun Jul 26 15:52:13 2015 +0800
parent commit
[git@osoci heads]$ git branch
* master
objects: blob+tree+commit
eaa5 1f48 66a3
b.txt c.txta.txt
43b2
2170
c830 d.txt
d.txt
subdir/
24b1
90c6
objects: tag
[git@osoci luke]$ git tag -a -m "v1.0 tag" v1.0 24b1
[git@osoci luke]$ git tag -a -m "v2.0 tag" v2.0 90c6
[git@osoci luke]$ ls .git/refs/tags/
v1.0 v2.0
[git@osoci luke]$ cat .git/refs/tags/v1.0
54ed67752ab1c0f0ca3ede2cfc77f06b014a0665
[git@osoci luke]$ cat .git/refs/tags/v2.0
481973c0fce98ba63b2aeaa3ab95f1694964e4c1
[git@osoci luke]$ git cat-file -p 481973
object 90c6523c6f71522b0f884f250af2aae2e941ee43
type commit
tag v2.0
tagger Luke Luo <luke.jf.luo@gmail.com> 1437903166 +0800
v2.0 tag
[git@osoci luke]$ git cat-file -p v1.0
object 24b1c6cb1d93933fd3eaf9af28e4bad180d19265
type commit
tag v1.0
tagger Luke Luo <luke.jf.luo@gmail.com> 1437903152 +0800
v1.0 tag
[git@osoci luke]$ git tag
v1.0
v2.0
objects: tag
[git@osoci luke]$ git show v1.0
tag v1.0
Tagger: Luke Luo <luke.jf.luo@gmail.com>
Date: Sun Jul 26 17:32:32 2015 +0800
v1.0 tag
commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Sun Jul 26 15:52:13 2015 +0800
parent commit
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..eaa5fa8
--- /dev/null
…………………..
[git@osoci luke]$ git log v1.0
commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Sun Jul 26 15:52:13 2015 +0800
parent commit
[git@osoci luke]$ git log v2.0
commit 90c6523c6f71522b0f884f250af2aae2e941ee43
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Sun Jul 26 15:53:01 2015 +0800
child commit
commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Sun Jul 26 15:52:13 2015 +0800
parent commit
Objects: blob+tree+commit+tag
eaa5 1f48 66a3
b.txt c.txta.txt
43b2
2170
c830 d.txt
d.txt
subdir/
24b1
90c6
54ed
4819
v1.0
v2.0
a.txt
b.txt
c.txt
objects: Repo housekeeping
● git prune
○ remove all objects not referenced via
branch/tag/reflog/stash
● git pack-objects basename < object-list
○ pack objects into a single pack file/index file
● git repack -a
○ pack all objects under .git/objects/pack/
● git gc --aggressive --prune=now
○ garbage collection and many housekeeping
index: operations
● display
○ git ls-files -s
● clear
○ git read-tree --empty
● add file entry in working tree
○ git update-index --add a.txt b.txt
● add file entry from repo
○ git update-index --add --cacheinfo mode,hashid,path
○ git update-index --indexinfo < index.entry
● add whole tree from repo
○ git read-tree --prefix=<xxx> tree-ish
● checkout index into working tree
○ git checkout-index -a
○ git checkout-index -- filename
● write index tree into repo
○ git write-tree
refs:
[git@osoci luke]$ cat ref.create
create refs/heads/b1 90c6523c6f71522b0f884f250af2aae2e941ee43
create refs/heads/b2 24b1c6cb1d93933fd3eaf9af28e4bad180d19265
[git@osoci luke]$ cat ref.create | git update-ref --stdin
[git@osoci luke]$ ls .git/refs/heads/
b1 b2 master
[git@osoci luke]$ cat .git/refs/heads/{b1,b2}
90c6523c6f71522b0f884f250af2aae2e941ee43
24b1c6cb1d93933fd3eaf9af28e4bad180d19265
[git@osoci luke]$ git branch
b1
b2
* master
refs: symbolic
[git@osoci luke]$ cat .git/HEAD
ref: refs/heads/master
[git@osoci luke]$ git branch
b1
b2
* master
[git@osoci luke]$ git symbolic-ref HEAD refs/heads/b1
[git@osoci luke]$ git branch
* b1
b2
master
[git@osoci luke]$ cat .git/HEAD
ref: refs/heads/b1
refs: special references
● HEAD - The currently checked-out commit/branch.
○ HEAD can contain either a symbolic ref or a commit hash.
● FETCH_HEAD - The most recently fetched branch from a remote repo.
● ORIG_HEAD - A backup reference to HEAD before drastic changes to it.
● MERGE_HEAD - The commit(s) that you’re merging into the current branch with git merge.
● CHERRY_PICK_HEAD - The commit that you are cherry-picking.
reflog: ref value history
Git will record all branch/tag/HEAD changes in .git/logs
[luke@rmbp x]$ tree .git/logs
.git/logs
├── HEAD
└── refs
└── heads
├── master
└── remote
└── y
└── master
4 directories, 3 files
[luke@rmbp x]$ cat .git/logs/refs/heads/master
0000000000000000000000000000000000000000 e6c4b7bcecde7ef05a3bc512b2445d841a9b2c56 Luke Luo <luke.jf.luo@gmail.com>
1438149983 +0800 commit (initial): x0
e6c4b7bcecde7ef05a3bc512b2445d841a9b2c56 3a460bd4c2b298230253b278b59689e2ec46d2b4 Luke Luo <luke.jf.luo@gmail.com>
1438149983 +0800 commit: x1
3a460bd4c2b298230253b278b59689e2ec46d2b4 9579bc8a3c878ff32bd202f873e7c475e89b6b71 Luke Luo
reflog: get back previous ref values
[luke@rmbp x]$ git reflog show
fd31956 HEAD@{0}: push
1972550 HEAD@{1}: push
9579bc8 HEAD@{2}: commit: x2
3a460bd HEAD@{3}: commit: x1
e6c4b7b HEAD@{4}: commit (initial): x0
Objects+refs : recap
eaa5 1f48 66a3
b.txt c.txta.txt
43b2
2170
c830 d.txt
d.txt
subdir/
24b1
90c6
54ed
4819
v1.0
v2.0
a.txt
b.txt
c.txt
b2
b1
HEAD
WIP commit
case study: checkout -- filepath
- checkout -- filepath
repo
index
work tree
checkout master -- filepath
checkout -- filepath
HEAD
master
case study: checkout [branch]
- checkout [branch]
repo
index
work tree
checkout master
HEAD
orig
branch
master
case study: checkout [commit]
- checkout [commit] : detach checkout
repo
index
work tree
checkout a3f678
HEAD
master a3f678
case study: checkout [commit]
- checkout [commit]
- checkout --detach [branch]
repo
index
work tree
checkout a3f678
HEAD
master a3f678
case study: checkout -b
- checkout -b [new-branch] [old-branch/commit]
HEAD
old-branch
new-branch
parent commit
case study: checkout --orphan
- checkout --orphan new-branch
HEAD
old-branch
new-branch
parent commit
case study: reset
- git reset a23e45 HEAD
master
a23e45…...
case study: stash
How stash is implemented via “reflog” and object model?
[luke@rmbp stash]$ git simple-commit a b c
[luke@rmbp stash]$ ls
a.txt b.txt c.txt
[luke@rmbp stash]$ echo "change a.txt" > a.txt
[luke@rmbp stash]$ echo "change b.txt" > b.txt
[luke@rmbp stash]$ echo "untracked file " > d.txt
[luke@rmbp stash]$ git add a.txt
[luke@rmbp stash]$ git stash save
Saved working directory and index state WIP on master: 5b32284 c
HEAD is now at 5b32284 c
[luke@rmbp stash]$ git stash list
stash@{0}: WIP on master: 5b32284 c
case study: stash
stash
WIP
commit
master
untracked
change
staged
index
tool command : git simple-commit
[luke@rmbp x]$ git init
Initialized empty Git repository in /tmp/x/.git/
[luke@rmbp x]$ git simple-commit a b c
[master (root-commit) 167c0ef] a
1 file changed, 1 insertion(+)
create mode 100644 a.txt
[master 88b4872] b
1 file changed, 1 insertion(+)
create mode 100644 b.txt
[master 2322a7f] c
1 file changed, 1 insertion(+)
create mode 100644 c.txt
[luke@rmbp x]$ ls
a.txt b.txt c.txt
[luke@rmbp x]$ cat a.txt
a
[luke@rmbp x]$ cat b.txt
b
[luke@rmbp x]$ cat c.txt
c
[luke@rmbp x]$ git log
commit 2322a7f97b833bd9a0411e9514d2cc7cbb549c17
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Fri Jul 31 06:43:07 2015 +0800
c
commit 88b4872eab7060fee9b617fdacc3c4f92ed33abc
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Fri Jul 31 06:43:07 2015 +0800
b
commit 167c0eff7f1076ceadcf27069713f7beccb3953d
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Fri Jul 31 06:43:07 2015 +0800
a
diff: algorithm
longest common subsequence problem:
https://en.wikipedia.org/wiki/Diff_utility#Algorithm
example:
a b c d f g h j q z
a b c d e f g i j k r x y z
a b c d f g j z
e h i q k r x y
+ - + - + + + +
diff: unified format
[git@osoci luke]$ diff -u orig.txt new.txt
--- orig.txt 2015-07-27 06:39:52.675733655 +0800
+++ new.txt 2015-07-27 06:40:43.242127477 +0800
@@ -1,3 +1,9 @@
+This is an important
+notice! It should
+therefore be located at
+the beginning of this
+document!
+
This part of the
document has stayed the
same from version to
@@ -5,16 +11,10 @@
be shown if it doesn't
change. Otherwise, that
would not be helping to
-compress the size of the
-changes.
-
-This paragraph contains
-text that is outdated.
-It will be deleted in the
-near future.
+compress anything.
git diff: enhanced unified format
[git@osoci luke]$ git diff
diff --git a/orig.txt b/orig.txt
index c49483c..3dc52fe 100644
--- a/orig.txt
+++ b/orig.txt
@@ -1,3 +1,9 @@
+This is an important
+notice! It should
+therefore be located at
+the beginning of this
+document!
+
This part of the
document has stayed the
same from version to
@@ -5,16 +11,10 @@ version. It shouldn't
be shown if it doesn't
change. Otherwise, that
would not be helping to
-compress the size of the
-changes.
-
-This paragraph contains
-text that is outdated.
-It will be deleted in the
-near future.
+compress anything.
It is important to spell
-check this dokument. On
+check this document. On
the other hand, a
misspelled word isn't
the end of the world.
git diff: trees
● git diff: working tree ----> index
● git diff --cached : index ---> repo commit
● git diff HEAD : working tree → repo commit
● git diff commit1 commit2 : commit tree 1 in repo ---->
commit tree 2 in repo
git diff: raw output format
git diff --raw
· A: addition of a file
· C: copy of a file into a new one
· D: deletion of a file
· M: modification of the contents or mode of a file
· R: renaming of a file
· T: change in the type of the file
· U: file is unmerged (you must complete the merge before
it can be committed)
· X: "unknown" change type (most probably a bug, please
report it)
git difftool
[luke@rmbp ~]$ git difftool --tool-help
'git difftool --tool=<tool>' may be set to one of the following:
gvimdiff
gvimdiff2
gvimdiff3
kdiff3
kompare
meld
vimdiff
vimdiff2
vimdiff3
The following tools are valid, but not currently available:
araxis
bc
bc3
codecompare
deltawalker
diffmerge
diffuse
ecmerge
emerge
opendiff
p4merge
tkdiff
xxdiff
git difftool
[git@osoci linux]$ git difftool -t vimdiff v4.1-rc8 v4.1
patch/git apply
reference:
http://www.thegeekstuff.com/2014/12/patch-command-examples/
[git@osoci ~]$ git clone http://192.168.88.12:8080/frank/git-tutorial.git
[git@osoci ~]$ cd git-tutorial
[git@osoci git-tutorial]$ ls
git_advanced.md Git分支模型最佳实践.pdf huhongyan.txt
README.md TortoiseGit zhanglingfeng.txt zhangxinxu.txt
git_commands.md huanglianbin.txt markdown_syntax.md
test.txt yhbtest.txt zhangtao.txt 幻灯片
[git@osoci git-tutorial]$ git rm *.txt
[git@osoci git-tutorial]$ git commit -m "delete txt files"
[git@osoci git-tutorial]$ git diff HEAD HEAD^ > my.diff
[git@osoci git-tutorial]$ git apply --stat my.diff
[git@osoci git-tutorial]$ git apply --check my.diff
[git@osoci git-tutorial]$ git apply my.diff
[git@osoci git-tutorial]$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
huanglianbin.txt
huhongyan.txt
my.diff
test.txt
yhbtest.txt
zhanglingfeng.txt
zhangtao.txt
zhangxinxu.txt
nothing added to commit but untracked files present (use "git add"
to track)
git apply -R
[git@osoci git-tutorial]$ git apply -R my.diff
[git@osoci git-tutorial]$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be
committed)
my.diff
nothing added to commit but untracked files present (use
"git add" to track)
git format-patch/am
[[git@osoci luke]$ git init
Initialized empty Git repository in /home/git/luke/.git/
[git@osoci luke]$ rm -rf *.txt
[git@osoci luke]$ git simple-commit m0 m1 m2 m3
[master (root-commit) d513ee0] m0
[git@osoci luke]$ git log
commit 29ec6becef10fceb126a52ac6bc731f674f19c3b
commit 35e2be099f848dffc096bfd4a53b544fac868272
commit f9f91093a6427ec51caa1e86003b152fcb1ba251
commit d513ee08d967114205b4f9892da73eb810125494
[git@osoci luke]$ git checkout -b test d513ee
Switched to a new branch 'test'
[git@osoci luke]$ git format-patch HEAD..master
0001-m1.patch
0002-m2.patch
0003-m3.patch
[git@osoci luke]$ git am --signoff *.patch
Applying: m1
Applying: m2
Applying: m3
[git@osoci luke]$ git log
commit 5e6de70e06d62ae4ea1894e5beabfd0f1cf3a93f
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Mon Jul 27 12:55:42 2015 +0800
m3
Signed-off-by: Luke Luo <luke.jf.luo@gmail.com>
commit ee61f13466eef391fcf238839bebf8fcc500b41b
Author: Luke Luo <luke.jf.luo@gmail.com>
Date: Mon Jul 27 12:55:42 2015 +0800
m2
Signed-off-by: Luke Luo <luke.jf.luo@gmail.com>
git diff: patch-id
● every commit has a diff with it (comparing to its parent)
● hash the diff to get a hashid
● identical patchid => commits introduce same change
[git@osoci rebase]$ git branch
master
* test
[git@osoci rebase]$ git simple-commit patchid
[git@osoci rebase]$ git show test | git patch-id
32bd856835dce7f3ebacf9c747c3bc01229fcf6c
5b8b610da4a3680234ad612639b8b875563425eb
[git@osoci rebase]$ git checkout master
Switched to branch 'master'
[git@osoci rebase]$ git simple-commit patchid
[git@osoci rebase]$ git show master | git patch-id
32bd856835dce7f3ebacf9c747c3bc01229fcf6c
86ba15eb53d069a9e3a33ca9469c20f45925c6ac
git cherry
What change have been applied to upstream?
c0
c1 c2 t1’ c4
master
t0 t1
test
[git@osoci patchid]$ git init
[git@osoci patchid]$ git simple-commit c0 c1 c2
[[git@osoci patchid]$ git checkout -b test HEAD~2
[git@osoci patchid]$ git simple-commit t0 t1
[git@osoci patchid]$ git format-patch master
0001-t0.patch
0002-t1.patch
[git@osoci patchid]$ git checkout master
[git@osoci patchid]$ git am 0002-t1.patch
Applying: t1
[git@osoci patchid]$ git checkout test
[git@osoci patchid]$ git log --oneline
1b99116 t1
958eab6 t0
a08f762 c0
[git@osoci patchid]$ git cherry master
+ 958eab6fc6ce904875949e10521e9879565e4eb5
- 1b9911603afc320f4d3aec2e8caef871d8aad511
merge: 3 way
ref: http://www.drdobbs.com/tools/three-way-merging-a-look-under-the-hood/240164902
merge: base
ref: http://codicesoftware.blogspot.com/2011/09/merge-recursive-strategy.html
git merge-base 15 16
result: 10
base:10
src: 16
dst: 15
merge: recursive base
ref: http://codicesoftware.blogspot.com/2011/09/merge-recursive-strategy.html
git merge-base src dst
result: ????
base: ancestor2
src: 16
dst: 15
ancestor2 =
merge(ancestor0,
ancestor1)
merge: octopus
merge: strategies
man git-merge:
git merge -s [strategies] -X [strategy options]
Strategies:
● resolve
● recursive(default)
○ ours
○ theirs
○ patience
○ diff-algorithm=[patience|minimal|histogram|myers]
○ ignore-space-change, ignore-all-space, ignore-space-at-eol
○ renormalize,no-renormalize
○ subtree[=<path>]
● octopus (heads>=3)
● ours
● subtree
merge: typical scenarios
c0 c1 c2 c3 c4
HEAD
c5
git merge t1
c1
c4
t1
c5base
dst
src
merged
t0 t1
merge: typical scenarios
c0 c1 c2 c3 c4
HEAD
c5
git revert c4
c4
c4
c3
c5base
dst
src
merged
merge: typical scenarios
c0 c1 c2 c3 c4
HEAD
c5
git revert c2
c2
c4
c1
c5base
dst
src
merged
merge: typical scenarios
c0 c1 c2 c3 c4
HEAD
c5
git cherry-pick t0
c1
c4
t0
c5base
dst
src
merged
t0 t1
merge: typical scenarios
c0 c1 c2 c3 c4
HEAD
c5
git cherry-pick t1
t0
c4
t1
c5base
dst
src
merged
t0 t1
merge: typical scenarios
c0 c1 c2 c3 c4
HEAD
git rebase c4 t1
c1
c4
t0
t0’base
dst
src
merged
t0 t1
t0’ t1’
t0
t0’
t1
t1’base
dst
merged
HEAD
merge: conflicts
[git@osoci ~]$ rm -rf luke
[git@osoci ~]$ mkdir luke
[git@osoci ~]$ cd luke
[git@osoci luke]$ git init
Initialized empty Git repository in /home/git/luke/.git/
[git@osoci luke]$ git simple-commit m0 m1 m2
[git@osoci luke]$ git checkout -b test
HEAD~1
Switched to a new branch 'test'
[git@osoci luke]$ ls
m0.txt m1.txt
[git@osoci luke]$ cat > m0.txt <<EOF
> test m0
> test m0 add a line
> EOF
[git@osoci luke]$ cat > t0.txt <<EOF
> t0 new file
> EOF
[git@osoci luke]$ git rm m1.txt
rm 'm1.txt'
[git@osoci luke]$ git add t0.txt m0.txt
merge: conflicts
[git@osoci luke]$ git checkout master
cat > m0.txt <<EOF
>m0
>m0 add a line
EOF
[git@osoci luke]$ git merge --no-commit test
Removing m1.txt
Auto-merging m0.txt
CONFLICT (content): Merge conflict in m0.txt
Automatic merge failed; fix conflicts and then
commit the result.
[git@osoci luke]$ git ls-files -s
100644 277540bebeee7be0642d87437590629e738fc184 1 m0.txt
100644 41b3b6f418ae91add3ff50569eac9afb780322c8 2 m0.txt
100644 21b10565974d4dbe96856e064fc56df997294190 3 m0.txt
100644 08bb2331e777f431177c40df6841c0034f89fb58 0 m2.txt
100644 36fb22f687676ab60973a770b274d62da04cb3ba 0 t0.txt
[git@osoci luke]$ cat m0.txt
<<<<<<< HEAD
m0
m0 add a line
=======
test m0
test m0 add a line
>>>>>>> test
merge: conflicts
[git@osoci luke]$ git ls-files -s
100644 277540bebeee7be0642d87437590629e738fc184 1 m0.txt
100644 41b3b6f418ae91add3ff50569eac9afb780322c8 2 m0.txt
100644 21b10565974d4dbe96856e064fc56df997294190 3 m0.txt
100644 08bb2331e777f431177c40df6841c0034f89fb58 0 m2.txt
100644 36fb22f687676ab60973a770b274d62da04cb3ba 0 t0.txt
[git@osoci luke]$ git checkout --ours -- m0.txt
[git@osoci luke]$ git checkout --theirs -- m0.txt
[git@osoci luke]$ git checkout-index --stage=2 -- m0.txt
[git@osoci luke]$ git checkout-index --stage=3 -- m0.txt
merge: conflicts
[git@osoci luke]$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Changes to be committed:
deleted: m1.txt
new file: t0.txt
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: m0.txt
[git@osoci luke]$ git diff
diff --cc m0.txt
index 41b3b6f,21b1056..0000000
--- a/m0.txt
+++ b/m0.txt
@@@ -1,2 -1,2 +1,7 @@@
++<<<<<<< HEAD
+m0
+m0 add a line
++=======
+ test m0
+ test m0 add a line
++>>>>>>> test
merge: conflicts
[git@osoci luke]$ echo "merged" > m0.txt
[git@osoci luke]$ git add m0.txt
[git@osoci luke]$ git ls-files -s
100644 20b117fdd3804508359ec883abe519486f0d19dd 0 m0.txt
100644 08bb2331e777f431177c40df6841c0034f89fb58 0 m2.txt
100644 36fb22f687676ab60973a770b274d62da04cb3ba 0 t0.txt
[git@osoci luke]$ git commit -m "m4"
[master 5802979] m4
merge: review merge commit
Diff Combined Format:
review merge and conflict
resolution
merge: misc tools
● git merge-base c1 c2
○ what is the best base commit to merge commit c1 and c2?
● git rev-list c1
○ list all the commits which could be reached from commit/branch c1
reverse chronological order
● git branch --merged
○ what branches’ changed have been merged into current branch so they
could be deleted safely?
● man gitrevisions
○ how to specify a range of commits?
● git merge [--squash] [--no-commit] [--no-ff] [--abort]
○ control the form of merged commit
merge: how it works
index
working tree
MERGE_HEAD
ORIG_HEAD
MERGE_MSG
SQUASH_MSG
HEAD
New
HEAD
c0
c1
c2
c3
base
dst
src
merged
checkout-index -a
(with potential
conflicts)
new
commit
old
commit
rebase:clean local history
c0
c1 c2 c3 c4
master
t0 t1 t2 t3
test
t0’ t1’ test
[git@osoci rebase]$ git init
[git@osoci rebase]$ git simple-commit c0 c1 c2 c3 c4
[git@osoci rebase]$ git checkout -b test HEAD~3
[git@osoci rebase]$ git simple-commit t0 t1 t2 t3
[git@osoci rebase]$ git rebase -i c1
pick 688cf6d c1
squash 1320ec6 t0
squash cc7cfe6 t1
squash 43f6b13 t2
pick ddc6988 t3
……..
Stopped at ddc69887c16ce9a904f453b3ff793338b44e6092... t3
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
[git@osoci rebase]$ git rebase --continue
Successfully rebased and updated refs/heads/test.
[git@osoci rebase]$ git log --oneline
32eb2e5 t3
7cac48a t0
688cf6d c1
b9a4e13 c0
rebase:sync with upstream
c0
c1 c2 c3 c4
master
t0’ t1’
[git@osoci rebase]$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: t0
Applying: t3
[git@osoci rebase]$ git log --oneline
305209e t3
f3e3986 t0
32ffe2d c4
7e363b9 c3
f16644f c2
688cf6d c1
b9a4e13 c0
t0”
t1”test
merge --squash : sync with
upstream without merge commit
c0
c1 c2 c3 c4
master
t0 t1
test
[git@osoci rebase]$ git init
Initialized empty Git repository in /tmp/rebase/.git/
[git@osoci rebase]$ git simple-commit c0 c1 c2 c3 c4
[git@osoci rebase]$ git checkout -b test HEAD~3
[git@osoci rebase]$ git simple-commit t0 t1
[git@osoci rebase]$ git merge --squash master
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as
requested
[git@osoci rebase]$ git log --oneline
1a87223 t1
5f7fe10 t0
7770250 c1
3b01ffa c0
[git@osoci rebase]$ ls
c0.txt c1.txt c2.txt c3.txt c4.txt t0.txt t1.txt
merge --squash
master
summary: merge cases
● for every merge, find out the correct “base/src/dst”;
● many advanced git commands are just special cases of git merge, where
“base/src/dst” are selected in specified way
1. git revert
2. git cherry-pick
3. git rebase
search: branch tag
● reference: http://lukeluo.blogspot.com/2014/06/git-as-i-understand-6-
search.html
● https://github.com/torvalds/linux.git
● https://github.com/git/git.git
[git@osoci git]$ git branch -avv
maint bb3e7b1 Git 2.4.6
* master 6f9504c RelNotes: am.threeWay does not exist
(yet)
next 8c70279 Sync with master
pu 20f0f97 Merge branch 'mh/tempfile' into pu
todo 163e88a What's cooking (2015/07 #06)
[git@osoci git]$ git branch --list -vv m*
maint bb3e7b1 Git 2.4.6
* master 6f9504c RelNotes: am.threeWay does not exist
(yet)
[git@osoci git]$ git show master
[git@osoci git]$ git tag -l
gitgui-0.10.0
…..
v2.5.0-rc2
v2.5.0-rc3
[git@osoci git]$ git tag -l v2.5*
v2.5.0-rc0
v2.5.0-rc1
v2.5.0-rc2
v2.5.0-rc3
[git@osoci git]$ git show v2.4.6
tag v2.4.6
Tagger: Junio C Hamano <gitster@pobox.com>
……….
search: branch/tag with commit
[git@osoci git]$ git branch -l -vv --contains v2.4.5
maint bb3e7b1 Git 2.4.6
* master 6f9504c RelNotes: am.threeWay does not exist (yet)
next 8c70279 Sync with master
pu 20f0f97 Merge branch 'mh/tempfile' into pu
[git@osoci git]$ git tag -l --contains 0df0541bf1
v2.4.5
v2.4.6
v2.5.0-rc0
v2.5.0-rc1
v2.5.0-rc2
v2.5.0-rc3
search: git browser “tig”
search: grep file content
[git@osoci git]$ git grep -i rerere v2.4.6 -- Documentation/config.txt
v2.4.6:Documentation/config.txt:gc.rerereResolved::
v2.4.6:Documentation/config.txt: kept for this many days when 'git rerere gc' is run.
v2.4.6:Documentation/config.txt: The default is 60 days. See linkgit:git-rerere[1].
v2.4.6:Documentation/config.txt:gc.rerereUnresolved::
v2.4.6:Documentation/config.txt: kept for this many days when 'git rerere gc' is run.
v2.4.6:Documentation/config.txt: The default is 15 days. See linkgit:git-rerere[1].
v2.4.6:Documentation/config.txt:rerere.autoUpdate::
v2.4.6:Documentation/config.txt: When set to true, `git-rerere` updates the index with the
…..
search: commits
git rev-list: DAG traversal and commits set operation
c0
c1 c2 c3 c4
master
t0 t1
test
rev-list c4 = {c0,c1,c2,c3,c4} -- branch master
rev-list t1 = {t1,t0,c1,c0} -- branch test
rev-list c2 t0 = {t0,c0,c1,c2}
rev-list t1 ^c4 = { t0,t1}
rev-list c4..t1 = rev-list t1 ^c4
rev-list t1...c3 = {c1,c2,c3,t0,t1}
rev-list ${git merge-base --all c4 t1} = {c0,c1}
search: commit meta-info
git log --author=*** --committer=*** --since=2013-04-05 --until=2014-05-07
got log --no-merges git log --merges
git log -i --grep “mac book”
(search commits which contains “mac book” in commit message)
[git@osoci linux]$ git log -i --grep "mac book"
commit bcd2623073e98f69f84720308db0b142c4da0bd6
Author: Tom Gundersen <teg@jklm.no>
Date: Thu Oct 31 00:38:30 2013 -0700
Input: allow deselecting serio drivers even without CONFIG_EXPERT
There is plenty of consumer hardware (e.g., mac books) that does not use AT
keyboards or PS/2 mice. It therefore makes sense for distro kernels to
build the related drivers as modules to avoid loading them on hardware that
does not need them. As such, these options should no longer be protected by
EXPERT.
Moreover, building these drivers as modules gets rid of the following ugly
error during boot:
search: commit diff
git log -- bug.txt:
which commits change “bug.txt” ?
search: commit diff (pickaxe)
git log -S “bug*” --pickaxe-regex :
which commits change the time of appearance of
“bug”, added or deleted
git log -G “bug*” :
which commits add/delete lines contains “bugs*”
Under the Hood:
git diff -S<string>
git diff -S<regex> --pickaxe-regex
git diff -G<regex>
search: commit diff blame
[luke@rmbp blame]$ cat blame.txt
a
b
c
d
[luke@rmbp blame]$ git blame blame.txt
^0a17a1e (Luke Luo 2014-06-16 22:44:45 +0800 1) a
4991d567 (Luke Luo 2014-06-16 22:44:57 +0800 2) b
bf0599d2 (Luke Luo 2014-06-16 22:45:08 +0800 3) c
d76ddb98 (Luke Luo 2014-06-16 22:45:24 +0800 4) d
git blame blame.txt:
for every line in blame.txt, find out the
latest commit which add this line. It tells
you why this line appears.
git blame --reverse master~7..master -- blame.txt
for every line in blame.txt, given a start commit
(master~7), looking forward find out the first commit
which delete this line. It tells you why this line
disappears.
[luke@rmbp blame]$ git blame --reverse master~7..master
-- blame.txt
^805836b (Luke Luo 2014-06-17 08:56:31 +0800 1) a
314a3c6e (Luke Luo 2014-06-17 08:56:47 +0800 2) b
506a3641 (Luke Luo 2014-06-17 08:56:55 +0800 3) c
bafa1476 (Luke Luo 2014-06-17 08:57:06 +0800 4) d
a95b8daa (Luke Luo 2014-06-17 08:57:19 +0800 5) e
search: blame via git log
git log -L <start>,<end>:<file>, -L :<funcname>:<file>
this is another form of “git blame”
Collaboration: namespace
Reference: local namespace Object: global namespace (hashid)
e34f 20a3
a5f9
fe2a 119a
3748
[git@osoci git]$ git branch -a
maint
master
* pu
remotes/origin/HEAD -> origin/master
remotes/origin/maint
remotes/origin/master
remotes/origin/next
remotes/origin/pu
remotes/origin/todo
[git@osoci git]$ git branch -a
maint
master
* pu
remotes/origin/HEAD -> origin/master
remotes/origin/maint
remotes/origin/master
remotes/origin/next
remotes/origin/pu
remotes/origin/todo
global unique
hashid
Collaboration: transfer protocol
● ssh://[user@]host.xz[:port]/path/to/repo.git/
● git://host.xz[:port]/path/to/repo.git/
● http[s]://host.xz[:port]/path/to/repo.git/
● ftp[s]://host.xz[:port]/path/to/repo.git/
● rsync://host.xz/path/to/repo.git/
Collaboration: connect examples
● Please connect to git repos in machine 192.168.88.35 and machine
192.168.88.12 via ssh and pub/private keys, using your git bash in
windows
● Please connect to above two machines via Sourcetree in windows
● Please register a git account in “http://git.oschina.net/” and connect
to it
Collaboration: refspec
● exchange of information among git repos happen in the unit of BRANCH
● refspec: +<source brances>:<dest brances>
git remote add origin https://git@github.com:mary/example-repo.git
git ls-remote origin
git remote show
[remote "origin"]
url = https://git@github.com:mary/example-repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "origin"]
url = https://git@github.com:mary/example-repo.git
fetch = +refs/heads/master:refs/remotes/origin/master
[remote "origin"]
url = https://git@github.com:mary/example-repo.git
fetch = +refs/heads/master:refs/remotes/origin/master
push = refs/heads/master:refs/heads/qa-master
Collaboration: sync examples
● Specify the remote branch:
$ git push origin master:refs/heads/qa-master
● Remove a remote branch:
$ git push origin :some-feature
$ git push origin --delete some-feature
● fetch from remote branch
$ git fetch origin master:refs/integration/qa
Collaboration: tracking branch
● create local tracking branch:
git branch -u origin/master master
git checkout -b master origin/master
● cat .git/config
[branch "master"]
remote = origin
merge = refs/heads/master
Collaboration: tracking branch
local tracking branch:
------master-------
remote tracking branch:
------origin/master-------
Local Repo
------master-------
Remote Repo
fetch
track merge
push
pull
Tracking branch: A metaphor
local tracking branch:
------master-------
remote tracking branch:
------origin/master-------
Local Repo
------master-------
Remote Repo
fetch
track merge
push
pull
Collaboration: push.default
● A complete push is like this:
○ git push origin master:master
● If you omit “ refspec”
during git push, like
○ git push
What would happen?
● cat .git/config
[branch "master"]
remote = origin
merge = refs/heads/master
It depends on how “push.default” is
configured:
git config --global push.default “xxx”
● nothing
○ do nothing. you must provide a ref
spec
● current
○ push origin
current_branch:current_branch
● upstream
push origin
current_branch:@{upstream}
● simple
push origin
current_branch:@{upstream}
here @{upstream} must have the same
name with current branch
● matching
push all local branches to remote with
Collaboration: pull-request
[luke@rmbp x]$ git request-pull -p HEAD~4 file:///tmp/x HEAD
The following changes since commit 61d4c4ff68d940906074fe6684687edf0e7180aa:
m1 (2015-07-29 13:31:20 +0800)
are available in the git repository at:
file:///tmp/x HEAD
for you to fetch changes up to 6e790570a168534e123c362b05db8055b53ec591:
t3 (2015-07-29 13:33:17 +0800)
----------------------------------------------------------------
Luke Luo (4):
t0
t1
t2
t3
t0.txt | 1 +
t1.txt | 1 +
t2.txt | 1 +
t3.txt | 1 +
4 files changed, 4 insertions(+)
…………..
Remote Repo: Demo
# modify “./ssh/config” to create entry for “osoci”
Host osoci
HostName 192.168.88.28
Port 22
User git
IdentityFile ~/.ssh/topdemo.pem
# login remote machine and create a “bare” remote repo[luke@rmbp ~]$ cd
localrepo/
[luke@rmbp ~]$ ssh osoci
Last login: Wed Aug 19 00:08:58 2015 from 222.129.52.83
[git@osoci ~]$ git init --bare /tmp/luke.git
Initialized empty Git repository in /tmp/luke.git/
[git@osoci ~]$ exit
logout
# create a local repo
[luke@rmbp ~]$ git init localrepo
Initialized empty Git repository in /home/luke/localrepo/.git/
# create a remote for local repo pointing to remote repo
[luke@rmbp ~]$ cd localrepo/
[luke@rmbp localrepo]$ git remote add osoci ssh://osoci/tmp/luke.git
[luke@rmbp localrepo]$ git remote -v
osoci ssh://osoci/tmp/luke.git (fetch)
osoci ssh://osoci/tmp/luke.git (push)
[luke@rmbp localrepo]$ cat .git/config
……….
[remote "osoci"]
url = ssh://osocipub/tmp/luke.git
fetch = +refs/heads/*:refs/remotes/osoci/*
# generate some commits in local repo “master”
[luke@rmbp localrepo]$ git simple-commit m0 m1 m2
# configuer “push.default” to “simple”
[luke@rmbp localrepo]$ git config --global push.default simple
# push local “master” to remote repo
[luke@rmbp localrepo]$ git push osoci master:master
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 626 bytes | 0 bytes/s, done.
Total 9 (delta 1), reused 0 (delta 0)
To ssh://osocipub/tmp/luke.git
* [new branch] master -> master
# a “remote tracking branch is created automatically
[luke@rmbp localrepo]$ git branch -avv
* master 3caa841 m2
remotes/osoci/master 3caa841 m2
Remote Repo: Demo
# checkout an “orphaned” branch “test”
[luke@rmbp localrepo]$ git checkout --orphan test
[luke@rmbp localrepo]$ git simple-commit t0 t1 t2
[luke@rmbp localrepo]$ git log --oneline --decorate --graph --all
* 61810bd (HEAD -> test) t2
* ce5fe1a t1
* 8970a60 t0
* 3caa841 (osoci/master, master) m2
* 0501ae6 m1
* 156e941 m0
# push “test” to remote repo, and create a “remote tracking branch”
[luke@rmbp localrepo]$ git push -u osoci test
[luke@rmbp localrepo]$ git branch -avv
master 3caa841 m2
* test 61810bd [osoci/test] t2
remotes/osoci/master 3caa841 m2
remotes/osoci/test 61810bd t2
# set up “master” ‘s remote tracking branch
[luke@rmbp localrepo]$ git branch -u remotes/osoci/master master
Branch master set up to track remote branch master from osoci.
[luke@rmbp localrepo]$ git branch -avv
* master 3caa841 [osoci/master] m2
test 61810bd [osoci/test] t2
remotes/osoci/master 3caa841 m2
remotes/osoci/test 61810bd t2
# create another local repo via “clone”
[luke@rmbp ~]$ git clone ssh://osocipub/tmp/luke.git clone-repo
[luke@rmbp ~]$ ls clone-repo
m0.txt m1.txt m2.txt
[luke@rmbp ~]$ cd clone-repo/
[luke@rmbp clone-repo]$ git branch -avv
* master 3caa841 [origin/master] m2
remotes/origin/HEAD -> origin/master
remotes/origin/master 3caa841 m2
remotes/origin/test 61810bd t2
# make commits in “master” and push to remote repo
[luke@rmbp clone-repo]$ git simple-commit m3 m4
[luke@rmbp clone-repo]$ git push origin
……..
To ssh://osocipub/tmp/luke.git
3caa841..12ae515 master -> master
# fetch remote update from repo “localrepo”
[luke@rmbp clone-repo]$ cd ../localrepo/
[luke@rmbp localrepo]$ git fetch osoci
………...
From ssh://osocipub/tmp/luke
3caa841..12ae515 master -> osoci/master
[luke@rmbp localrepo]$ git branch -avv
* master 3caa841 [osoci/master: behind 2] m2
test 61810bd [osoci/test] t2
remotes/osoci/master 12ae515 m4
Remote Repo: Demo
# merge change from remote “master” into local “master”
[luke@rmbp localrepo]$ git merge remotes/osoci/master
Updating 3caa841..12ae515
Fast-forward
m3.txt | 1 +
m4.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 m3.txt
create mode 100644 m4.txt
[luke@rmbp localrepo]$ git branch -avv
* master 12ae515 [osoci/master] m4
test 61810bd [osoci/test] t2
remotes/osoci/master 12ae515 m4
remotes/osoci/test 61810bd t2
# make more chang in local “master” and push it to remote repo
[luke@rmbp localrepo]$ git simple-commit m5 m6 m7
[luke@rmbp localrepo]$ git push osoci
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 654 bytes | 0 bytes/s, done.
Total 9 (delta 3), reused 0 (delta 0)
To ssh://osocipub/tmp/luke.git
12ae515..47cadbc master -> master
[luke@rmbp localrepo]$ git branch -avv
* master 47cadbc [osoci/master] m7
test 61810bd [osoci/test] t2
remotes/osoci/master 47cadbc m7
remotes/osoci/test 61810bd t2
Workflow: reference
https://www.atlassian.com/git/tutorials/com
paring-workflows/
http://nvie.com/posts/a-successful-git-
branching-model/
http://www.toptal.com/git/git-workflows-for-
pros-a-good-git-guide
http://documentup.com/skwp/git-
workflows-book
Workflow: Ad-hoc Peer to Peer
Tom
Ceph
Mary
Linda
fetch tom master:refs/remote/tom/master push linda master:refs/remote/linda/feature
Workflow: Centralized
eature Branch
Workflow: Gitflow
Workflow: Forking
Project Dependency:
internal software
repository server
external software
repository server
external
project
external
project
publish
sync
internal
project
internal
project
git git
CI server
release
internal
project
build
build dependency
build dependency
subtree/
submodule
Project Dependency:
Dependency Level :
● Level I : external public project
● Level II: internal friend project (different release cycle)
● Level III: internal projects which share part of code base (in different git repositories)
Solutions:
● Level I : package manager (yum/pacman/apt-get, maven, ivy,....)
● Level II: Internal package manager (yum/pacman/apt-get, maven, ivy,....)
● Level III: Git submodule/subtree
Project Dependency: Subtree
Project Dependency: manual
Subtree
● fetch subproject from remote repo:
○ git fetch remote-A master:proj-A/master
● read subproject tree into main project:
○ git checkout master
○ git read-tree --prefix=project-A proj-A/master@{tree}
○ git commit -m “add subproject A”
● fetch update for project A from remote repo:
○ git fetch remote-A master:proj-A/master
○ git checkout master
○ git merge -s subtree --prefix=project-A --squash --no-commit proj-A/master
○ git commit -m “update subproject A”
Project Dependency: git Subtree
man git-subtree:
GIT-SUBTREE(1) Git Manual
GIT-SUBTREE(1)
NAME
git-subtree - Merge subtrees together and split repository into subtrees
SYNOPSIS
git subtree add -P <prefix> <commit>
git subtree add -P <prefix> <repository> <ref>
git subtree pull -P <prefix> <repository> <ref>
git subtree push -P <prefix> <repository> <ref>
git subtree merge -P <prefix> <commit>
git subtree split -P <prefix> [OPTIONS] [<commit>]

More Related Content

What's hot

Git: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commandsGit: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commandsth507
 
Git Introduction
Git IntroductionGit Introduction
Git IntroductionGareth Hall
 
My Notes from https://www.codeschool.com/courses/git-real
My Notes from  https://www.codeschool.com/courses/git-realMy Notes from  https://www.codeschool.com/courses/git-real
My Notes from https://www.codeschool.com/courses/git-realEneldo Serrata
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptuallyseungzzang Kim
 
Git workflows automat-it
Git workflows automat-itGit workflows automat-it
Git workflows automat-itAutomat-IT
 
Git intro hands on windows with msysgit
Git intro hands on windows with msysgitGit intro hands on windows with msysgit
Git intro hands on windows with msysgitGeshan Manandhar
 
Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)Mizan Riqzia
 
Git: basic to advanced
Git: basic to advancedGit: basic to advanced
Git: basic to advancedYodalee
 

What's hot (20)

Git: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commandsGit: An introduction of plumbing and porcelain commands
Git: An introduction of plumbing and porcelain commands
 
Presentacion git
Presentacion gitPresentacion git
Presentacion git
 
Git tutorial
Git tutorialGit tutorial
Git tutorial
 
Learn Git Basics
Learn Git BasicsLearn Git Basics
Learn Git Basics
 
Git & GitHub WorkShop
Git & GitHub WorkShopGit & GitHub WorkShop
Git & GitHub WorkShop
 
Git SCM
Git SCMGit SCM
Git SCM
 
Git Introduction
Git IntroductionGit Introduction
Git Introduction
 
Git real slides
Git real slidesGit real slides
Git real slides
 
Git
GitGit
Git
 
My Notes from https://www.codeschool.com/courses/git-real
My Notes from  https://www.codeschool.com/courses/git-realMy Notes from  https://www.codeschool.com/courses/git-real
My Notes from https://www.codeschool.com/courses/git-real
 
Git training
Git trainingGit training
Git training
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptually
 
Git workflows automat-it
Git workflows automat-itGit workflows automat-it
Git workflows automat-it
 
Git intro hands on windows with msysgit
Git intro hands on windows with msysgitGit intro hands on windows with msysgit
Git intro hands on windows with msysgit
 
Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)
 
Git Real
Git RealGit Real
Git Real
 
Git github
Git githubGit github
Git github
 
Advanced Git
Advanced GitAdvanced Git
Advanced Git
 
390a gitintro 12au
390a gitintro 12au390a gitintro 12au
390a gitintro 12au
 
Git: basic to advanced
Git: basic to advancedGit: basic to advanced
Git: basic to advanced
 

Similar to Git Basic

Git Introduction Tutorial
Git Introduction TutorialGit Introduction Tutorial
Git Introduction TutorialThomas Rausch
 
Version Control and Git - GitHub Workshop
Version Control and Git - GitHub WorkshopVersion Control and Git - GitHub Workshop
Version Control and Git - GitHub WorkshopAll Things Open
 
git. WTF is it doing anyway?
git. WTF is it doing anyway?git. WTF is it doing anyway?
git. WTF is it doing anyway?Erin Zimmer
 
Git Tutorial Yang Yang
Git Tutorial Yang YangGit Tutorial Yang Yang
Git Tutorial Yang YangYang Yang
 
GIT: Content-addressable filesystem and Version Control System
GIT: Content-addressable filesystem and Version Control SystemGIT: Content-addressable filesystem and Version Control System
GIT: Content-addressable filesystem and Version Control SystemTommaso Visconti
 
slides.pdf
slides.pdfslides.pdf
slides.pdfvidsvagi
 
Git for beginners
Git for beginnersGit for beginners
Git for beginnersVinh Nguyen
 
Git Memento of basic commands
Git Memento of basic commandsGit Memento of basic commands
Git Memento of basic commandsZakaria Bouazza
 
GTFO: Git Theory For OpenSource
GTFO: Git Theory For OpenSourceGTFO: Git Theory For OpenSource
GTFO: Git Theory For OpenSourceForest Mars
 
New Views on your History with git replace
New Views on your History with git replaceNew Views on your History with git replace
New Views on your History with git replaceChristian Couder
 

Similar to Git Basic (20)

Version Control with Git
Version Control with GitVersion Control with Git
Version Control with Git
 
Git Introduction Tutorial
Git Introduction TutorialGit Introduction Tutorial
Git Introduction Tutorial
 
Version Control and Git - GitHub Workshop
Version Control and Git - GitHub WorkshopVersion Control and Git - GitHub Workshop
Version Control and Git - GitHub Workshop
 
git. WTF is it doing anyway?
git. WTF is it doing anyway?git. WTF is it doing anyway?
git. WTF is it doing anyway?
 
Git internals
Git internalsGit internals
Git internals
 
Git Tutorial Yang Yang
Git Tutorial Yang YangGit Tutorial Yang Yang
Git Tutorial Yang Yang
 
GIT: Content-addressable filesystem and Version Control System
GIT: Content-addressable filesystem and Version Control SystemGIT: Content-addressable filesystem and Version Control System
GIT: Content-addressable filesystem and Version Control System
 
git - the basics
git - the basicsgit - the basics
git - the basics
 
Get on with git
Get on with gitGet on with git
Get on with git
 
slides.pdf
slides.pdfslides.pdf
slides.pdf
 
slides.pdf
slides.pdfslides.pdf
slides.pdf
 
git internals
git internalsgit internals
git internals
 
Git for beginners
Git for beginnersGit for beginners
Git for beginners
 
Git Memento of basic commands
Git Memento of basic commandsGit Memento of basic commands
Git Memento of basic commands
 
Git
GitGit
Git
 
Git internals
Git internalsGit internals
Git internals
 
Tech thursdays / GIT
Tech thursdays / GITTech thursdays / GIT
Tech thursdays / GIT
 
GTFO: Git Theory For OpenSource
GTFO: Git Theory For OpenSourceGTFO: Git Theory For OpenSource
GTFO: Git Theory For OpenSource
 
Git
GitGit
Git
 
New Views on your History with git replace
New Views on your History with git replaceNew Views on your History with git replace
New Views on your History with git replace
 

Recently uploaded

SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 

Recently uploaded (20)

SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 

Git Basic

  • 1. Git Basic Learn via Practice luke.jf.luo@gmail.com
  • 2. tmux cheat sheet config file: ~/.tmux.conf tmux new -s luke tmux attach -t luke tmux new -s luke-1 -t luke key-bind: <ctrl-a> + help: ? : list all key binds sessions: s : list and switch session(return) d : detach session layout: [space]: switch layout Alt 1-5 : select layout 1-5 windows: c : new window k: close window p/n : previous/next window 0-9: select windows 0/9 w: list and select window panes: v: vertical pane h: horizontal pane z: zoom pane x: close pane up/down/lef/right: select pane Alt-[up/down/left/right]: resize pane copy/paste: [ : enter copy mode. [space] to begin select, [up/down/left/right] to select text,[return] to end select ]: paste logging: ctrl-p : output current pane to log config: r : reload config file
  • 3. ssh: git bash [luke@rmbp ~]$ ssh osoci Last login: Sat Jul 25 18:31:58 2015 from 203.187.164.42 git@osoci:~$ pwd /home/git git@osoci:~$ tmux [luke@rmbp ~]$ cat .ssh/config Host osoci HostName 192.168.88.28 Port 22 User git IdentityFile ~/.ssh/topdemo.pem ServerAliveInterval 15 ServerAliveCountMax 2
  • 4. ssh : bitvise client luke@LUKE-WIN7 ~ $ scp osoci:~/BvSshClient-Inst.exe ./ BvSshClient-Inst.exe 100% 10MB 1.7MB/s 00:06
  • 5. git init git@osoci:~$ pwd /home/git git@osoci:~$ mkdir luke git@osoci:~$ cd luke git@osoci:~/luke$ git init Initialized empty Git repository in /home/git/luke/.git/ git@osoci:~/luke (master)$ git@osoci:~/luke (master)$ tree -a . └── .git ├── branches ├── config ├── description ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ………….. │ └── update.sample ├── info │ └── exclude ├── objects │ ├── info │ └── pack └── refs ├── heads └── tags 10 directories, 13 files
  • 6. Exercise: create an example model eaa5 1f48 66a3 b.txt c.txta.txt 43b2 2170 c830 d.txt d.txt subdir/ 24b1 90c6 54ed 4819 v1.0 v2.0 a.txt b.txt c.txt b2 b1HEAD Legends: blob tree commit tag reference
  • 7. Questions: Porcelain versus Plumbing ● Porcelain commands are implemented via plumbing commands. ● Please explain how porcelain commands in the left are implemented via plumbing commands in the right. 1. git add/mv/rm 2. git commit 3. git checkout [-b] 4. git reset [--soft] [--hard] 5. git branch 6. git format-patch/am 7. git diff [--cached] [commit] 8. git status 1. git hash-object 2. git cat-file 3. git update-index 4. git checkout-index 5. git read-tree 6. git write-tree 7. git commit-tree 8. git update-ref 9. git symbolic-ref 10. git ls-tree 11. git mktree 12. git ls-files 13. git diff-files/diff-index/diff-tree
  • 8. Tools: Three Areas ls-files -s update-index checkout-index read-tree write-tree commit-tree ls cd rm -rf mkdir nano a.txt cat tree hash-object cat-object show tag ls-tree log Working Tree Index DB update-index checkout-index write-tree read-tree commit-tree hash-object add/rm/mv
  • 9. Commands: create ● git hash-object -t blob -w a.txt ○ write a.txt into DB as a blob ● git update-index --cache-info 100644,hashid,a.txt ○ add a object as an entry in index tree with path name “a.txt” ● git checkout-index -a ○ checkout all files in index into working tree ● git write-tree ○ write index tree into DB and generate a tree object ● git read-tree --prefix pathname tree-hashid ○ read an existing tree in DB and generate an entry in index tree with name “pathname” ● git-commit-tree -m “commit index tree” -p commit-hashid tree-hashid ○ write index tree and generate a commit object that points to this tree with commit message and parent commit ● git tag -a -m “generate v1.0 tag” v1.0 commit-hashid ○ generate an annotated tag with name “v1.0” which point to a commit
  • 10. Commands: Retrieve ● git ls-files -s ○ display content of index ● git hash-object -p hashid ○ display the content of object with hashid ● git show hashid ○ display object with hashid ● git log commit-hashid ○ display a commit ● git ls-tree tree-hashid ○ display a tree ● git tag -l ○ list all tags ● git tag -v v1.0 ○ display contents of tag “v1.0”
  • 12. objects: blob create [git@osoci luke]$ tree -a .git/objects .git/objects ├── info └── pack [git@osoci luke]$ ls -l total 12 -rw-r--r-- 1 git git 6 Jul 26 08:15 a.txt -rw-r--r-- 1 git git 6 Jul 26 08:15 b.txt -rw-r--r-- 1 git git 6 Jul 26 08:16 c.txt [git@osoci luke]$ git hash-object -t blob a.txt eaa5fa8755fc20f08d0b3da347a5d1868404e4 62 [git@osoci luke]$ git hash-object -t blob b.txt 1f482482efa7cc35d1dbab733e23a10c97a936 4f [git@osoci luke]$ git hash-object -t blob c.txt 66a370d7c2b2035179287c876fbbc59a615c6 d42
  • 13. objects: blob create [git@osoci luke]$ tree -a .git/objects .git/objects ├── 1f │ └── 482482efa7cc35d1dbab733e23a10c97a9364f ├── 66 │ └── a370d7c2b2035179287c876fbbc59a615c6d42 ├── ea │ └── a5fa8755fc20f08d0b3da347a5d1868404e462 ├── info └── pack 5 directories, 3 files [git@osoci luke]$ git hash-object -t blob -w a.txt eaa5fa8755fc20f08d0b3da347a5d1868404e462 [git@osoci luke]$ git hash-object -t blob -w b.txt 1f482482efa7cc35d1dbab733e23a10c97a9364f [git@osoci luke]$ git hash-object -t blob -w c.txt 66a370d7c2b2035179287c876fbbc59a615c6d42
  • 14. objects: blob structure [git@osoci luke]$ file .git/objects/66/a370d7c2b2035179287c876fbbc59a615c6d42 .git/objects/66/a370d7c2b2035179287c876fbbc59a615c6d42: zlib compressed data [git@osoci luke]$ zpipe -d < .git/objects/ea/a5fa8755fc20f08d0b3da347a5d1868404e462 > a.txt.blob [git@osoci luke]$ hexdump -C a.txt.blob 00000000 62 6c 6f 62 20 36 00 61 2e 74 78 74 0a |blob 6.a.txt.| 0000000d [git@osoci luke]$ shasum a.txt.blob eaa5fa8755fc20f08d0b3da347a5d1868404e462 a.txt.blob [git@osoci luke]$ zpipe < a.txt.blob > a.txt.blob.compressed [git@osoci luke]$ cmp a.txt.blob.compressed .git/objects/ea/a5fa8755fc20f08d0b3da347a5d1868404e462
  • 15. objects: blob retrieve [git@osoci luke]$ git cat-file -t eaa5fa8755fc20f08d0b3da347a5d1868404e462 blob [git@osoci luke]$ git cat-file -p eaa5fa8755fc20f08d0b3da347a5d1868404e462 a.txt [git@osoci luke]$ git show eaa5fa8755fc20f08d0b3da347a5d1868404e462 a.txt
  • 16. objects: blobs eaa5 1f48 66a3 b.txt c.txta.txt
  • 17. objects: indx tree create (1) [git@osoci luke]$ git update-index --add --cacheinfo 100644,eaa5fa8755fc20f08d0b3da347a5d1868404e462,a.txt --cacheinfo 100644,1f482482efa7cc35d1dbab733e23a10c97a9364f,b.txt --cacheinfo 100644,66a370d7c2b2035179287c876fbbc59a615c6d42,c.txt [git@osoci luke]$ git ls-files -s 100644 eaa5fa8755fc20f08d0b3da347a5d1868404e462 0a.txt 100644 1f482482efa7cc35d1dbab733e23a10c97a9364f 0b.txt 100644 66a370d7c2b2035179287c876fbbc59a615c6d42 0 c.txt
  • 18. objects: index tree create(1) [git@osoci luke]$ file .git/index .git/index: Git index, version 2, 3 entries [git@osoci luke]$ tree -a .git/objects .git/objects ├── 1f │ └── 482482efa7cc35d1dbab733e23a10c97a9364f ├── 66 │ └── a370d7c2b2035179287c876fbbc59a615c6d42 ├── ea │ └── a5fa8755fc20f08d0b3da347a5d1868404e462 ├── info └── pack 5 directories, 3 files
  • 19. objects: index tree create(2) [git@osoci luke]$ rm a.txt b.txt c.txt [git@osoci luke]$ git ls-files -s 100644 eaa5fa8755fc20f08d0b3da347a5d1868404e462 0a.txt 100644 1f482482efa7cc35d1dbab733e23a10c97a9364f 0b.txt 100644 66a370d7c2b2035179287c876fbbc59a615c6d42 0 c.txt [git@osoci luke]$ git ls-files -s > index.tree [git@osoci luke]$ rm .git/index [git@osoci luke]$ git ls-files -s [git@osoci luke]$ cat index.tree | git update-index --index-info [git@osoci luke]$ git ls-files -s 100644 eaa5fa8755fc20f08d0b3da347a5d1868404e462 0a.txt 100644 1f482482efa7cc35d1dbab733e23a10c97a9364f 0b.txt 100644 66a370d7c2b2035179287c876fbbc59a615c6d42 0 c.txt
  • 20. objects: index tree create(2) [git@osoci luke]$ ls a.txt b.txt c.txt index.tree [git@osoci luke]$ rm *.txt index.tree [git@osoci luke]$ ls [git@osoci luke]$ git checkout-index -a [git@osoci luke]$ ls a.txt b.txt c.txt
  • 21. objects: tree create [git@osoci luke]$ git cat-file -t 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf tree [git@osoci luke]$ git cat-file -p 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf 100644 blob eaa5fa8755fc20f08d0b3da347a5d1868404e462 a.txt 100644 blob 1f482482efa7cc35d1dbab733e23a10c97a9364f b.txt 100644 blob 66a370d7c2b2035179287c876fbbc59a615c6d42 c.txt [git@osoci luke]$ git show 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf tree 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf a.txt b.txt c.txt [git@osoci luke]$ git write-tree 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf [git@osoci luke]$ find .git/objects/ -type f .git/objects/66/a370d7c2b2035179287c876fbbc59a615c6d42 .git/objects/43/b2935d64c7fdb71dd299337ef0a9a6d95e48bf .git/objects/1f/482482efa7cc35d1dbab733e23a10c97a9364f .git/objects/ea/a5fa8755fc20f08d0b3da347a5d1868404e462 [git@osoci luke]$ git ls-tree 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf 100644 blob eaa5fa8755fc20f08d0b3da347a5d1868404e462 a.txt 100644 blob 1f482482efa7cc35d1dbab733e23a10c97a9364f b.txt 100644 blob 66a370d7c2b2035179287c876fbbc59a615c6d42 c.txt
  • 22. objects: blob+tree eaa5 1f48 66a3 b.txt c.txta.txt 43b2
  • 23. objects: tree with subdir [git@osoci luke]$ echo "d.txt" > d.txt [git@osoci luke]$ git hash-object -w d.txt c830ea98d189c62bb530f061e2467e52da775b33 [git@osoci luke]$ git read-tree --empty [git@osoci luke]$ git update-index --add --cacheinfo 100400,c830ea98d189c62bb530f061e2467e52da775b33,d.txt [git@osoci luke]$ git ls-files -s 100644 c830ea98d189c62bb530f061e2467e52da775b33 0 d.txt
  • 25. objects: blob+tree eaa5 1f48 66a3 b.txt c.txta.txt 43b2 2170 c830 d.txt d.txt subdir/
  • 26. objects: commit commit-tree author name/email committer name/email index tree date commit message parent commits commit
  • 27. objects: commit config [git@osoci luke]$ git config --global user.name "Luke Luo" [git@osoci luke]$ git config --global user.email "luke.jf.luo@gmail.com" [git@osoci luke]$ git config --list user.name=Luke Luo user.email=luke.jf.luo@gmail.com ui.color=false core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true
  • 28. objects: commit [git@osoci luke]$ git commit-tree -m "parent commit" 43b2 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 [git@osoci luke]$ git commit-tree -m "child commit" -p 24b12170 90c6523c6f71522b0f884f250af2aae2e941ee43 [git@osoci luke]$ git cat-file -p 24b1 tree 43b2935d64c7fdb71dd299337ef0a9a6d95e48bf author Luke Luo <luke.jf.luo@gmail.com> 1437897133 +0800 committer Luke Luo <luke.jf.luo@gmail.com> 1437897133 +0800 parent commit [git@osoci luke]$ git cat-file -p 90c6 tree 21702cd074d61a5b5581142e66a8db8154bb161f parent 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 author Luke Luo <luke.jf.luo@gmail.com> 1437897181 +0800 committer Luke Luo <luke.jf.luo@gmail.com> 1437897181 +0800 child commit
  • 29. objects: commit [git@osoci luke]$ cd .git/refs/heads/ [git@osoci heads]$ echo 90c6523c6f71522b0f884f250af2aae2e941ee43 > master [git@osoci heads]$ git log commit 90c6523c6f71522b0f884f250af2aae2e941ee43 Author: Luke Luo <luke.jf.luo@gmail.com> Date: Sun Jul 26 15:53:01 2015 +0800 child commit commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 Author: Luke Luo <luke.jf.luo@gmail.com> Date: Sun Jul 26 15:52:13 2015 +0800 parent commit [git@osoci heads]$ git branch * master
  • 30. objects: blob+tree+commit eaa5 1f48 66a3 b.txt c.txta.txt 43b2 2170 c830 d.txt d.txt subdir/ 24b1 90c6
  • 31. objects: tag [git@osoci luke]$ git tag -a -m "v1.0 tag" v1.0 24b1 [git@osoci luke]$ git tag -a -m "v2.0 tag" v2.0 90c6 [git@osoci luke]$ ls .git/refs/tags/ v1.0 v2.0 [git@osoci luke]$ cat .git/refs/tags/v1.0 54ed67752ab1c0f0ca3ede2cfc77f06b014a0665 [git@osoci luke]$ cat .git/refs/tags/v2.0 481973c0fce98ba63b2aeaa3ab95f1694964e4c1 [git@osoci luke]$ git cat-file -p 481973 object 90c6523c6f71522b0f884f250af2aae2e941ee43 type commit tag v2.0 tagger Luke Luo <luke.jf.luo@gmail.com> 1437903166 +0800 v2.0 tag [git@osoci luke]$ git cat-file -p v1.0 object 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 type commit tag v1.0 tagger Luke Luo <luke.jf.luo@gmail.com> 1437903152 +0800 v1.0 tag [git@osoci luke]$ git tag v1.0 v2.0
  • 32. objects: tag [git@osoci luke]$ git show v1.0 tag v1.0 Tagger: Luke Luo <luke.jf.luo@gmail.com> Date: Sun Jul 26 17:32:32 2015 +0800 v1.0 tag commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 Author: Luke Luo <luke.jf.luo@gmail.com> Date: Sun Jul 26 15:52:13 2015 +0800 parent commit diff --git a/a.txt b/a.txt new file mode 100644 index 0000000..eaa5fa8 --- /dev/null ………………….. [git@osoci luke]$ git log v1.0 commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 Author: Luke Luo <luke.jf.luo@gmail.com> Date: Sun Jul 26 15:52:13 2015 +0800 parent commit [git@osoci luke]$ git log v2.0 commit 90c6523c6f71522b0f884f250af2aae2e941ee43 Author: Luke Luo <luke.jf.luo@gmail.com> Date: Sun Jul 26 15:53:01 2015 +0800 child commit commit 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 Author: Luke Luo <luke.jf.luo@gmail.com> Date: Sun Jul 26 15:52:13 2015 +0800 parent commit
  • 33. Objects: blob+tree+commit+tag eaa5 1f48 66a3 b.txt c.txta.txt 43b2 2170 c830 d.txt d.txt subdir/ 24b1 90c6 54ed 4819 v1.0 v2.0 a.txt b.txt c.txt
  • 34. objects: Repo housekeeping ● git prune ○ remove all objects not referenced via branch/tag/reflog/stash ● git pack-objects basename < object-list ○ pack objects into a single pack file/index file ● git repack -a ○ pack all objects under .git/objects/pack/ ● git gc --aggressive --prune=now ○ garbage collection and many housekeeping
  • 35. index: operations ● display ○ git ls-files -s ● clear ○ git read-tree --empty ● add file entry in working tree ○ git update-index --add a.txt b.txt ● add file entry from repo ○ git update-index --add --cacheinfo mode,hashid,path ○ git update-index --indexinfo < index.entry ● add whole tree from repo ○ git read-tree --prefix=<xxx> tree-ish ● checkout index into working tree ○ git checkout-index -a ○ git checkout-index -- filename ● write index tree into repo ○ git write-tree
  • 36. refs: [git@osoci luke]$ cat ref.create create refs/heads/b1 90c6523c6f71522b0f884f250af2aae2e941ee43 create refs/heads/b2 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 [git@osoci luke]$ cat ref.create | git update-ref --stdin [git@osoci luke]$ ls .git/refs/heads/ b1 b2 master [git@osoci luke]$ cat .git/refs/heads/{b1,b2} 90c6523c6f71522b0f884f250af2aae2e941ee43 24b1c6cb1d93933fd3eaf9af28e4bad180d19265 [git@osoci luke]$ git branch b1 b2 * master
  • 37. refs: symbolic [git@osoci luke]$ cat .git/HEAD ref: refs/heads/master [git@osoci luke]$ git branch b1 b2 * master [git@osoci luke]$ git symbolic-ref HEAD refs/heads/b1 [git@osoci luke]$ git branch * b1 b2 master [git@osoci luke]$ cat .git/HEAD ref: refs/heads/b1
  • 38. refs: special references ● HEAD - The currently checked-out commit/branch. ○ HEAD can contain either a symbolic ref or a commit hash. ● FETCH_HEAD - The most recently fetched branch from a remote repo. ● ORIG_HEAD - A backup reference to HEAD before drastic changes to it. ● MERGE_HEAD - The commit(s) that you’re merging into the current branch with git merge. ● CHERRY_PICK_HEAD - The commit that you are cherry-picking.
  • 39. reflog: ref value history Git will record all branch/tag/HEAD changes in .git/logs [luke@rmbp x]$ tree .git/logs .git/logs ├── HEAD └── refs └── heads ├── master └── remote └── y └── master 4 directories, 3 files [luke@rmbp x]$ cat .git/logs/refs/heads/master 0000000000000000000000000000000000000000 e6c4b7bcecde7ef05a3bc512b2445d841a9b2c56 Luke Luo <luke.jf.luo@gmail.com> 1438149983 +0800 commit (initial): x0 e6c4b7bcecde7ef05a3bc512b2445d841a9b2c56 3a460bd4c2b298230253b278b59689e2ec46d2b4 Luke Luo <luke.jf.luo@gmail.com> 1438149983 +0800 commit: x1 3a460bd4c2b298230253b278b59689e2ec46d2b4 9579bc8a3c878ff32bd202f873e7c475e89b6b71 Luke Luo
  • 40. reflog: get back previous ref values [luke@rmbp x]$ git reflog show fd31956 HEAD@{0}: push 1972550 HEAD@{1}: push 9579bc8 HEAD@{2}: commit: x2 3a460bd HEAD@{3}: commit: x1 e6c4b7b HEAD@{4}: commit (initial): x0
  • 41. Objects+refs : recap eaa5 1f48 66a3 b.txt c.txta.txt 43b2 2170 c830 d.txt d.txt subdir/ 24b1 90c6 54ed 4819 v1.0 v2.0 a.txt b.txt c.txt b2 b1 HEAD WIP commit
  • 42. case study: checkout -- filepath - checkout -- filepath repo index work tree checkout master -- filepath checkout -- filepath HEAD master
  • 43. case study: checkout [branch] - checkout [branch] repo index work tree checkout master HEAD orig branch master
  • 44. case study: checkout [commit] - checkout [commit] : detach checkout repo index work tree checkout a3f678 HEAD master a3f678
  • 45. case study: checkout [commit] - checkout [commit] - checkout --detach [branch] repo index work tree checkout a3f678 HEAD master a3f678
  • 46. case study: checkout -b - checkout -b [new-branch] [old-branch/commit] HEAD old-branch new-branch parent commit
  • 47. case study: checkout --orphan - checkout --orphan new-branch HEAD old-branch new-branch parent commit
  • 48. case study: reset - git reset a23e45 HEAD master a23e45…...
  • 49. case study: stash How stash is implemented via “reflog” and object model? [luke@rmbp stash]$ git simple-commit a b c [luke@rmbp stash]$ ls a.txt b.txt c.txt [luke@rmbp stash]$ echo "change a.txt" > a.txt [luke@rmbp stash]$ echo "change b.txt" > b.txt [luke@rmbp stash]$ echo "untracked file " > d.txt [luke@rmbp stash]$ git add a.txt [luke@rmbp stash]$ git stash save Saved working directory and index state WIP on master: 5b32284 c HEAD is now at 5b32284 c [luke@rmbp stash]$ git stash list stash@{0}: WIP on master: 5b32284 c
  • 51. tool command : git simple-commit [luke@rmbp x]$ git init Initialized empty Git repository in /tmp/x/.git/ [luke@rmbp x]$ git simple-commit a b c [master (root-commit) 167c0ef] a 1 file changed, 1 insertion(+) create mode 100644 a.txt [master 88b4872] b 1 file changed, 1 insertion(+) create mode 100644 b.txt [master 2322a7f] c 1 file changed, 1 insertion(+) create mode 100644 c.txt [luke@rmbp x]$ ls a.txt b.txt c.txt [luke@rmbp x]$ cat a.txt a [luke@rmbp x]$ cat b.txt b [luke@rmbp x]$ cat c.txt c [luke@rmbp x]$ git log commit 2322a7f97b833bd9a0411e9514d2cc7cbb549c17 Author: Luke Luo <luke.jf.luo@gmail.com> Date: Fri Jul 31 06:43:07 2015 +0800 c commit 88b4872eab7060fee9b617fdacc3c4f92ed33abc Author: Luke Luo <luke.jf.luo@gmail.com> Date: Fri Jul 31 06:43:07 2015 +0800 b commit 167c0eff7f1076ceadcf27069713f7beccb3953d Author: Luke Luo <luke.jf.luo@gmail.com> Date: Fri Jul 31 06:43:07 2015 +0800 a
  • 52. diff: algorithm longest common subsequence problem: https://en.wikipedia.org/wiki/Diff_utility#Algorithm example: a b c d f g h j q z a b c d e f g i j k r x y z a b c d f g j z e h i q k r x y + - + - + + + +
  • 53. diff: unified format [git@osoci luke]$ diff -u orig.txt new.txt --- orig.txt 2015-07-27 06:39:52.675733655 +0800 +++ new.txt 2015-07-27 06:40:43.242127477 +0800 @@ -1,3 +1,9 @@ +This is an important +notice! It should +therefore be located at +the beginning of this +document! + This part of the document has stayed the same from version to @@ -5,16 +11,10 @@ be shown if it doesn't change. Otherwise, that would not be helping to -compress the size of the -changes. - -This paragraph contains -text that is outdated. -It will be deleted in the -near future. +compress anything.
  • 54. git diff: enhanced unified format [git@osoci luke]$ git diff diff --git a/orig.txt b/orig.txt index c49483c..3dc52fe 100644 --- a/orig.txt +++ b/orig.txt @@ -1,3 +1,9 @@ +This is an important +notice! It should +therefore be located at +the beginning of this +document! + This part of the document has stayed the same from version to @@ -5,16 +11,10 @@ version. It shouldn't be shown if it doesn't change. Otherwise, that would not be helping to -compress the size of the -changes. - -This paragraph contains -text that is outdated. -It will be deleted in the -near future. +compress anything. It is important to spell -check this dokument. On +check this document. On the other hand, a misspelled word isn't the end of the world.
  • 55. git diff: trees ● git diff: working tree ----> index ● git diff --cached : index ---> repo commit ● git diff HEAD : working tree → repo commit ● git diff commit1 commit2 : commit tree 1 in repo ----> commit tree 2 in repo
  • 56. git diff: raw output format git diff --raw · A: addition of a file · C: copy of a file into a new one · D: deletion of a file · M: modification of the contents or mode of a file · R: renaming of a file · T: change in the type of the file · U: file is unmerged (you must complete the merge before it can be committed) · X: "unknown" change type (most probably a bug, please report it)
  • 57. git difftool [luke@rmbp ~]$ git difftool --tool-help 'git difftool --tool=<tool>' may be set to one of the following: gvimdiff gvimdiff2 gvimdiff3 kdiff3 kompare meld vimdiff vimdiff2 vimdiff3 The following tools are valid, but not currently available: araxis bc bc3 codecompare deltawalker diffmerge diffuse ecmerge emerge opendiff p4merge tkdiff xxdiff
  • 58. git difftool [git@osoci linux]$ git difftool -t vimdiff v4.1-rc8 v4.1
  • 59. patch/git apply reference: http://www.thegeekstuff.com/2014/12/patch-command-examples/ [git@osoci ~]$ git clone http://192.168.88.12:8080/frank/git-tutorial.git [git@osoci ~]$ cd git-tutorial [git@osoci git-tutorial]$ ls git_advanced.md Git分支模型最佳实践.pdf huhongyan.txt README.md TortoiseGit zhanglingfeng.txt zhangxinxu.txt git_commands.md huanglianbin.txt markdown_syntax.md test.txt yhbtest.txt zhangtao.txt 幻灯片 [git@osoci git-tutorial]$ git rm *.txt [git@osoci git-tutorial]$ git commit -m "delete txt files" [git@osoci git-tutorial]$ git diff HEAD HEAD^ > my.diff [git@osoci git-tutorial]$ git apply --stat my.diff [git@osoci git-tutorial]$ git apply --check my.diff [git@osoci git-tutorial]$ git apply my.diff [git@osoci git-tutorial]$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) huanglianbin.txt huhongyan.txt my.diff test.txt yhbtest.txt zhanglingfeng.txt zhangtao.txt zhangxinxu.txt nothing added to commit but untracked files present (use "git add" to track)
  • 60. git apply -R [git@osoci git-tutorial]$ git apply -R my.diff [git@osoci git-tutorial]$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) my.diff nothing added to commit but untracked files present (use "git add" to track)
  • 61. git format-patch/am [[git@osoci luke]$ git init Initialized empty Git repository in /home/git/luke/.git/ [git@osoci luke]$ rm -rf *.txt [git@osoci luke]$ git simple-commit m0 m1 m2 m3 [master (root-commit) d513ee0] m0 [git@osoci luke]$ git log commit 29ec6becef10fceb126a52ac6bc731f674f19c3b commit 35e2be099f848dffc096bfd4a53b544fac868272 commit f9f91093a6427ec51caa1e86003b152fcb1ba251 commit d513ee08d967114205b4f9892da73eb810125494 [git@osoci luke]$ git checkout -b test d513ee Switched to a new branch 'test' [git@osoci luke]$ git format-patch HEAD..master 0001-m1.patch 0002-m2.patch 0003-m3.patch [git@osoci luke]$ git am --signoff *.patch Applying: m1 Applying: m2 Applying: m3 [git@osoci luke]$ git log commit 5e6de70e06d62ae4ea1894e5beabfd0f1cf3a93f Author: Luke Luo <luke.jf.luo@gmail.com> Date: Mon Jul 27 12:55:42 2015 +0800 m3 Signed-off-by: Luke Luo <luke.jf.luo@gmail.com> commit ee61f13466eef391fcf238839bebf8fcc500b41b Author: Luke Luo <luke.jf.luo@gmail.com> Date: Mon Jul 27 12:55:42 2015 +0800 m2 Signed-off-by: Luke Luo <luke.jf.luo@gmail.com>
  • 62. git diff: patch-id ● every commit has a diff with it (comparing to its parent) ● hash the diff to get a hashid ● identical patchid => commits introduce same change [git@osoci rebase]$ git branch master * test [git@osoci rebase]$ git simple-commit patchid [git@osoci rebase]$ git show test | git patch-id 32bd856835dce7f3ebacf9c747c3bc01229fcf6c 5b8b610da4a3680234ad612639b8b875563425eb [git@osoci rebase]$ git checkout master Switched to branch 'master' [git@osoci rebase]$ git simple-commit patchid [git@osoci rebase]$ git show master | git patch-id 32bd856835dce7f3ebacf9c747c3bc01229fcf6c 86ba15eb53d069a9e3a33ca9469c20f45925c6ac
  • 63. git cherry What change have been applied to upstream? c0 c1 c2 t1’ c4 master t0 t1 test [git@osoci patchid]$ git init [git@osoci patchid]$ git simple-commit c0 c1 c2 [[git@osoci patchid]$ git checkout -b test HEAD~2 [git@osoci patchid]$ git simple-commit t0 t1 [git@osoci patchid]$ git format-patch master 0001-t0.patch 0002-t1.patch [git@osoci patchid]$ git checkout master [git@osoci patchid]$ git am 0002-t1.patch Applying: t1 [git@osoci patchid]$ git checkout test [git@osoci patchid]$ git log --oneline 1b99116 t1 958eab6 t0 a08f762 c0 [git@osoci patchid]$ git cherry master + 958eab6fc6ce904875949e10521e9879565e4eb5 - 1b9911603afc320f4d3aec2e8caef871d8aad511
  • 64. merge: 3 way ref: http://www.drdobbs.com/tools/three-way-merging-a-look-under-the-hood/240164902
  • 66. merge: recursive base ref: http://codicesoftware.blogspot.com/2011/09/merge-recursive-strategy.html git merge-base src dst result: ???? base: ancestor2 src: 16 dst: 15 ancestor2 = merge(ancestor0, ancestor1)
  • 68. merge: strategies man git-merge: git merge -s [strategies] -X [strategy options] Strategies: ● resolve ● recursive(default) ○ ours ○ theirs ○ patience ○ diff-algorithm=[patience|minimal|histogram|myers] ○ ignore-space-change, ignore-all-space, ignore-space-at-eol ○ renormalize,no-renormalize ○ subtree[=<path>] ● octopus (heads>=3) ● ours ● subtree
  • 69. merge: typical scenarios c0 c1 c2 c3 c4 HEAD c5 git merge t1 c1 c4 t1 c5base dst src merged t0 t1
  • 70. merge: typical scenarios c0 c1 c2 c3 c4 HEAD c5 git revert c4 c4 c4 c3 c5base dst src merged
  • 71. merge: typical scenarios c0 c1 c2 c3 c4 HEAD c5 git revert c2 c2 c4 c1 c5base dst src merged
  • 72. merge: typical scenarios c0 c1 c2 c3 c4 HEAD c5 git cherry-pick t0 c1 c4 t0 c5base dst src merged t0 t1
  • 73. merge: typical scenarios c0 c1 c2 c3 c4 HEAD c5 git cherry-pick t1 t0 c4 t1 c5base dst src merged t0 t1
  • 74. merge: typical scenarios c0 c1 c2 c3 c4 HEAD git rebase c4 t1 c1 c4 t0 t0’base dst src merged t0 t1 t0’ t1’ t0 t0’ t1 t1’base dst merged HEAD
  • 75. merge: conflicts [git@osoci ~]$ rm -rf luke [git@osoci ~]$ mkdir luke [git@osoci ~]$ cd luke [git@osoci luke]$ git init Initialized empty Git repository in /home/git/luke/.git/ [git@osoci luke]$ git simple-commit m0 m1 m2 [git@osoci luke]$ git checkout -b test HEAD~1 Switched to a new branch 'test' [git@osoci luke]$ ls m0.txt m1.txt [git@osoci luke]$ cat > m0.txt <<EOF > test m0 > test m0 add a line > EOF [git@osoci luke]$ cat > t0.txt <<EOF > t0 new file > EOF [git@osoci luke]$ git rm m1.txt rm 'm1.txt' [git@osoci luke]$ git add t0.txt m0.txt
  • 76. merge: conflicts [git@osoci luke]$ git checkout master cat > m0.txt <<EOF >m0 >m0 add a line EOF [git@osoci luke]$ git merge --no-commit test Removing m1.txt Auto-merging m0.txt CONFLICT (content): Merge conflict in m0.txt Automatic merge failed; fix conflicts and then commit the result. [git@osoci luke]$ git ls-files -s 100644 277540bebeee7be0642d87437590629e738fc184 1 m0.txt 100644 41b3b6f418ae91add3ff50569eac9afb780322c8 2 m0.txt 100644 21b10565974d4dbe96856e064fc56df997294190 3 m0.txt 100644 08bb2331e777f431177c40df6841c0034f89fb58 0 m2.txt 100644 36fb22f687676ab60973a770b274d62da04cb3ba 0 t0.txt [git@osoci luke]$ cat m0.txt <<<<<<< HEAD m0 m0 add a line ======= test m0 test m0 add a line >>>>>>> test
  • 77. merge: conflicts [git@osoci luke]$ git ls-files -s 100644 277540bebeee7be0642d87437590629e738fc184 1 m0.txt 100644 41b3b6f418ae91add3ff50569eac9afb780322c8 2 m0.txt 100644 21b10565974d4dbe96856e064fc56df997294190 3 m0.txt 100644 08bb2331e777f431177c40df6841c0034f89fb58 0 m2.txt 100644 36fb22f687676ab60973a770b274d62da04cb3ba 0 t0.txt [git@osoci luke]$ git checkout --ours -- m0.txt [git@osoci luke]$ git checkout --theirs -- m0.txt [git@osoci luke]$ git checkout-index --stage=2 -- m0.txt [git@osoci luke]$ git checkout-index --stage=3 -- m0.txt
  • 78. merge: conflicts [git@osoci luke]$ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") Changes to be committed: deleted: m1.txt new file: t0.txt Unmerged paths: (use "git add <file>..." to mark resolution) both modified: m0.txt [git@osoci luke]$ git diff diff --cc m0.txt index 41b3b6f,21b1056..0000000 --- a/m0.txt +++ b/m0.txt @@@ -1,2 -1,2 +1,7 @@@ ++<<<<<<< HEAD +m0 +m0 add a line ++======= + test m0 + test m0 add a line ++>>>>>>> test
  • 79. merge: conflicts [git@osoci luke]$ echo "merged" > m0.txt [git@osoci luke]$ git add m0.txt [git@osoci luke]$ git ls-files -s 100644 20b117fdd3804508359ec883abe519486f0d19dd 0 m0.txt 100644 08bb2331e777f431177c40df6841c0034f89fb58 0 m2.txt 100644 36fb22f687676ab60973a770b274d62da04cb3ba 0 t0.txt [git@osoci luke]$ git commit -m "m4" [master 5802979] m4
  • 80. merge: review merge commit Diff Combined Format: review merge and conflict resolution
  • 81. merge: misc tools ● git merge-base c1 c2 ○ what is the best base commit to merge commit c1 and c2? ● git rev-list c1 ○ list all the commits which could be reached from commit/branch c1 reverse chronological order ● git branch --merged ○ what branches’ changed have been merged into current branch so they could be deleted safely? ● man gitrevisions ○ how to specify a range of commits? ● git merge [--squash] [--no-commit] [--no-ff] [--abort] ○ control the form of merged commit
  • 82. merge: how it works index working tree MERGE_HEAD ORIG_HEAD MERGE_MSG SQUASH_MSG HEAD New HEAD c0 c1 c2 c3 base dst src merged checkout-index -a (with potential conflicts) new commit old commit
  • 83. rebase:clean local history c0 c1 c2 c3 c4 master t0 t1 t2 t3 test t0’ t1’ test [git@osoci rebase]$ git init [git@osoci rebase]$ git simple-commit c0 c1 c2 c3 c4 [git@osoci rebase]$ git checkout -b test HEAD~3 [git@osoci rebase]$ git simple-commit t0 t1 t2 t3 [git@osoci rebase]$ git rebase -i c1 pick 688cf6d c1 squash 1320ec6 t0 squash cc7cfe6 t1 squash 43f6b13 t2 pick ddc6988 t3 …….. Stopped at ddc69887c16ce9a904f453b3ff793338b44e6092... t3 You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue [git@osoci rebase]$ git rebase --continue Successfully rebased and updated refs/heads/test. [git@osoci rebase]$ git log --oneline 32eb2e5 t3 7cac48a t0 688cf6d c1 b9a4e13 c0
  • 84. rebase:sync with upstream c0 c1 c2 c3 c4 master t0’ t1’ [git@osoci rebase]$ git rebase master First, rewinding head to replay your work on top of it... Applying: t0 Applying: t3 [git@osoci rebase]$ git log --oneline 305209e t3 f3e3986 t0 32ffe2d c4 7e363b9 c3 f16644f c2 688cf6d c1 b9a4e13 c0 t0” t1”test
  • 85. merge --squash : sync with upstream without merge commit c0 c1 c2 c3 c4 master t0 t1 test [git@osoci rebase]$ git init Initialized empty Git repository in /tmp/rebase/.git/ [git@osoci rebase]$ git simple-commit c0 c1 c2 c3 c4 [git@osoci rebase]$ git checkout -b test HEAD~3 [git@osoci rebase]$ git simple-commit t0 t1 [git@osoci rebase]$ git merge --squash master Squash commit -- not updating HEAD Automatic merge went well; stopped before committing as requested [git@osoci rebase]$ git log --oneline 1a87223 t1 5f7fe10 t0 7770250 c1 3b01ffa c0 [git@osoci rebase]$ ls c0.txt c1.txt c2.txt c3.txt c4.txt t0.txt t1.txt merge --squash master
  • 86. summary: merge cases ● for every merge, find out the correct “base/src/dst”; ● many advanced git commands are just special cases of git merge, where “base/src/dst” are selected in specified way 1. git revert 2. git cherry-pick 3. git rebase
  • 87. search: branch tag ● reference: http://lukeluo.blogspot.com/2014/06/git-as-i-understand-6- search.html ● https://github.com/torvalds/linux.git ● https://github.com/git/git.git [git@osoci git]$ git branch -avv maint bb3e7b1 Git 2.4.6 * master 6f9504c RelNotes: am.threeWay does not exist (yet) next 8c70279 Sync with master pu 20f0f97 Merge branch 'mh/tempfile' into pu todo 163e88a What's cooking (2015/07 #06) [git@osoci git]$ git branch --list -vv m* maint bb3e7b1 Git 2.4.6 * master 6f9504c RelNotes: am.threeWay does not exist (yet) [git@osoci git]$ git show master [git@osoci git]$ git tag -l gitgui-0.10.0 ….. v2.5.0-rc2 v2.5.0-rc3 [git@osoci git]$ git tag -l v2.5* v2.5.0-rc0 v2.5.0-rc1 v2.5.0-rc2 v2.5.0-rc3 [git@osoci git]$ git show v2.4.6 tag v2.4.6 Tagger: Junio C Hamano <gitster@pobox.com> ……….
  • 88. search: branch/tag with commit [git@osoci git]$ git branch -l -vv --contains v2.4.5 maint bb3e7b1 Git 2.4.6 * master 6f9504c RelNotes: am.threeWay does not exist (yet) next 8c70279 Sync with master pu 20f0f97 Merge branch 'mh/tempfile' into pu [git@osoci git]$ git tag -l --contains 0df0541bf1 v2.4.5 v2.4.6 v2.5.0-rc0 v2.5.0-rc1 v2.5.0-rc2 v2.5.0-rc3
  • 89. search: git browser “tig”
  • 90. search: grep file content [git@osoci git]$ git grep -i rerere v2.4.6 -- Documentation/config.txt v2.4.6:Documentation/config.txt:gc.rerereResolved:: v2.4.6:Documentation/config.txt: kept for this many days when 'git rerere gc' is run. v2.4.6:Documentation/config.txt: The default is 60 days. See linkgit:git-rerere[1]. v2.4.6:Documentation/config.txt:gc.rerereUnresolved:: v2.4.6:Documentation/config.txt: kept for this many days when 'git rerere gc' is run. v2.4.6:Documentation/config.txt: The default is 15 days. See linkgit:git-rerere[1]. v2.4.6:Documentation/config.txt:rerere.autoUpdate:: v2.4.6:Documentation/config.txt: When set to true, `git-rerere` updates the index with the …..
  • 91. search: commits git rev-list: DAG traversal and commits set operation c0 c1 c2 c3 c4 master t0 t1 test rev-list c4 = {c0,c1,c2,c3,c4} -- branch master rev-list t1 = {t1,t0,c1,c0} -- branch test rev-list c2 t0 = {t0,c0,c1,c2} rev-list t1 ^c4 = { t0,t1} rev-list c4..t1 = rev-list t1 ^c4 rev-list t1...c3 = {c1,c2,c3,t0,t1} rev-list ${git merge-base --all c4 t1} = {c0,c1}
  • 92. search: commit meta-info git log --author=*** --committer=*** --since=2013-04-05 --until=2014-05-07 got log --no-merges git log --merges git log -i --grep “mac book” (search commits which contains “mac book” in commit message) [git@osoci linux]$ git log -i --grep "mac book" commit bcd2623073e98f69f84720308db0b142c4da0bd6 Author: Tom Gundersen <teg@jklm.no> Date: Thu Oct 31 00:38:30 2013 -0700 Input: allow deselecting serio drivers even without CONFIG_EXPERT There is plenty of consumer hardware (e.g., mac books) that does not use AT keyboards or PS/2 mice. It therefore makes sense for distro kernels to build the related drivers as modules to avoid loading them on hardware that does not need them. As such, these options should no longer be protected by EXPERT. Moreover, building these drivers as modules gets rid of the following ugly error during boot:
  • 93. search: commit diff git log -- bug.txt: which commits change “bug.txt” ?
  • 94. search: commit diff (pickaxe) git log -S “bug*” --pickaxe-regex : which commits change the time of appearance of “bug”, added or deleted git log -G “bug*” : which commits add/delete lines contains “bugs*” Under the Hood: git diff -S<string> git diff -S<regex> --pickaxe-regex git diff -G<regex>
  • 95. search: commit diff blame [luke@rmbp blame]$ cat blame.txt a b c d [luke@rmbp blame]$ git blame blame.txt ^0a17a1e (Luke Luo 2014-06-16 22:44:45 +0800 1) a 4991d567 (Luke Luo 2014-06-16 22:44:57 +0800 2) b bf0599d2 (Luke Luo 2014-06-16 22:45:08 +0800 3) c d76ddb98 (Luke Luo 2014-06-16 22:45:24 +0800 4) d git blame blame.txt: for every line in blame.txt, find out the latest commit which add this line. It tells you why this line appears. git blame --reverse master~7..master -- blame.txt for every line in blame.txt, given a start commit (master~7), looking forward find out the first commit which delete this line. It tells you why this line disappears. [luke@rmbp blame]$ git blame --reverse master~7..master -- blame.txt ^805836b (Luke Luo 2014-06-17 08:56:31 +0800 1) a 314a3c6e (Luke Luo 2014-06-17 08:56:47 +0800 2) b 506a3641 (Luke Luo 2014-06-17 08:56:55 +0800 3) c bafa1476 (Luke Luo 2014-06-17 08:57:06 +0800 4) d a95b8daa (Luke Luo 2014-06-17 08:57:19 +0800 5) e
  • 96. search: blame via git log git log -L <start>,<end>:<file>, -L :<funcname>:<file> this is another form of “git blame”
  • 97. Collaboration: namespace Reference: local namespace Object: global namespace (hashid) e34f 20a3 a5f9 fe2a 119a 3748 [git@osoci git]$ git branch -a maint master * pu remotes/origin/HEAD -> origin/master remotes/origin/maint remotes/origin/master remotes/origin/next remotes/origin/pu remotes/origin/todo [git@osoci git]$ git branch -a maint master * pu remotes/origin/HEAD -> origin/master remotes/origin/maint remotes/origin/master remotes/origin/next remotes/origin/pu remotes/origin/todo global unique hashid
  • 98. Collaboration: transfer protocol ● ssh://[user@]host.xz[:port]/path/to/repo.git/ ● git://host.xz[:port]/path/to/repo.git/ ● http[s]://host.xz[:port]/path/to/repo.git/ ● ftp[s]://host.xz[:port]/path/to/repo.git/ ● rsync://host.xz/path/to/repo.git/
  • 99. Collaboration: connect examples ● Please connect to git repos in machine 192.168.88.35 and machine 192.168.88.12 via ssh and pub/private keys, using your git bash in windows ● Please connect to above two machines via Sourcetree in windows ● Please register a git account in “http://git.oschina.net/” and connect to it
  • 100. Collaboration: refspec ● exchange of information among git repos happen in the unit of BRANCH ● refspec: +<source brances>:<dest brances> git remote add origin https://git@github.com:mary/example-repo.git git ls-remote origin git remote show [remote "origin"] url = https://git@github.com:mary/example-repo.git fetch = +refs/heads/*:refs/remotes/origin/* [remote "origin"] url = https://git@github.com:mary/example-repo.git fetch = +refs/heads/master:refs/remotes/origin/master [remote "origin"] url = https://git@github.com:mary/example-repo.git fetch = +refs/heads/master:refs/remotes/origin/master push = refs/heads/master:refs/heads/qa-master
  • 101. Collaboration: sync examples ● Specify the remote branch: $ git push origin master:refs/heads/qa-master ● Remove a remote branch: $ git push origin :some-feature $ git push origin --delete some-feature ● fetch from remote branch $ git fetch origin master:refs/integration/qa
  • 102. Collaboration: tracking branch ● create local tracking branch: git branch -u origin/master master git checkout -b master origin/master ● cat .git/config [branch "master"] remote = origin merge = refs/heads/master
  • 103. Collaboration: tracking branch local tracking branch: ------master------- remote tracking branch: ------origin/master------- Local Repo ------master------- Remote Repo fetch track merge push pull
  • 104. Tracking branch: A metaphor local tracking branch: ------master------- remote tracking branch: ------origin/master------- Local Repo ------master------- Remote Repo fetch track merge push pull
  • 105. Collaboration: push.default ● A complete push is like this: ○ git push origin master:master ● If you omit “ refspec” during git push, like ○ git push What would happen? ● cat .git/config [branch "master"] remote = origin merge = refs/heads/master It depends on how “push.default” is configured: git config --global push.default “xxx” ● nothing ○ do nothing. you must provide a ref spec ● current ○ push origin current_branch:current_branch ● upstream push origin current_branch:@{upstream} ● simple push origin current_branch:@{upstream} here @{upstream} must have the same name with current branch ● matching push all local branches to remote with
  • 106. Collaboration: pull-request [luke@rmbp x]$ git request-pull -p HEAD~4 file:///tmp/x HEAD The following changes since commit 61d4c4ff68d940906074fe6684687edf0e7180aa: m1 (2015-07-29 13:31:20 +0800) are available in the git repository at: file:///tmp/x HEAD for you to fetch changes up to 6e790570a168534e123c362b05db8055b53ec591: t3 (2015-07-29 13:33:17 +0800) ---------------------------------------------------------------- Luke Luo (4): t0 t1 t2 t3 t0.txt | 1 + t1.txt | 1 + t2.txt | 1 + t3.txt | 1 + 4 files changed, 4 insertions(+) …………..
  • 107. Remote Repo: Demo # modify “./ssh/config” to create entry for “osoci” Host osoci HostName 192.168.88.28 Port 22 User git IdentityFile ~/.ssh/topdemo.pem # login remote machine and create a “bare” remote repo[luke@rmbp ~]$ cd localrepo/ [luke@rmbp ~]$ ssh osoci Last login: Wed Aug 19 00:08:58 2015 from 222.129.52.83 [git@osoci ~]$ git init --bare /tmp/luke.git Initialized empty Git repository in /tmp/luke.git/ [git@osoci ~]$ exit logout # create a local repo [luke@rmbp ~]$ git init localrepo Initialized empty Git repository in /home/luke/localrepo/.git/ # create a remote for local repo pointing to remote repo [luke@rmbp ~]$ cd localrepo/ [luke@rmbp localrepo]$ git remote add osoci ssh://osoci/tmp/luke.git [luke@rmbp localrepo]$ git remote -v osoci ssh://osoci/tmp/luke.git (fetch) osoci ssh://osoci/tmp/luke.git (push) [luke@rmbp localrepo]$ cat .git/config ………. [remote "osoci"] url = ssh://osocipub/tmp/luke.git fetch = +refs/heads/*:refs/remotes/osoci/* # generate some commits in local repo “master” [luke@rmbp localrepo]$ git simple-commit m0 m1 m2 # configuer “push.default” to “simple” [luke@rmbp localrepo]$ git config --global push.default simple # push local “master” to remote repo [luke@rmbp localrepo]$ git push osoci master:master Counting objects: 9, done. Delta compression using up to 8 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (9/9), 626 bytes | 0 bytes/s, done. Total 9 (delta 1), reused 0 (delta 0) To ssh://osocipub/tmp/luke.git * [new branch] master -> master # a “remote tracking branch is created automatically [luke@rmbp localrepo]$ git branch -avv * master 3caa841 m2 remotes/osoci/master 3caa841 m2
  • 108. Remote Repo: Demo # checkout an “orphaned” branch “test” [luke@rmbp localrepo]$ git checkout --orphan test [luke@rmbp localrepo]$ git simple-commit t0 t1 t2 [luke@rmbp localrepo]$ git log --oneline --decorate --graph --all * 61810bd (HEAD -> test) t2 * ce5fe1a t1 * 8970a60 t0 * 3caa841 (osoci/master, master) m2 * 0501ae6 m1 * 156e941 m0 # push “test” to remote repo, and create a “remote tracking branch” [luke@rmbp localrepo]$ git push -u osoci test [luke@rmbp localrepo]$ git branch -avv master 3caa841 m2 * test 61810bd [osoci/test] t2 remotes/osoci/master 3caa841 m2 remotes/osoci/test 61810bd t2 # set up “master” ‘s remote tracking branch [luke@rmbp localrepo]$ git branch -u remotes/osoci/master master Branch master set up to track remote branch master from osoci. [luke@rmbp localrepo]$ git branch -avv * master 3caa841 [osoci/master] m2 test 61810bd [osoci/test] t2 remotes/osoci/master 3caa841 m2 remotes/osoci/test 61810bd t2 # create another local repo via “clone” [luke@rmbp ~]$ git clone ssh://osocipub/tmp/luke.git clone-repo [luke@rmbp ~]$ ls clone-repo m0.txt m1.txt m2.txt [luke@rmbp ~]$ cd clone-repo/ [luke@rmbp clone-repo]$ git branch -avv * master 3caa841 [origin/master] m2 remotes/origin/HEAD -> origin/master remotes/origin/master 3caa841 m2 remotes/origin/test 61810bd t2 # make commits in “master” and push to remote repo [luke@rmbp clone-repo]$ git simple-commit m3 m4 [luke@rmbp clone-repo]$ git push origin …….. To ssh://osocipub/tmp/luke.git 3caa841..12ae515 master -> master # fetch remote update from repo “localrepo” [luke@rmbp clone-repo]$ cd ../localrepo/ [luke@rmbp localrepo]$ git fetch osoci ………... From ssh://osocipub/tmp/luke 3caa841..12ae515 master -> osoci/master [luke@rmbp localrepo]$ git branch -avv * master 3caa841 [osoci/master: behind 2] m2 test 61810bd [osoci/test] t2 remotes/osoci/master 12ae515 m4
  • 109. Remote Repo: Demo # merge change from remote “master” into local “master” [luke@rmbp localrepo]$ git merge remotes/osoci/master Updating 3caa841..12ae515 Fast-forward m3.txt | 1 + m4.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 m3.txt create mode 100644 m4.txt [luke@rmbp localrepo]$ git branch -avv * master 12ae515 [osoci/master] m4 test 61810bd [osoci/test] t2 remotes/osoci/master 12ae515 m4 remotes/osoci/test 61810bd t2 # make more chang in local “master” and push it to remote repo [luke@rmbp localrepo]$ git simple-commit m5 m6 m7 [luke@rmbp localrepo]$ git push osoci Counting objects: 9, done. Delta compression using up to 8 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (9/9), 654 bytes | 0 bytes/s, done. Total 9 (delta 3), reused 0 (delta 0) To ssh://osocipub/tmp/luke.git 12ae515..47cadbc master -> master [luke@rmbp localrepo]$ git branch -avv * master 47cadbc [osoci/master] m7 test 61810bd [osoci/test] t2 remotes/osoci/master 47cadbc m7 remotes/osoci/test 61810bd t2
  • 111. Workflow: Ad-hoc Peer to Peer Tom Ceph Mary Linda fetch tom master:refs/remote/tom/master push linda master:refs/remote/linda/feature
  • 116. Project Dependency: internal software repository server external software repository server external project external project publish sync internal project internal project git git CI server release internal project build build dependency build dependency subtree/ submodule
  • 117. Project Dependency: Dependency Level : ● Level I : external public project ● Level II: internal friend project (different release cycle) ● Level III: internal projects which share part of code base (in different git repositories) Solutions: ● Level I : package manager (yum/pacman/apt-get, maven, ivy,....) ● Level II: Internal package manager (yum/pacman/apt-get, maven, ivy,....) ● Level III: Git submodule/subtree
  • 119. Project Dependency: manual Subtree ● fetch subproject from remote repo: ○ git fetch remote-A master:proj-A/master ● read subproject tree into main project: ○ git checkout master ○ git read-tree --prefix=project-A proj-A/master@{tree} ○ git commit -m “add subproject A” ● fetch update for project A from remote repo: ○ git fetch remote-A master:proj-A/master ○ git checkout master ○ git merge -s subtree --prefix=project-A --squash --no-commit proj-A/master ○ git commit -m “update subproject A”
  • 120. Project Dependency: git Subtree man git-subtree: GIT-SUBTREE(1) Git Manual GIT-SUBTREE(1) NAME git-subtree - Merge subtrees together and split repository into subtrees SYNOPSIS git subtree add -P <prefix> <commit> git subtree add -P <prefix> <repository> <ref> git subtree pull -P <prefix> <repository> <ref> git subtree push -P <prefix> <repository> <ref> git subtree merge -P <prefix> <commit> git subtree split -P <prefix> [OPTIONS] [<commit>]