Slide 1

Slide 1 text

࣮ߦ͠ͳ͕ΒΈΔ .gitͷத਎

Slide 2

Slide 2 text

໨త • gitͷΦϒδΣΫτ؅ཧͷ࢓૊ΈΛཧղ͢Δ • ίϛοτ͔ΒͭΒͳΔάϥϑߏ଄ • branch/tagͳͲͷϦϑΝϨϯεߏ଄ • ߏ଄ΛΘ্͔ͬͨͰίϚϯυΛݟΔ͜ͱͰཧղ͕ਂ·Δ • Կ͕͓͖͍ͯΔ͔Θ͔Δ͔Βɺݩʹ໭͢ํ๏΋Θ͔Δ

Slide 3

Slide 3 text

git init ΛΈΔ

Slide 4

Slide 4 text

git init௚ޙͷ.git $ git init $ ls -p .git/ HEAD config description hooks/ info/ objects/ refs/ $ find .git/objects .git/objects .git/objects/info .git/objects/pack

Slide 5

Slide 5 text

git add & commitΛΈΔ $ echo 'Hello world!' > hello_world.txt $ git add hello_world.txt $ git commit -m 'First Commit' [master (root-commit) 27baa2b] First Commit 1 file changed, 1 insertion(+) create mode 100644 hello_world.txt

Slide 6

Slide 6 text

.git/objectsͷத $ find .git/objects .git/objects .git/objects/27 .git/objects/27/baa2b9da21b4485966c2a97268876b189331e9 .git/objects/9b .git/objects/9b/e385e79f8f3e0870e48474809dab293843aece .git/objects/cd .git/objects/cd/0875583aabe89ee197ea133980a9085d08e497 .git/objects/info .git/objects/pack 3ͭϑΝΠϧ͕૿Ճ

Slide 7

Slide 7 text

3ͭϑΝΠϧ͕૿͑ͨ • 1ͭ͸͓ͦΒ͘ςΩετϑΝΠϧ • ࢒Γͷ2ϑΝΠϧʹ͸Կ͕?

Slide 8

Slide 8 text

Ͱ͸ݟ͍͖ͯ·͢

Slide 9

Slide 9 text

# ίϛοτޙͷϝοηʔδ [master (root-commit) 27baa2b] First Commit ~~~~~~~ 1 file changed, 1 insertion(+) create mode 100644 hello_world.txt # .gitҎԼͷϑΝΠϧ .git/objects/27/baa2b9da21b4485966c2a97268876b189331e9 ~~~~~~~~ SHA-1͕Ұக͍ͯ͠Δ

Slide 10

Slide 10 text

27baa2b(ུ)ͷத਎ΛݟͯΈΔ $ git cat-file -t 27baa2b9da21b4485966c2a97268876b189331e9 commit ͜ͷϑΝΠϧͷλΠϓ͸commit

Slide 11

Slide 11 text

27baa2b(ུ)ͷத਎ΛݟͯΈΔ தʹ͸͜Μͳ৘ใ͕อଘ͞Ε͍ͯΔ $ git cat-file -p 27baa2b9da21b4485966c2a97268876b189331e9 tree 9be385e79f8f3e0870e48474809dab293843aece author chiastolite 1494611014 +0900 committer chiastolite 1494611014 +0900 First Commit

Slide 12

Slide 12 text

27baa2b(ུ)ͷத਎ΛݟͯΈΔ • author ͱ λΠϜελϯϓ • commiter ͱ λΠϜελϯϓ • ίϛοτϝοηʔδ • tree?

Slide 13

Slide 13 text

# commitΦϒδΣΫτͷத਎ tree 9be385e79f8f3e0870e48474809dab293843aece # .gitҎԼͷϑΝΠϧ .git/objects/9b/e385e79f8f3e0870e48474809dab293843aece ·ͨ΋SHA-1͕Ұக͍ͯ͠Δ

Slide 14

Slide 14 text

9be385e(ུ)ͷத਎ΛݟͯΈΔ $ git cat-file -t 9be385e79f8f3e0870e48474809dab293843aece tree ͜ͷϑΝΠϧͷλΠϓ͸tree

Slide 15

Slide 15 text

9be385e(ུ)ͷத਎ΛݟͯΈΔ $ git cat-file -p 9be385e79f8f3e0870e48474809dab293843aece 100644 blob cd0875583aabe89ee197ea133980a9085d08e497 hello_world.txt

