Git

Git

1 Git

1.1 Git

1.1.1 Blob

Blob
Git
.git/objects
git init
.git/objects
pack
info
Git
key-value data store
key
value

Git
echo 'content'
content
|
-w
--stdin
echo 'content'
content
git hash-object --stdin
content
-w
Git
.git/objects

echo 'content' | git hash-object -w --stdin d95f3ad1...

d95f3ad1...
blob
SHA-1
.git/objects
38
find
-type
f

find .git/objects -type f .git/objects/d9/5f3ad1...

d95f3ad1...
key
Git
-p
-t

git cat-file -p d95f3ad1 content

echo
Git
readme.md

git hash-object -w readme.md

1.1.2

echo 'hello world' > readme.md git hash-object -w readme.md 3b18e512...

echo 'hi git' > readme.md git hash-object -w readme.md 2984e649...

find .git/objects -type f .git/objects/29/84e649... .git/objects/3b/18e512...

git cat-file -p 3b18e512 > readme.md cat readme.md hello world

git cat-file -p 2984e649 > readme.md cat readme.md hi git

1.1.3 Tree

SHA-1
Tree
blob
SHA-1

master^{tree}
master
readme.md
file.txt
dist

git cat-file -p master^{tree} 100644 blob a906cb12... readme.md 100644 blob 8f941322... file.txt 040000 tree 99f1a699... dist

git cat-file -p 99f1a699 100644 blob 47c63436... index.html

readme.md --> blob / tree file.txt --> blob / dist --> tree index.html --> blob

Git
--add
--cacheinfo
SHA-1
SHA-1
3b18e512...
100644
readme.md

git update-index --add --cacheinfo 100644/ 3b18e512... readme.md

git ls-files -s 100644 3b18e512... 0 readme.md

7394b8cc...
SHA-1

git write-tree 7394b8cc...

git cat-file -p 7394b8cc 100644 blob 3b18e512... readme.md find .git/objects -type f .git/objects/73/94b8cc...

1.1.4

git update-index --add --cacheinfo 100644/ 2984e649... readme.md

Git
SHA-1

echo 'file' > file.txt git update-index --add file.txt

readme.md
file.txt

git write-tree e331c9c2...

git ls-files -s 100644 f73f3093... 0 file.txt 100644 2984e649... 0 readme.md

7394b8cc...
e331c9c2...
--prefix
dist

git read-tree --prefix=dist 7394b8cc

git write-tree ccf028c6...

git cat-file -p ccf028c6 040000 tree 7394b8cc... dist 100644 blob f73f3093... file.txt 100644 blob 2984e649... readme.md

1.1.5

7394b8cc...
e331c9c2...
ccf028c6...
SHA-1

'A'
7394b8cc

echo 'A' | git commit-tree 7394b8cc f0dcf2c3...

f0dcf2c3

git cat-file -p f0dcf2c3 tree 7394b8cc... author ... committer ... A

e331c9c2
-p
f0dcf2c3

echo 'B' | git commit-tree e331c9c2 -p f0dcf2c3 5d3d89ce... echo 'C' | git commit-tree ccf028c6 -p 5d3d89ce b41c0107...

5d3d89ce

git cat-file -p 5d3d89ce tree e331c9c2... parent f0dcf2c3... author ... committer ... B

Git
git log

readme.md (2984e649) --> blob (hi git) ------------------------------------| / | C (b41c0107) --> tree (ccf028c6) dist --> tree (7394b8cc) readme.md (3b18e512) --> blob (hello world) --|--| | / | | | file.txt (f73f3093) --> blob (file) ------------------------------------| | | | | | | B (5d3d89ce) --> tree (e331c9c2) readme.md (2984e649) --> blob (hi git) --------------------------------|--| | | / | | | file.txt (f73f3093) --> blob (file) ------------------------------------| | | | A (f0dcf2c3) --> tree (7394b8cc) readme.md (3b18e512) --> blob (hello world) ---------------------------------|

1.1.6

Git
Git
commit
tree
blob

  • blob
    content
  • content
    SHA-1
    40
  • 38
  • content
    lib

1.2 Git

git log b41c0107
b41c0107
C
.git/refs

1.2.1

.git/refs/heads
Git
update-ref
b41c0107
.git/refs/heads
master

git update-ref refs/heads/master b41c0107

master

cat .git/refs/heads/master b41c0107...

1.2.2 HEAD

HEAD

