Escolar Documentos
Profissional Documentos
Cultura Documentos
Advanced Git
Luc Sarzyniec
http://127.0.0.1:4000/advanced-git/#1
1 1/86
/ 86
17/02/2019 Advanced Git
About
This slides are using resources from the Pro Git book [isbn:1430218339] which is licensed
under the Creative Commons v3.0 (by-nc-sa) license.
http://127.0.0.1:4000/advanced-git/#1
2 2/86
/ 86
17/02/2019 Advanced Git
Summary
1. Overview
2. Basic usage
3. Work with branches
4. Rewrite history
5. Code introspection
6. Useful commands
7. Internals
http://127.0.0.1:4000/advanced-git/#1
3 3/86
/ 86
17/02/2019 Advanced Git
Overview
Originally developed to work on the GNU/Linux kernel
First release in 2005 (1 year after subversion 1.0)
Free software (GPLv2)
Main goals
Speed
Simple design
Strong support for non-linear development (thousands of branches)
Fully distributed
Ability to handle large projects like the Linux kernel efficiently (speed and data
size)
http://127.0.0.1:4000/advanced-git/#1
4 4/86
/ 86
17/02/2019 Advanced Git
Finding documentation
Read the manual
Well written
A lot of examples
Very complete
Easy to read/understand
Available in different formats
Other books
http://127.0.0.1:4000/advanced-git/#1
5 5/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
6 6/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
7 7/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
8 8/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
9 9/86
/ 86
17/02/2019 Advanced Git
Git basics
[Pro Git, chapter 2]
http://127.0.0.1:4000/advanced-git/#1
1010/86
/ 86
17/02/2019 Advanced Git
Start to work
1. Create an empty (sandbox) repository
$ cd /tmp/sandbox
$ git checkout -b master
http://127.0.0.1:4000/advanced-git/#1
1111/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
1212/86
/ 86
17/02/2019 Advanced Git
$ git status
Changes to be committed:
new file: staged_file
deleted: file
http://127.0.0.1:4000/advanced-git/#1
1313/86
/ 86
17/02/2019 Advanced Git
State of les
http://127.0.0.1:4000/advanced-git/#1
1414/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
1515/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
1616/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
1717/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
1818/86
/ 86
17/02/2019 Advanced Git
$ git status -s
A file
M modified_file
D removed_file
?? untracked_file
http://127.0.0.1:4000/advanced-git/#1
1919/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
2020/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
2121/86
/ 86
17/02/2019 Advanced Git
Commit changes
Commit and specify message on the CLI
Rewrite (amend) the last commit (staged files will be added in the commit)
http://127.0.0.1:4000/advanced-git/#1
2222/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
2323/86
/ 86
17/02/2019 Advanced Git
$ git diff
http://127.0.0.1:4000/advanced-git/#1
2424/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
2525/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
2626/86
/ 86
17/02/2019 Advanced Git
$ git shortlog
http://127.0.0.1:4000/advanced-git/#1
2727/86
/ 86
17/02/2019 Advanced Git
Specifying revisions
The previous commit: HEAD^, HEAD~, HEAD^1
The previous commit of the develop branch: develop~1 or develop^1
Two commit before fa616be: fa616be~2 or fa616be^^
Three commit before this commit: HEAD~3 or HEAD^^^
G H I J A = = A^0
\ / \ / B = A^ = A^1 = A~1
D E F C = A^2 = A^2
\ | / \ D = A^^ = A^1^1 = A~2
\ | / | E = B^2 = A^^2
\|/ | F = B^3 = A^^3
B C G = A^^^ = A^1^1^1 = A~3
\ / H = D^2 = B^^2 = A^^^2 = A~2^2
\ / I = F^ = B^3^ = A^^3^
A J = F^2 = B^3^2 = A^^3^2
http://127.0.0.1:4000/advanced-git/#1
2828/86
/ 86
17/02/2019 Advanced Git
Work in team
[Pro Git, chapter 2.5 and chapter 5.2]
http://127.0.0.1:4000/advanced-git/#1
2929/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
3030/86
/ 86
17/02/2019 Advanced Git
Classic state
C1 C2 C3
uri:///project.git/refs/heads/master ----*----*----*
(remote,read-write)
C1 C2
refs/remotes/origin/master --------------*----*
(local,read-only)
C1
refs/heads/master -----------------------*
(local,read-write)
http://127.0.0.1:4000/advanced-git/#1
3131/86
/ 86
17/02/2019 Advanced Git
C1 C2 C3
uri:///project.git/refs/heads/master ----*----*----*
(remote,read-write) | update
C1 C2 v
refs/remotes/origin/master --------------*----*====*
(local,read-only) C3
C1
refs/heads/master -----------------------*
(local,read-write)
C1 C2 C3
uri:///project.git/refs/heads/master ----*----*----*
(remote,read-write) | update
C1 C2 v
refs/remotes/origin/master --------------*----*====*
(local,read-only) | merge
C1 v
refs/heads/master -----------------------*====*====*
(local,read-write) C2 C3
http://127.0.0.1:4000/advanced-git/#1
3232/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
3333/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
3434/86
/ 86
17/02/2019 Advanced Git
Working in branches
$ git branch testing
http://127.0.0.1:4000/advanced-git/#1
3535/86
/ 86
17/02/2019 Advanced Git
Working in branches
$ git checkout testing
http://127.0.0.1:4000/advanced-git/#1
3636/86
/ 86
17/02/2019 Advanced Git
Working in branches
$ git add ... && git commit ... # in testing
http://127.0.0.1:4000/advanced-git/#1
3737/86
/ 86
17/02/2019 Advanced Git
Working in branches
$ git checkout master
http://127.0.0.1:4000/advanced-git/#1
3838/86
/ 86
17/02/2019 Advanced Git
Working in branches
$ git add ... && git commit ... # in master
http://127.0.0.1:4000/advanced-git/#1
3939/86
/ 86
17/02/2019 Advanced Git
$ git reflog
http://127.0.0.1:4000/advanced-git/#1
4040/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4141/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4242/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4343/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4444/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4545/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4646/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4747/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4848/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
4949/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
5050/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
5151/86
/ 86
17/02/2019 Advanced Git
commit4
(cherry picked from commit db3e256ed4a23c92077aa2f136edab95970e8597)
http://127.0.0.1:4000/advanced-git/#1
5252/86
/ 86
17/02/2019 Advanced Git
Rewrite history
[Pro Git, chapter 7.6]
http://127.0.0.1:4000/advanced-git/#1
5353/86
/ 86
17/02/2019 Advanced Git
Rewrite history
Rewrite (amend) the last commit
http://127.0.0.1:4000/advanced-git/#1
5454/86
/ 86
17/02/2019 Advanced Git
Rewrite history
Rewrite commit messages only
Re-order commits
Delete commits
http://127.0.0.1:4000/advanced-git/#1
5555/86
/ 86
17/02/2019 Advanced Git
Rewrite history
Edit several commits
http://127.0.0.1:4000/advanced-git/#1
5656/86
/ 86
17/02/2019 Advanced Git
# edit files
$ git add ... # git rm ...
$ git commit --amend
http://127.0.0.1:4000/advanced-git/#1
5757/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
5858/86
/ 86
17/02/2019 Advanced Git
Rewrite history
Mix commits
http://127.0.0.1:4000/advanced-git/#1
5959/86
/ 86
17/02/2019 Advanced Git
Mix commits
Select the commits to be mixed (with the previous commit)
http://127.0.0.1:4000/advanced-git/#1
6060/86
/ 86
17/02/2019 Advanced Git
Rewrite history
Insert new commits
http://127.0.0.1:4000/advanced-git/#1
6161/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
6262/86
/ 86
17/02/2019 Advanced Git
Rewrite history
Split commits
http://127.0.0.1:4000/advanced-git/#1
6363/86
/ 86
17/02/2019 Advanced Git
Split commits
Select the commits to split
http://127.0.0.1:4000/advanced-git/#1
6464/86
/ 86
17/02/2019 Advanced Git
Split commits
Continue once it's done
http://127.0.0.1:4000/advanced-git/#1
6565/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
6666/86
/ 86
17/02/2019 Advanced Git
Debugging
[Pro Git, chapter 7.10]
http://127.0.0.1:4000/advanced-git/#1
6767/86
/ 86
17/02/2019 Advanced Git
Code introspection
Read the code annotated with commit/line
http://127.0.0.1:4000/advanced-git/#1
6868/86
/ 86
17/02/2019 Advanced Git
Code introspection
In short format
http://127.0.0.1:4000/advanced-git/#1
6969/86
/ 86
17/02/2019 Advanced Git
Code introspection
See where sections of code originally came from
http://127.0.0.1:4000/advanced-git/#1
7070/86
/ 86
17/02/2019 Advanced Git
# Do some tests
$ git bisect good
Bisecting: 75 revisions left to test after this (roughly 6 steps)
# Do some tests
$ git bisect bad
Bisecting: 37 revisions left to test after this (roughly 5 steps)
# ...
Find the version that introduced the bug (-> read the diff to understand)
# ...
bcbdeb1a1256f777e52192fa7da0f7dbad680162 is the first bad commit
http://127.0.0.1:4000/advanced-git/#1
7171/86
/ 86
17/02/2019 Advanced Git
rake test # ?
[See http://lwn.net/Articles/317154/]
http://127.0.0.1:4000/advanced-git/#1
7272/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
7373/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
7474/86
/ 86
17/02/2019 Advanced Git
$ git mergetool
Share changes saving commits in a bundle file (can be sent by mail, ...)
http://127.0.0.1:4000/advanced-git/#1
7575/86
/ 86
17/02/2019 Advanced Git
Memo
$ git add -p
$ git checkout -- FILE
$ git reset REV # --soft/--hard
$ git stash
$ git commit --amend
$ git diff REV -- FILE
$ git diff --color-words
$ git show REV:FILE
$ git log --decorate --graph
$ git fetch origin BRANCH:OTHER_BRANCH
$ git revert REV
$ git rebase -i REV
$ git cherry-pick -x REV
$ git blame FILE
$ git bisect REV_END REV_START
$ git grep STR REV
$ git clean
$ git archive -o FILE.tar.gz REV
http://127.0.0.1:4000/advanced-git/#1
7676/86
/ 86
17/02/2019 Advanced Git
Internals
[Pro Git, chapter 10]
http://127.0.0.1:4000/advanced-git/#1
7777/86
/ 86
17/02/2019 Advanced Git
http://127.0.0.1:4000/advanced-git/#1
7878/86
/ 86
17/02/2019 Advanced Git
$ find .git/objects/
.git/objects/6d/
.git/objects/6d/4ed2c98c4fbe835280634af0cbddefffaf7ee6
http://127.0.0.1:4000/advanced-git/#1
7979/86
/ 86
17/02/2019 Advanced Git
$ cat .git/objects/6d/4ed2c98c4fbe835280634af0cbddefffaf7ee6
xKÊÉOR°dp,O-ÎÏMUä,S
$ cat .git/objects/6d/4ed2c98c4fbe835280634af0cbddefffaf7ee6 | \
openssl zlib -d | od -vtc -tx1
0000000 b l o b 9 \0 A w e s o m e ! \n
62 6c 6f 62 20 39 00 41 77 65 73 6f 6d 65 21 0a
http://127.0.0.1:4000/advanced-git/#1
8080/86
/ 86
17/02/2019 Advanced Git
Commit message
http://127.0.0.1:4000/advanced-git/#1
8181/86
/ 86
17/02/2019 Advanced Git
A commit
http://127.0.0.1:4000/advanced-git/#1
8282/86
/ 86
17/02/2019 Advanced Git
History
http://127.0.0.1:4000/advanced-git/#1
8383/86
/ 86
17/02/2019 Advanced Git
Branches
http://127.0.0.1:4000/advanced-git/#1
8484/86
/ 86
17/02/2019 Advanced Git
Branches
Branch = pointer on a commit object
$ cat .git/refs/heads/master
7f4ba4b6e3ba7075ca6b379ba23fd3088cbe69a8
$ cat .git/HEAD
ref: refs/heads/master
Create a branch
http://127.0.0.1:4000/advanced-git/#1
8585/86
/ 86
17/02/2019 Advanced Git
Thank you !
http://127.0.0.1:4000/advanced-git/#1
8686/86
/ 86