Slide 16

Slide 16 text

9be385e(ུ)ͷத਎ΛݟͯΈΔ • mode • ύʔϛογϣϯͱ͔ͦ͏͍͏ͷ • ϑΝΠϧ໊ • SHA-1 • blob?

Slide 17

Slide 17 text

# treeΦϒδΣΫτͷத਎ 100644 blob cd0875583aabe89ee197ea133980a9085d08e497 hello_world.txt # .gitҎԼͷϑΝΠϧ .git/objects/cd/0875583aabe89ee197ea133980a9085d08e497 ·ͨ΋SHA-1͕Ұக͍ͯ͠Δ

Slide 18

Slide 18 text

cd08755(ུ)ͷத਎ΛݟͯΈΔ $ git cat-file -t cd0875583aabe89ee197ea133980a9085d08e497 blob ͜ͷϑΝΠϧͷλΠϓ͸blob

Slide 19

Slide 19 text

cd08755(ུ)ͷத਎ΛݟͯΈΔ $ git cat-file -p cd0875583aabe89ee197ea133980a9085d08e497 Hello world! Α͏΍͘த਎ʹḷΓ͍ͭͨ

Slide 20

Slide 20 text

͜͜·Ͱͷ·ͱΊ git ͸ • 3छྨͷλΠϓͷΦϒδΣΫτͰ؅ཧͯ͠Δ • commmit • tree • blob • ֤ΦϒδΣΫτ͸SHA-1Ͱ؅ཧ͞Ε͍ͯͯΦϒδΣΫτಉ࢜Λ ࢀর͍ͯ͠Δ

Slide 21

Slide 21 text

ਤʹ͢Δͱ͜Μͳײ͡

Slide 22

Slide 22 text

࣍ʹ # ϑΝΠϧΛαϒσΟϨΫτϦʹίϐʔ $ mkdir subdir $ cp hello_world.txt subdir/sample.txt $ git add subdir/sample.txt # ݩϑΝΠϧΛॻ͖׵͑ $ echo 'Hello world!!' > hello_world.txt $ git add -u hello_world.txt $ git commit -m'update'

Slide 23

Slide 23 text

ࠓͷঢ়ଶ !"" hello_world.txt #=> Hello world!! #"" subdir #"" sample.txt #=> Hello world!

Slide 24

Slide 24 text

commitͷத਎ $ git cat-file -p HEAD tree 95aaf923a1c8171c9ec05225a3911a0dbeb53d6f parent 27baa2b9da21b4485966c2a97268876b189331e9 author chiastolite 1494611946 +0900 committer chiastolite 1494611946 +0900 update • parent͕௥Ճ͞Ε͍ͯΔ • 1ͭલͷcommitͷSHA-1

Slide 25

Slide 25 text

treeͷத਎ git cat-file -p 95aaf923a1c8171c9ec05225a3911a0dbeb53d6f 100644 blob b297683f36d0557a8ef83506beda7b80e233723d hello_world.txt 040000 tree 601df7ed8aabbd875614ba3a301fe344e5c45ae2 subdir • த਎͕มΘͬͨhello_world.txtͷSHA-1͕มԽ • treeͷதʹtree͕

Slide 26

Slide 26 text

αϒπϦʔͷத਎ git cat-file -p 601df7ed8aabbd875614ba3a301fe344e5c45ae2 100644 blob cd0875583aabe89ee197ea133980a9085d08e497 sample.txt • sample.txt͸࠷ॳͷίϛοτͷhello_world.txtͱಉ͡SHA-1

Slide 27

Slide 27 text

͜͜·Ͱ • commitΛ͢Δͱݩcommit΁ͷࢀর͕͓͖Δ • 1όΠτϑΝΠϧ͕มΘΕ͹SHA1͕มΘΔ • SHA1͕Ͱ͖Δͱ͍͏͜ͱ͸ΦϒδΣΫτ͕1ͭੜ੒͞Ε͍ͯ Δ • gitͰ͸ࠩ෼Ͱ͸؅ཧ͕͞Ε͍ͯͳ͍ • ϑΝΠϧ໊͕มΘͬͯ΋த਎͕ҰॹͳΒ͹SHA1͸มΘΒͳ͍

Slide 28

Slide 28 text

ਤʹ͢Δͱ͜Μͳײ͡

Slide 29

Slide 29 text