git branch dev
dev
dev
HEAD

dev
Git
HEAD
.git/refs/heads
dev

git branch dev cat .git/refs/heads/dev b41c0107...

HEAD

cat .git/HEAD ref: refs/heads/master

HEAD
dev

git symbolic-ref HEAD refs/heads/dev

1.2.3

v1.8.5

git update-ref refs/tags/v1.8.5 HEAD

v1.8.5
v1.8.5
b41c0107

cat .git/refs/tags/v1.8.5 b41c0107...

v1.8.6

git tag -a v1.8.6 HEAD -m 'message'

4740ea0b

cat .git/refs/tags/v1.8.6 4740ea0b...

4740ea0b
object

git cat-file -p 4740ea0b object b41c0107... type commit tag v1.8.6 tagger ... message

1.2.4

git checkout
Git
HEAD

refs/remotes

repo
dev
master
origin

git remote add origin https://github.com/username/repo.git git fetch origin ... From https://github.com/username/repo * [new branch] dev -> origin/dev * [new branch] master -> origin/master

.git/refs/remotes/origin

find .git/refs/remotes/origin -type f .git/refs/remotes/origin/dev .git/refs/remotes/origin/master

master
90edc78e
master

cat .git/refs/remotes/origin/master 90edc78e...

1.3

Git
Git
blob
loose

Git
git gc
Git

git gc

1.3.1

repo
10kb
readme.md
readme.me
10311

git add readme.md git ls-files -s 100644 767466fd... 0 readme.md git cat-file -s 767466fd 10311 git commit -m 'A'

readme.md
10319

echo 'message' >> readme.md git add readme.md git ls-files -s 100644 f08cfd3e... 0 readme.md git cat-file -s f08cfd3e 10319 git commit -m 'B'

.git/objects
6

find .git/objects -type f .git/objects/76/7466fd... .git/objects/f0/8cfd3e... ...

git gc
.git/objects
.idx
.pack
Git

Git

git gc .git/objects/info/packs .git/objects/pack/pack-3575...a060.idx .git/objects/pack/pack-3575...a060.pack

6
SHA-1
SHA-1

readme.md
f08cfd3e
767466fd
Git

git verify-pack -v .git/objects/pack/pack-3575...a060.ind f08cfd3e... blob 10319 2561 265 767466fd... blob 7 18 2921 1 f08cfd3e... ...

1.3.2

git gc
.git/refs
.git/packed-refs

find .git/refs -type f .git/refs/heads/master .git/refs/remotes/origin/master .git/refs/tags/v1.8.5 .git/refs/tags/v1.8.6

.git/refs
.git/packed-refs
^
^

Git
packed-refs
.git/refs
Git
SHA-1
.git/refs
packed-refs

cat .git/packed-refs ceb21368... refs/heads/master 040db796... refs/remotes/origin/master ceb21368... refs/tags/v1.8.5 9dcb07d9... refs/tags/v1.8.6 ^ceb2136...

1.4

.git/config
url
<src>:<dst>
<src>
<dst>
+

git remote add origin https://github.com/username/repo.git cat .git/config ... [remote "origin"] url = https://github.com/username/repo.git fetch = +refs/heads/*:refs/remotes/origin/*

git fetch
refs/heads
refs/remotes/origin

git fetch origin ... From https://github.com/username/repo * [new branch] dev -> origin/dev * [new branch] master -> origin/master find .git/refs/remotes/origin -type f .git/refs/remotes/origin/dev .git/refs/remotes/origin/master

1.4.1

fetch
master
refs/remotes/origin/master

git log origin/master git log remotes/origin/master git log refs/remotes/origin/master

1.4.2

repo
master

fetch = +refs/heads/master:refs/remotes/origin/master

master
dev

fetch = +refs/heads/master:refs/remotes/origin/master fetch = +refs/heads/dev:refs/remotes/origin/dev

1.4.3

master
feat/master

git push origin master:feat/master

dev
feat

