I have a commit number. I would like to get the previous commit number (parent). I need commits from the current branch.
-
There is no telling which parent was the one from "current branch", alas, in case of multiple parents. – marcolz May 22 '17 at 12:22
-
6All parents are in the same branch as the descendant commit, by definition. – William Pursell May 22 '17 at 12:28
-
Possible duplicate of [How to get the parents of a merge commit in git?](https://stackoverflow.com/questions/9059335/how-to-get-the-parents-of-a-merge-commit-in-git) – Ciro Santilli OurBigBook.com Jun 01 '18 at 06:39
12 Answers
-
4Upvoted. This command gets all parents of `$commit_from`. `-n 1` can be `-1` for short. – ElpieKay May 22 '17 at 13:51
-
-
@mljrg You are right. But it seems a bug. The parent sha1 values are part of the meta data of a commit object. – ElpieKay Feb 22 '18 at 02:15
-
1I needed a well performing version for my use case; `git rev-list --parents -n 1 [commit]` runs 6.27 ± 7.92 times faster according to hyperfine. You do then need to filter out the first element, e.g. with `| cut -d' ' -f2-` to leave only the parents. – TamaMcGlinn Feb 07 '21 at 20:18
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

- 19,645
- 3
- 36
- 61

- 1,767
- 18
- 12
-
3This works with shallow clones, and their grafted commits as listed in the `shallow` file. – mljrg Feb 21 '18 at 17:46
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}^

- 204,365
- 48
- 270
- 300
-
1This will fail on the root commit when what might be wanted in the empty object hash that is the ultimate first object in any git repository. – Caleb Mar 01 '19 at 17:03
-
4
-
3@user151841 It's only an edge case until you try to do _anything_ that operates on the whole repository, whether it's a UI or an analyzer or whatever. In fact pretty much any tooling you build on top of git must consider this case, only one off things from the command line can rely on the user to supply the context. Basically there are 3 or 4 different scenarios and this command only handles one of them. – Caleb May 13 '19 at 18:36
-
@Caleb by "root commit", do you mean the very first commit in a repository? How does that come into play if you do anyting that "operates on the whole repostory"? – user151841 May 13 '19 at 18:40
-
This doesn't list all parents only the previous commit. If I have a merge commit into a master branch then the @MPAW's solution lists two commits (the previous one and the one from which the feature branch has started) but this command fails in that case. – Juraj Martinka Jul 24 '19 at 13:00
-
@JurajMartinka No, it doesn't list all parents. The question is "I would like to get previous commit number(parent)." "Parent" is singular. – William Pursell Jul 24 '19 at 16:04
-
Still misleading. Which parent? To disambiguate I think you _need_ to list all parents. – Juraj Martinka Jul 25 '19 at 08:24
-
1@JurajMartinka A non-merge commit has zero or one parent. It may have many ancestors, but it has at most one parent. There is nothing misleading about it. However, using the word "parent" when you intend to use the word "ancestor" *is* misleading. – William Pursell Jul 25 '19 at 15:37
-
@WilliamPursell It would be more useful to provide a generic answer rather than assuming it's not a merge commit. Both @MPAW's and @nate's answer (with `^@` ) do this and list all parents - not ancestors. – Juraj Martinka Jul 25 '19 at 19:35
-
For example: git rev-parse aaaaaf3f856577815b0fcda95d6ef8d9dc6eeeee^ – Andrew Rondeau Aug 09 '19 at 20:09
-
@Caleb you can have more than one "root commit" if you've used `git merge --allow-unrelated-histories` – diachedelic Jun 26 '20 at 22:03
-
@diachedelic I'm aware of that. The parent of such commits is still the empty object hash — which various between repositories and hence might be something you want to discover and use; something this answer's solution doesn't account for. – Caleb Jul 12 '20 at 12:27
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).

- 792
- 6
- 10
-
1In Windows, the shell may interpret ^ as a line continuation character. So you either need to type ^^ or you should use the tilde (~) syntax. – Craig Apr 13 '22 at 14:36
-
Bear in mind that `~` and `^` have different meaning in Git history traversal. – Victor Schröder Feb 24 '23 at 13:19
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^

- 16,401
- 10
- 68
- 88
-
-
1Does not work to get the parents of grafted commits. @Nayagam answer works with grafted commits. – mljrg Feb 21 '18 at 17:48
-
Actually, most commonly, one would simply use `git log` to see the history of commit numbers and their commit message, and then simply select the commit number you want. – Magne Oct 30 '20 at 13:52
-
3In Windows, the shell may interpret ^ as a line continuation character. So you either need to type ^^ or you should use the tilde (~) syntax. – Craig Apr 13 '22 at 14:36
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^@

- 258
- 4
- 9
-
-
The two parent commits can be referred to using the ^1 and ^2 suffixes. - $commit^1 refers to the first parent of the merge commit. This is the commit from the target branch, which was merged into the source branch. - $commit^2 refers to the second parent of the merge commit. This is the commit from the source branch, which was merged into the target branch. – EranGrin Feb 19 '23 at 22:48
-
And subsequent parents are determined by the order in which they were merged in? – user18348324 Feb 20 '23 at 07:13
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>
.

- 1,077
- 1
- 8
- 20
-
In my case, `git show` gives a different answer comparing to `git rev-parse`: `$ git show -p 1eab0c14^@ --pretty=%P --no-patch` --> c0d22588057a08a8461e70e2e4ae96174f55fc79 524709d4f1ebf9e7bf65c3f5b31583532e48880f vs `$ git rev-parse 1eab0c14^@` --> 3974c549a04465ad5404feda6b91867cbf3df7f6 c6fdab24cca32151dd77a6749bcbd3445497a5fa – it-alien Jun 24 '22 at 09:23
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.

- 733
- 8
- 9
For the full details: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1
For just the hash: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1 --pretty=%H

- 702
- 1
- 9
- 16
The previous commit of a commit can be reached also using the ~
notation:
git log aabbccdd~1

- 6,187
- 1
- 31
- 45
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.

- 4,430
- 2
- 36
- 49
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"
}
],
...

- 65
- 1
- 3