SHA-1ͷ͖·Γ͔ͨ • த਎(+ΦϒδΣΫτͷtype)͔Β Ұҙʹ ܾ·͍ͬͯΔ • ͩΕ͕ͲͷλΠϛϯάͰHello world!ͯ͠΋SHA-1͸ cd0875583aabe89ee197ea133980a9085d08e497 $ git hash-object -t blob <(echo 'Hello world!') cd0875583aabe89ee197ea133980a9085d08e497 ৄ͘͠͸ Git͸sha1ͷܭࢉΛͲͷΑ͏ʹ΍͍ͬͯΔͷ͔ · DQNEO ىۀ೔ه ͕ৄ͍͠

Slide 30

Slide 30 text

refs

Slide 31

Slide 31 text

refs • master • branch • tag • HEAD(=@) • ORIG_HEAD ͳͲͳͲ

Slide 32

Slide 32 text

.git/HEAD ݱࡏࢀর͍ͯ͠Δͱ͜Ζ $ cat .git/HEAD ref: refs/heads/master ࠓ͸masterϒϥϯνΛݟ͍ͯΔΆ͍

Slide 33

Slide 33 text

$ cat .git/refs/heads/master 2e7a843cd5706b128e13f06cf7572cda27afa0a7 $ git log commit 2e7a843cd5706b128e13f06cf7572cda27afa0a7 (HEAD -> master) • Ұக • masterϒϥϯν͕ͲͷίϛοτΛࢦ͍ͯ͠Δ͔ܾΊ͍ͯΔ

Slide 34

Slide 34 text

ͨΊ͠ʹ $ mv .git/refs/heads/{master, hogehoge} $ cat 'ref: refs/heads/hogehoge' > .git/HEAD ͱ΍Δͱ git branch -m hogehoge͕Ͱ͖Δ

Slide 35

Slide 35 text

branch $ git branch test_branch $ cat .git/refs/heads/test_branch 2e7a843cd5706b128e13f06cf7572cda27afa0a7 • ϒϥϯνΛ੾͚ͬͨͩͩͱSHA-1͸͔ΘΒͳ͍ • ͜ͷ࣌఺Ͱ͸masterͱԿ΋ҧ͍͕ͳ͍ • ϒϥϯν͸ͲͪΒ͔ͱ͍͏ͱϙΠϯλʔ΍ϥϕϧతײ֮ • ݴ͏΄ͲࢬΆ͘͸ͳ͍

Slide 36

Slide 36 text

branchΛࢀর͍ͯ͠Δঢ়ଶͰίϛοτΛͯ͠ΈΔ $ git checkout test_branch Switched to branch 'test_branch' $ git rm -r subdir rm 'subdir/sample.txt' $ git commit -m'Remove sub dir'

Slide 37

Slide 37 text

$ cat .git/refs/heads/test_branch 508b59bff7ae9af5b516d873eea32c9446540c09 $ git cat-file -p 686bcfe77bf1c43eb8d4e2aa75075e6d6e7e961e tree 19e6ae1b6538047780cd1d44a10fdb0685f15705 parent 2e7a843cd5706b128e13f06cf7572cda27afa0a7 author chiastolite 1494613438 +0900 committer chiastolite 1494613438 +0900 Remove sub dir ίϛοτ͢Δref͕มߋ͞ΕΔ

Slide 38

Slide 38 text

mergeͯ͠ΈΔ $ git checkout test_branch $ git merge test_branch --no-ff Removing subdir/sample.txt Merge made by the 'recursive' strategy. subdir/sample.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 subdir/sample.txt

Slide 39

Slide 39 text

$ git cat-file -p master tree 19e6ae1b6538047780cd1d44a10fdb0685f15705 parent 2e7a843cd5706b128e13f06cf7572cda27afa0a7 parent 508b59bff7ae9af5b516d873eea32c9446540c09 author chiastolite 1494613595 +0900 committer chiastolite 1494613595 +0900 Merge branch 'test_branch' • parent͕2ͭ • masterͩͬͨͱ͜Ζ • test_branch

Slide 40

Slide 40 text

Slide 41

Slide 41 text

rebaseͯ͠ΈΔ (͜ͷ··ͩͱ্ख͍͔͘ͳ͍ͷͰmasterΛmergeલʹ໭ͯ͠ί ϛοτΛ௥Ճ͓ͯ͘͠)

Slide 42

