63

I have a commit number. I would like to get the previous commit number (parent). I need commits from the current branch.

3

12 Answers 12

49
git log --pretty=%P -n 1 "$commit_from"
4
  • 4
    Upvoted. This command gets all parents of $commit_from. -n 1 can be -1 for short.
    – ElpieKay
    May 22, 2017 at 13:51
  • @ElpieKay Does not work for shallow clones. See my answer above this.
    – mljrg
    Feb 21, 2018 at 19:44
  • @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, 2018 at 2:15
  • 1
    I 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. Feb 7, 2021 at 20:18
32

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 
1
  • 3
    This works with shallow clones, and their grafted commits as listed in the shallow file.
    – mljrg
    Feb 21, 2018 at 17:46
20

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}^
12
  • 1
    This 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 1, 2019 at 17:03
  • 4
    @Caleb seems like pretty edge case
    – user151841
    May 13, 2019 at 18:13
  • 2
    @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, 2019 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, 2019 at 18:40
  • 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. Jul 25, 2019 at 15:37
19

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).
2
  • 1
    In 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, 2022 at 14:36
  • Bear in mind that ~ and ^ have different meaning in Git history traversal. Feb 24 at 13:19
14

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^

4
  • @Sheen Thanks. Fixed it.
    – Magne
    Nov 29, 2017 at 0:22
  • 1
    Does not work to get the parents of grafted commits. @Nayagam answer works with grafted commits.
    – mljrg
    Feb 21, 2018 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, 2020 at 13:52
  • 2
    In 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, 2022 at 14:36
7

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^@
3
  • How is it determined what is first and second parent? Feb 18 at 20:47
  • 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 at 22:48
  • And subsequent parents are determined by the order in which they were merged in? Feb 20 at 7:13
3

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.

  1. git cat-file -p <commit-ish>^{}.
  2. git show -p <commit-ish>^{} --pretty=%P --no-patch.
  3. git log --pretty=%P -n 1 <commit-ish>.
  4. git rev-list --parents -n 1 <commit-ish>.
1
  • 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, 2022 at 9:23
2

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.

2

For the full details: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1

For just the hash: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1 --pretty=%H

1

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.

1

The previous commit of a commit can be reached also using the ~ notation:

git log aabbccdd~1
-2
 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"
    }
  ],
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.