World map

 

BM: Baca dalam Bahasa Melayu

World 10: Deep Git

Revision syntax

You will learn: how to name any commit, or a group of commits, without copying ids.

The problem

The graph: main has seven commits, with the menu branch merged in. The prices branch has one extra commit. Terminal output: $ git log --oneline --graph --all * c804c3f (HEAD -> main) Add about page | * 9cd9e6c (prices) Add a price |/ * ab7c243 Link the menu * ec4a356 Merge menu |\ | * bdfdf02 (menu) Add onde-onde | * 4a73658 Add menu page * | ce13150 Say we open daily |/ * dd18cc9 Add shop page

You need the commit two steps back. Copying long ids gets old fast. Is there a shorter way?

Watch

HEAD jumps two commits back along main, to Merge menu.
dd18cc94a73658bdfdf02ce13150ec4a356ab7c2439cd9e6cc804c3f

A revision: Any way to name a commit: an id, a branch, HEAD~2, v1.0 and more. is any way to name a commit. HEAD~2 means two steps back from HEAD. Each step follows the first parent.

Try it

Count the steps back along main.

Tap the commit that HEAD~3 names.

dd18cc94a73658bdfdf02ce13150ec4a356ab7c2439cd9e6cc804c3f

Predict

HEAD~2 is a merge, so it has two parents. ^2 picks the second one. Which commit is HEAD~2^2?

Pick one answer.

Try it

Visit “Add menu page” with git switch --detach. Name it from HEAD, using ~ and ^.

dd18cc94a73658bdfdf02ce13150ec4a356ab7c2439cd9e6cc804c3f

Watch

main..prices lists Add a price. prices..main lists Add about page. Terminal output: $ git log --oneline main..prices 9cd9e6c (prices) Add a price $ git log --oneline prices..main c804c3f (HEAD -> main) Add about page

A..B (two dots) lists commits in B that A does not have. Swap the sides, and you see the other side’s extra work.

Try it

A...B (three dots) lists commits on either side, but not both. --left-right marks the sides: < for A, > for B.

Tap the commit that only prices has.

~/shop $ git log --oneline --left-right main...prices

0 of 1 found

Check

Before a merge, you review what prices will bring into main. Which command?

Pick one answer.

You did it!

You can now name any commit without copying its id.

Go deeper (optional)

HEAD^ is the same as HEAD~1. HEAD@{1} is where HEAD was one move ago, from the reflog. v1.0^{} is the commit a tag points to. git rev-parse turns any revision into a full id. git diff main...prices shows what prices changed since it split off.

rev-parse prints the short id of Add onde-onde. git diff main...prices shows one line in menu.html gaining RM1. Terminal output: $ git rev-parse --short HEAD~2^2 bdfdf02 $ git diff main...prices diff --git a/menu.html b/menu.html index d80e4d9..b83048a 100644 --- a/menu.html +++ b/menu.html @@ -1,3 +1,3 @@ <h1>Menu</h1> -<p>Kuih lapis</p> +<p>Kuih lapis RM1</p> <p>Onde-onde</p>