I have commit number. I would like to get previous commit number(parent). I need commits from current branch.
If ${SHA} is the commit you know and you want its parent (assuming it's not a merge commit and has only one parent):
git rev-parse ${SHA}^
You can use git rev-parse
for this.
# Output hash of the first parent
git rev-parse $commit^
# Nth parent
git rev-parse $commit^N
# All parents
git rev-parse $commit^@
These constructs are explained in git help rev-parse
:
<rev>^, e.g. HEAD^, v1.5.1^0
A suffix ^ to a revision parameter means the first
parent of that commit object. ^<n> means the <n>th
parent (i.e. <rev>^ is equivalent to <rev>^1). As a
special rule, <rev>^0 means the commit itself and is
used when <rev> is the object name of a tag object that
refers to a commit object.
...
<rev>^@, e.g. HEAD^@
A suffix ^ followed by an at sign is the same as listing
all parents of <rev> (meaning, include anything
reachable from its parents, but not the commit itself).