What is the difference between tilde and caret

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the git category.

Last Updated: 2024-11-23

I had previously thought git HEAD~2 was equivalent to git HEAD^2. This is untrue.

Say the last commit was a merge commit. Then:

The form HEAD^n gives the nth "horizontal" parent of HEAD. But this only means something when there's a merge commit. Here, therefore, HEAD^1 is "a" (the main branch) and HEAD^2 is "b" (the merged in branch)

HEAD~ gives the first parent. Always. Therefore HEAD~2, means the first parent of the first parent. So here it will be the parent of "a", i.e. "a-parent". So "vertical"

References: