|
| 1 | +--- |
| 2 | +title: "Selectors" |
| 3 | +description: "Git recipes for navigating history with tilde, caret, double-dot, triple-dot, and reflog selectors." |
| 4 | +section: "playbook/selectors" |
| 5 | +order: 87 |
| 6 | +--- |
| 7 | + |
| 8 | +## Selectors |
| 9 | + |
| 10 | +### Navigate to a parent commit (tilde) |
| 11 | + |
| 12 | +```text |
| 13 | +$ git show HEAD~1 # parent |
| 14 | +$ git show HEAD~3 # great-grandparent |
| 15 | +``` |
| 16 | + |
| 17 | +Tilde follows the first-parent chain. `HEAD~3` means "go back 3 |
| 18 | +commits along the first parent." |
| 19 | + |
| 20 | +### Select a specific parent (caret) |
| 21 | + |
| 22 | +```text |
| 23 | +$ git show HEAD^1 # first parent |
| 24 | +$ git show HEAD^2 # second parent (merge commits) |
| 25 | +``` |
| 26 | + |
| 27 | +Caret selects which parent of a merge commit to follow. Only |
| 28 | +meaningful on merge commits — `^1` and `^2` differ. |
| 29 | + |
| 30 | +### Combine tilde and caret |
| 31 | + |
| 32 | +```text |
| 33 | +$ git show HEAD~1^2 # second parent of the previous commit |
| 34 | +``` |
| 35 | + |
| 36 | +### Commits on one branch but not another (double-dot) |
| 37 | + |
| 38 | +```text |
| 39 | +$ git log main..feature --oneline |
| 40 | +``` |
| 41 | + |
| 42 | +Shows commits reachable from `feature` that are not reachable from |
| 43 | +`main` — what would be merged. |
| 44 | + |
| 45 | +### Symmetric difference (triple-dot) |
| 46 | + |
| 47 | +```text |
| 48 | +$ git log --left-right main...feature --oneline |
| 49 | +``` |
| 50 | + |
| 51 | +Shows commits on either branch but not both. The `--left-right` |
| 52 | +flag marks each commit with `<` (left) or `>` (right). |
| 53 | + |
| 54 | +### Reflog selectors |
| 55 | + |
| 56 | +```text |
| 57 | +$ git show HEAD@{1} # previous position of HEAD |
| 58 | +$ git show main@{yesterday} # where main was yesterday |
| 59 | +$ git show HEAD@{2.hours.ago} # HEAD two hours ago |
| 60 | +``` |
| 61 | + |
| 62 | +Reflog entries expire after 90 days (reachable) or 30 days |
| 63 | +(unreachable) by default. |
0 commit comments