How to get the parent of a specific commit in Git
GitGit Problem Overview
I have a commit number. I would like to get the previous commit number (parent). I need commits from the current branch.
Git Solutions
Solution 1 - Git
git log --pretty=%P -n 1 "$commit_from"
Solution 2 - Git
To get Parent Commit
git cat-file -p commit_id
tree tree_id
parent parent_commit_id
[parent other_parent_commit_id] # present only in case of merge commits
author xxx <xxx@email.com> 1513768542 +0530
committer xxx <xxx@email.com> 1513768542 +0530
Solution 3 - Git
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}^
Solution 4 - Git
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).
Solution 5 - Git
To get the parent, just add a ^ after the commit SHA of the commit you want to see.
To see a commit:
git show <SHA>
Example:
git show bb05425c504575145d005c0a887e0a80b885ced0
To see the parent:
git show <SHA>^
Example:
git show bb05425c504575145d005c0a887e0a80b885ced0^
Solution 6 - Git
If you are looking for the parent of a merge commit
# Output sha of the first parent
git rev-parse $commit^1
# Output sha of the second parent
git rev-parse $commit^2
# All parents sha
git rev-parse $commit^@
Solution 7 - Git
If trying to get all parents and using revision parameter syntax, you may try using log
subcommand with
--no-walk
option.
An example, if we have the following:
$ git --oneline --graph
* A
|\
| * B
| * C
| * D
* | E
In this example, I'll use ^@
to get all parents and the --no-walk
option to
show only the parents and not their ancestors.
$ git log --no-walk A^@
commit B
........
commit E
........
Check out git rev-parse for more detail about revision parameter.
Solution 8 - Git
For the full details: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1
For just the hash: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1 --pretty=%H
Solution 9 - Git
The most efficient way
With git rev-parse <commit-ish>^@
, parents will be displayed on a different line, making it easy to both view and parse.
Be careful not to use <commit-ish>^
, this will only show the first parent, not the parents.
Other ways to inventory
Replacing -pretty=%P
below with -pretty=raw
will also work, the latter will show more content.
git cat-file -p <commit-ish>^{}
.git show -p <commit-ish>^{} --pretty=%P --no-patch
.git log --pretty=%P -n 1 <commit-ish>
.git rev-list --parents -n 1 <commit-ish>
.
Solution 10 - Git
If you only want the ids of the parents of an input commit with id <SHA>
then run this command:
git cat-file -p <SHA> | awk 'NR > 1 {if(/^parent/){print $2; next}{exit}}'
This will work for normal and shallow clones.
Solution 11 - Git
The previous commit of a commit can be reached also using the ~
notation:
git log aabbccdd~1
Solution 12 - Git
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/vadz/libtiff/commits/1ad0b2b5d3f1bd8bfe34d81e7742966cffeab6eb
...
"parents": [
{
"sha": "86a4d8ea94037f4b440caef8f5e4466adb99c536",
"url": "https://api.github.com/repos/vadz/libtiff/commits/86a4d8ea94037f4b440caef8f5e4466adb99c536",
"html_url": "https://github.com/vadz/libtiff/commit/86a4d8ea94037f4b440caef8f5e4466adb99c536"
}
],
...