Skip to content

Instantly share code, notes, and snippets.

@dasgoll
Forked from esmooov/ctrr.md
Last active March 3, 2019 20:10
Show Gist options
  • Save dasgoll/d50caae170ade89b1f5921bb050aaf42 to your computer and use it in GitHub Desktop.
Save dasgoll/d50caae170ade89b1f5921bb050aaf42 to your computer and use it in GitHub Desktop.
Carats and Tildes, Resets and Reverts

http://www.paulboxley.com/blog/2011/06/git-caret-and-tilde

Oh cool but what's 'HEAD^'

Carats and tildes are relative commit markers in Git. They both mean "parent" but in a different way.

Most commonly used, they are the same. HEAD^1 (or HEAD^ for short) is the same as HEAD1 (HEAD). Always.

The difference comes when they stack. So HEAD^2 means "The second parent of HEAD". This only means anything if there's been a merge. In a merge, the main branch is parent #1; the merged in branch is parent 2. So, HEAD^2 will be the merged parent, whereas HEAD^1 will be the parent merged into.

Now, HEAD^^ is not the same as HEAD^2. HEAD^^ means the first parent of the first parent. This is just shorthand for HEAD^1^1. Evaluate from left to right.

But HEAD^^ is ugly. Isn't there something nicer. Yes. Tilde. Tilde ALWAYS means first parent. so HEAD~2 == HEAD^^.

Finally, you can stack tildes and carats. Say you wanted the merged in branch of two heads ago. That would be HEAD~2^2. I'm sure you will be doing that a lot. Not really.

Here's a good chart Carat vs Tilde

That's from a good piece on carats and tildes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment