2

In the example that the Git docs give for git rebase --onto its not clear about what the ~ means

A range of commits could also be removed with rebase. If we have the following situation:

enter code here E---F---G---H---I---J topicA

then the command

git rebase --onto topicA~5 topicA~3 topicA

would result in the removal of commits F and G:

E---H'---I'---J' topicA

This is useful if F and G were flawed in some way, or should not be part of topicA. Note that the argument to --onto and the parameter can be any valid commit-ish.

Does topicA~5 mean 5 commits from the head of topicA? (So counting backwards?)

I cant think of anything else that it would mean but I want to be sure before i try it on my repo.

Community
  • 1
  • 1
Perry Bunn
  • 37
  • 6

2 Answers2

3

This is from git rev-parse

<rev>~<n>, e.g. master~3

A suffix ~<n> to a revision parameter means the commit object that is the <n>th generation ancestor of the named commit object, following only the first parents.
I.e. <rev>~3 is equivalent to <rev>^^^ which is equivalent to <rev>^1^1^1.

So in your case, yes, topicA~5 mean 5 commits from the head of topicA: commit E.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

~ Means the parent of a commit, so hash~5 is the grand-grand-grand-grand-parent of hash.

Graphically, it can be seen like that (output similar to git log --graph --oneline, oldest commit at the bottom):

* ggggg - (HEAD)
* fffff
* eeeee
* ddddd
* ccccc
* bbbbb
* aaaaa

Then:

ggggg~5 == bbbbb

I gave a more in depth explanation of ~ and ^ in this response: Git what is the logical difference between parent and ancestor

padawin
  • 4,230
  • 15
  • 19