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
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”
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
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
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
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
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
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
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
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
…..
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:
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
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
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