I have commit number. I would like to get previous commit number(parent). I need commits from current branch.
For the full details: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1
For just the hash: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1 --pretty=%H
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
git log --pretty=%P -n 1 "$commit_from"
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.
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.
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^