fetch = +refs/heads/master:refs/remotes/origin/master fetch = +refs/heads/feat/*:refs/remotes/origin/feat/*

1.4.4

repo
master
A
B
C
fetch

A
master

B
.git/config

fetch = +refs/heads/*:refs/remotes/origin/*

C
.git/config
+

fetch = refs/heads/*:refs/remotes/origin/*

B
fetch
forced update
master

git fetch origin ... + ... master -> origin/master (forced update)

C
master

git fetch origin ... ! [rejected] master -> origin/master (non-fast-forward)

1.5

1.5.1

HEAD
master

A B C D E <-- master <-- HEAD

master
C

git reset --hard HEAD^^

master
E
git reflog
E

git reflog ... 329e7f4 HEAD@{1}: commit: E

git reset --hard HEAD@{1}
master
E

E
reflog
E

master
C
.git/logs
git reflog

rm -r .git/logs

Git
--full
dangling commit
E

git fsck --full ... dangling commit 329e7f4...

1.5.2

Git

git count-objects -v count: 3 size: 4 in-pack: 6 packs: 1 size-pack: 132 prune-packable: 2 garbage: 0 size-garbage: 0

  • count
  • size
    KiB
  • in-pack
    .git/objects/pack
    .idx
  • packs
    .git/objects/pack
    .pack
  • size-pack
    KiB
  • prune-packable
    git prune-packed
    git prune-packed -n
  • garbage
  • size-garbage
    KiB

1.5.3

Git
Git
git clone

git pull --rebase

git gc
Git
.git/objects/pack

git gc find .git/objects/pack -type f .git/objects/pack/pack-3aa8...a09e.idx .git/objects/pack/pack-3aa8...a09e.pack

sort
-n
-k 3
tail -3
3

git verify-pack -v .git/objects/pack/pack-3aa8...a09e.idx/ | sort -n -k 3/ | tail -3 bc65c0d9... commit 206 141 437 c4a96707... commit 206 140 1145 072e320f... blob 7407405 6594320 1978

072e320f
7M
git rev-list --objects --all
Git
grep
072e320f
file.zip
7M

git rev-list --objects --all | grep 072e320f 072e320f... file.zip

--branches

git log --oneline --branches -- file.zip 9db12cc (HEAD -> master) remove file.zip 52bd5ce add file.zip

52bd5ce^..HEAD
52bd5ce

git filter-branch -f --prune-empty --index-filter 'git rm -f --cached --ignore-unmatch file.zip' 52bd5ce^..HEAD

  • rm -rf
    .git/refs/original
rm -rf .git/refs/original/ git reflog expire --expire=now --all git fsck --full --unreachable git repack -A -d git gc --aggressive --prune=now

2 Gitk

Gitk
Git

Git
gitk
A
B
C
D

2.1

2.1.1 File

  • Update
    gitk
  • Reload
  • Reread references
  • List references
    Filter
    *
    *master
    master
    remotes/origin/master

  • Start git gui
    git gui
  • Quit

2.1.2 Edit

Preferences
C
.git/config

[gui] encoding = utf-8

  • Maximum graph width (lines)
    ( )
  • Maximum graph width (% of pane)
    ( )
  • Show local changes
  • Auto-select SHA1 (length)
    SHA-1
    ( )
  • Hide remote refs
  • Tab spacing
  • Display nearby tags/heads
  • Maximum # tags/heads to show
  • Limit diffs to listed paths
  • Support per-file encodings
  • External difftool
    diff
  • Web browser
    Web
  • Use themed widgets

2.1.3 View

  • View Name
  • Branches & tags
    All refs
    All (local) branches
    All tags
    All remote-tracking branches
  • Commit Info
    Author
    Committer
    Commit Message
    Matches all Commit Info citeria
    Matches no Commit Info crteria
  • Changes to Files
    Fixed String
    Regular Expression
  • Commit Dates
    Since
    Until
  • Limit and/or skip a number of revision (positive integer)
  • Miscellaneous options
    Strictly sort by date
    Mark branch sides
    Limit to first parent
    Simple history
    git log
  • Enter files and directories to include, one per line

2.1.4 Help

gitk
gitk

2.2

SHA-1
gitk
Row

HEAD

find

2.2.1

  • Check out this branch
    HEAD
  • Rename this branch
  • Remove this branch
  • Copy branch name

2.2.2

  • Create tag
  • Copy commit reference
    7
    SHA-1
  • Write commit to file
  • Create new branch
  • Cherry-pick
  • Reset master branch to here
    master
    Soft
    Mixed
    Hard
    Hard
    Mixed
    Soft

  • Mark this commit
  • Revert this commit

2.2.3

  • Return to mark
  • Find descendant of this and mark
  • Compare with marked commit
    diff
  • Diff this -> markedcommit
    diff
  • Diff marked commit -> this
    diff

2.2.4

  • Diff this-> selected
    diff
  • Diff selected -> this
    diff
  • Make patch
    diff
    patch

2.2.5

commit

  • containing
  • touching paths
  • adding/removing string
  • changing lines matching

  • Exact
  • IgnCase
  • Regexp

  • All fileds
  • Headline
  • Comments
  • Author
  • Committer

2.3

diff

2.3.1

  • Diff
    diff
  • Old version
  • New version
  • Lines of context
  • ignore space change
  • diff
    Line diff
    Markup words
    Color words

2.3.2

  • Author
  • Committer
  • Tags
  • Parent
  • Branches
  • Follows
  • Precedes

2.4

2.4.1

  • Patch
  • Tree

2.4.2

  • Highlight this too
  • Highlight this only
  • External diff
    diff
  • Blame parent commit
  • Copy path

3 Git Gui

Git Gui
Git

Git GUI Here
git gui
A
B
C
D
E

3.1

3.1.1 Repository

  • Explore Working Copy
  • Git Bash
    Git
  • Browse master's Files
  • Browse Branch Files
    Revision Expression
    Local Branch
    Tracking Branch
    Tag

  • Visualize master's History
  • Visualize All Branch History
  • Database Statistics
    Number of loose objects
    Disk space used by loose objects
    Number of packed objects
    Number of packs
    Disk space used by packed objects
    Packed objects waiting for pruning
    Garbage files
    Compress Database
    git gc

  • Compress Database
  • Verify Database
    fsck
  • Create Desktop Icon
  • Quit

3.1.2 Edit

Edit
Undo
Redo
Cut
Copy
Paste
Delete
Select All
Options
Options

repo Repository
Global (All Repository)

  • Summarize Merge Commits
  • Show Diffstat After Merge
    diff
  • use Merge Tool
  • Trust File Modification Timestamps
  • Prune Tracking Branches During Fetch
  • Match Tracking Branches
  • Use Textconv For Diffs and Blames
    Textconv
  • Blame Copy Only On Changed Files
  • Maximum Length of Recent Repositories List
  • Minimum Letters To Blame Copy On
  • Blame History Context Radius (days)
  • Number of Diff Context Line
    diff
  • Additional Diff Parameters
    diff
  • Commit Message Text Width
  • Commit Message Text Width
  • Default File Contents Encoding
  • Warn before committing to a detached head
    HEAD
  • Staging of untracked files
    yes
    no
    ask
  • Show untracked files

3.1.3 Branch

  • Create
    Name
    Match Tracking Branch Name
    Local Branch
    Tracking Branch
    Tag
    Options
    Update Existing Branch
    No
    Fast Forward Only
    Reset
    Fetch Tracking Branch
    Checkout After Creation

  • Checkout
  • Rename
  • Delete
  • Reset

3.1.4 Commit

  • Amend Last Commit
  • Rescan
  • Statge To Commit
  • Stage Changed Files To Commit
  • Unstage Form Commit
  • Show Less Context
    diff
  • Show More Context
    diff
  • Sign Off
  • Commit

3.1.5 Merge

  • Local Merge
  • Abort Merge

3.1.6 Remote

  • Fetch from
  • Prune from
  • Remove Remote
  • Add
    Fetch Immediately
    Initialize Remote Repository and Push
    Do Nothing EIse Now

  • Push
    Force overwrite existing branch (may discard changes)
    Use thin pack (for slow network connections)
    Include tags

  • Delete Branch

3.1.7 Tools

  • Add
    Name
    Command
    Show a dialog before running
    Ask the user to select a revision
    Ask the user for additional arguments
    Don't show the command output window
    diff
    Run only if a diff is selected

  • Remove

3.1.8 Help

  • About Git Gui
    Git Gui
  • Online Documentation
  • Show SSH Key
    SSH
    Generate Key
    Key

3.2

  • Undo Last Revert
    Revert
    Revert Hunk
    Revert Line
    Git Bash
    git checkout
    Git Gui
  • Refresh
  • Copy
  • Select All
  • Copy All
  • Decrease Font Size
  • Increase Font Size
  • Encoding
  • Options

3.3

  • Stage Hunk For Commit
  • Stage Line For Commit
  • Rever Hunk
  • Rever Line
  • Show Less Context
  • Show More Context

3.4

  • Unstaged Hunk From Commit
  • Unstaged Line From Commit

3.5

  • Rescan
  • Staged Changed
  • Sign Off
  • Push
  • Amend Last Commit
  • Commit Message