World map

 

BM: Baca dalam Bahasa Melayu

World 10: Deep Git

Plumbing vs porcelain

You will learn: Git's two kinds of commands, and how to save a blob by hand.

The problem

Your script needs the newest commit id. git log prints too much, and its look can change. What should the script use?

Pick one answer.

Watch

git rev-parse HEAD prints one full id. git ls-tree HEAD lists about.html, the css tree and index.html, each with mode, type and id. Terminal output: $ git rev-parse HEAD 9e8ddf8fb3a185e7920bc21542abe85b09f07e21 $ git ls-tree HEAD 100644 blob 85bde0423f5f223e79568ce56454532cbf598613 about.html 040000 tree a88e32d7965263df0f8e5deb4bcf41bd6ea9fc40 css 100644 blob 6b62adb46423f92b1c925e6fdcc9ec947097a1d4 index.html

Low-level commands are plumbing: Git's low-level commands, like cat-file. Plain, steady output for scripts.: the pipes under the sink. They print plain, steady output. Scripts love them.

Try it

The shiny sink on top is porcelain: Git's friendly everyday commands, like status and commit.: commands for people. Sort each command.

Drag a card into a box. Or tap the card, then tap the box.

      Watch

      git hash-object index.html and git rev-parse HEAD:index.html print the same id, 6b62adb46423f92b1c925e6fdcc9ec947097a1d4. Terminal output: $ git hash-object index.html 6b62adb46423f92b1c925e6fdcc9ec947097a1d4 $ git rev-parse HEAD:index.html 6b62adb46423f92b1c925e6fdcc9ec947097a1d4

      git hash-object prints the id Git would give a file. It matches the blob already in the commit. Same content, same id.

      Try it

      notes.txt is not in Git yet. Save its content as a blob, with no commit. Then read the blob back.

      Predict

      Ben has the same notes.txt on his laptop. He runs git hash-object notes.txt. What id does he get?

      Pick one answer.

      Check

      Build the command that prints index.html as the newest commit saved it.

      Tap the pieces in the right order.

      …

      You did it!

      You can now tell plumbing from porcelain and save a blob yourself.

      Go deeper (optional)

      Porcelain output may change between Git versions. Plumbing output stays steady. Need status in a script? git status --porcelain gives a format that is promised to stay the same. git ls-tree -r --name-only HEAD lists every file in a commit.

      git ls-tree -r --name-only HEAD lists about.html, css/style.css and index.html. Terminal output: $ git ls-tree -r --name-only HEAD about.html css/style.css index.html