How to get the parent of a specific commit in GIT?

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

+5
source share
6 answers
git log --pretty=%P -n 1 "$commit_from" 
+9
source

If $ {SHA} is a commit you know and you want its parent (assuming it is not a merge commit and has only one parent):

 git rev-parse ${SHA}^ 
+3
source

To get Parent Commit

 git cat-file -p commit_id tree tree_id parent parent_commit_id author xxx < xxx@email.com > 1513768542 +0530 committer xxx < xxx@email.com > 1513768542 +0530 
+3
source

To get the parent, just add ^ after the commit SHA commit you want to see.

To see the commit: git show <SHA> Example: git show bb05425c504575145d005c0a887e0a80b885ced0

To see the parent: git show <SHA>^
Example: git show bb05425c504575145d005c0a887e0a80b885ced0^

+2
source

If you want the parent identifiers of the input file with the identifier <SHA> execute the following command:

 git cat-file -p <SHA> | awk 'NR > 1 {if(/^parent/){print $2; next}{exit}}' 

This will work for normal and shallow clones.

+1
source

If you are trying to get all parents and using the revision parameter syntax, you can try using the log subcommand with --no-walk .

An example if we have the following:

 $ git --oneline --graph * A |\ | * B | * C | * D * | E 

In this example, I use ^@ to get all parents and the --no-walk option to show only parents, not their ancestors.

 $ git log --no-walk A^@ commit B ........ commit E ........ 

For more information on the revision parameter, open git rev-parse .

0
source

Source: https://habr.com/ru/post/1268098/


All Articles