Slide 42 text

$ git rebase master First, rewinding head to replay your work on top of it... Applying: Remove sub dir $ cat .git/refs/heads/test_branch 0957492b728dbf7854f46c30be6670fcf29b61a6 ৽͍͠SHA-1

Slide 43

Slide 43 text

rebaseલͷίϛοτ͸? $ git log 508b59bff7ae9af5b516d873eea32c9446540c09 commit 508b59bff7ae9af5b516d873eea32c9446540c09 Author: chiastolite Date: Sat May 13 03:23:58 2017 +0900 Remove sub dir ී௨ʹΞΫηεͰ͖Δ rebaseͯ͠΋ίϛοτ͕ফ͑ͨΓ͸͠ͳ͍

Slide 44

Slide 44 text

Slide 45

Slide 45 text

͓͖͍֮͑ͯͨ͜ͱ • gitͰίϛοτ͕ফ͑Δ͜ͱ͸ͳ͍ • (ྫ֎తʹ)git gcίϚϯυͷΈ • ͳͷͰؾܰʹrebase΍mergeͯ͠΍͹͔ͬͨΒ໭ͦ͏

Slide 46

Slide 46 text

ϦϞʔτͷѻ͍ • gitͰϦϞʔτϦϙδτϦͷૢ࡞Λग़དྷΔͷ͸ҎԼͷίϚϯυͷ Έ (set-remote ͸ઃఆ͢Δ͚ͩͳͷͰૢ࡞͸Ͱ͖ͳ͍) • fetch • push • pull͸಺෦Ͱfetch + (merge or rebase) ΛݺΜͰ͍Δ

Slide 47

Slide 47 text

• fetch • ϦϞʔτϦϙδτϦͷঢ়ଶΛϩʔΧϧʹ࣋ͬͯ͘Δ • push • ϩʔΧϧͷ಺༰Λࢦఆͨ͠ϒϥϯνʹૹΔ

Slide 48

Slide 48 text

git remote ײ֮తʹϨΠϠʔߏ଄ͱࣅͯΔؾ͕ͯ͠Δ • ϩʔΧϧͱϦϞʔτͱ͍͏ϨΠϠʔ • fetch͢Δ͜ͱͰϩʔΧϧͷϦϙδτϦϨΠϠʔʹϦϞʔτͷ ಺༰Λ൓ө͢ΔΈ͍ͨͳ • ౷߹͡Όͳ͍ͷ͕ҧ͏ͱ͜Ζ

Slide 49

Slide 49 text

ਤʹ͢Δͱ͜Μͳײ͡?

Slide 50

Slide 50 text

origin/masterͱorigin masterͷҧ͍ • origin master • originʹઃఆͨ͠ϦϞʔτϦϙδτϦͷmasterϒϥϯν • ϦϞʔτͷ࿩ • origin/master • originʹઃఆͨ͠ϦϞʔτϦϙδτϦͷmasterϒϥϯνͷref Λखݩʹίϐʔͨ͠΋ͷ • ϩʔΧϧͷ࿩

Slide 51

Slide 51 text

• origin/master͸࠷ޙʹfetchͨ͠ͱ͖ͷ origin ͷ masterͷঢ়ଶ ͳͷͰϦϞʔτͷ࠷৽ͱ͸ݶΒͳ͍ • ϦϞʔτʹ௥ै͠Α͏ͱ rebase origin/master͢Δ৔߹͸ ࣄ લʹfetchͨ͠΄͏͕Α͍

Slide 52

Slide 52 text

Α͘࢖͏Φϓγϣϯ • add • -u • -p • commit • --ammend • -C • -v

Slide 53

Slide 53 text

Α͘࢖͏Φϓγϣϯ • stash • --keep-index • push • --force-with-lease • rebase • -i • --autosquash(git commit --fixupલఏ)

Slide 54

Slide 54 text

Conditional configuration(>= Git 2.13) $ cat ~/.gitconfig [includeIf "gitdir:~/work/"] path = .gitconfig-work [includeIf "gitdir:~/play/"] path = .gitconfig-play $ cat ~/.gitconfig-work [user] name = Hiroyuki Morita email = [email protected] $ cat ~/.gitconfig-play [user] name = chiastolite email = [email protected] ࢓ࣄͱϓϥΠϕʔτɺ࢓ࣄͱOSSͷઃఆΛ෼͚ΒΕΔ