Skip to content

Instantly share code, notes, and snippets.

@zhangzhhz
Last active June 25, 2024 04:36
Show Gist options
  • Save zhangzhhz/77850a5d6d64568b5ea50ffc7b6c57ce to your computer and use it in GitHub Desktop.
Save zhangzhhz/77850a5d6d64568b5ea50ffc7b6c57ce to your computer and use it in GitHub Desktop.
caret (^) and tilde (~) in Git

Both ^ and ~ themselves mean the parent.

~ followed by a number works the same as putting the same number of ~. For example, my_commit~3 is the same as my_commit~~~ which refers to the 3rd parent of my_commit.

But ^2 only makes sense for a merge commit, i.e., merge_commit^2 means the second parent of the merge_commit, merge_commit^1 is the same as merge_commit^ or merge_commit~ or merge_commit~1.

On the other hand, repetitive ^ is the same as repetitive ~. For example, ~~~ is the same as ^^^, and the same as ~3.

Some examples:

Example commit history is as follows:

* c78c529 slight re-factoring
*   7c01006 Merge a local change to the script in old name and remote changes to the script in new name.
|\  
| * 985b0f7 update to latest.
| * ce14dae updated for accessing onedrive personal
| * 3a98d15 renamed main program
* | d092386 correct a bug in handling exception for pyperclip
|/  
* 57f1ddf attemp to add more client id
* 02c23cc rewording
* 75ce0b9 minor bug fix
* 579b32a added deleting a file or non-empty folder.
* 28c7709 Added what this repository does now.

Along the main line:

$[master $%=] git log --oneline --no-walk c78c529^
7c01006 Merge a local change to the script in old name and remote changes to the script in new name.
$[master $%=] git log --oneline --no-walk c78c529^2
fatal: ambiguous argument 'c78c529^2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$[master $%=] git log --oneline --no-walk c78c529~~~~
02c23cc rewording
$[master $%=] git log --oneline --no-walk c78c529~4
02c23cc rewording
$[master $%=] 

At a merge commit:

$[master $%=] git log --oneline --no-walk 7c01006^
d092386 correct a bug in handling exception for pyperclip
$[master $%=] git log --oneline --no-walk 7c01006^1
d092386 correct a bug in handling exception for pyperclip
$[master $%=] git log --oneline --no-walk 7c01006^2
985b0f7 update to latest.
$[master $%=] git log --oneline --no-walk 7c01006~
d092386 correct a bug in handling exception for pyperclip
$[master $%=] git log --oneline --no-walk 7c01006~1
d092386 correct a bug in handling exception for pyperclip
$[master $%=] git log --oneline --no-walk 7c01006~2
57f1ddf attemp to add more client id
$[master $%=] git log --oneline --no-walk 7c01006^^
57f1ddf attemp to add more client id
$[master $%=] 

Within a merged branch:

$[master $%=] git log --oneline --no-walk 985b0f7^
ce14dae updated for accessing onedrive personal
$[master $%=] git log --oneline --no-walk 985b0f7~
ce14dae updated for accessing onedrive personal
$[master $%=] git log --oneline --no-walk 985b0f7^^^
57f1ddf attemp to add more client id
$[master $%=] git log --oneline --no-walk 985b0f7~~~
57f1ddf attemp to add more client id
$[master $%=] git log --oneline --no-walk 985b0f7~3
57f1ddf attemp to add more client id
$[master $%=] 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment