Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT get the commit hash prior to a specific commit

Tags:

git

shell

centos

git 1.7.1

git show <hash>:<file> gives me the file based on the commit hash provided

I am trying to figure out how to bet the file of the previous commit before the one who's hash I have.

I know I can always use the log to get all hashes and figure out the one i need but that's not a good solution in my case as I am trying to minimise the number of commands I need to do for performance issues.

Was wondering if there is a simple way.

like image 406
transilvlad Avatar asked Sep 04 '14 11:09

transilvlad


People also ask

How do I find previous commits?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

How do you checkout to a specific commit?

Checkout From Specific Git Commit ID Follow the steps to checkout from a specific commit id. Step 1: Clone the repository or fetch all the latest changes and commits. Step 2: Get the commit ID (SHA) that you want to checkout. From your local repository, you can get the commit SHA from the log.

How do you go back to a specific commit in git?

Go back to the selected commit on your local environmentUse git checkout & the ID (in the same way you would checkout a branch) to go back: $ git checkout <commit-id> . Don't forget the final ' .


1 Answers

Use git show HEAD^1. You can replace HEAD with your commit-hash

Edit to take multiple parents into account:

In case you want to see all the parents for a commit hash, you can use git rev-list --parents -n 1 <commithash> or use git show as @Bhaskar suggested in the comments to the question.

There are other ways as well as explained here.

like image 66
Nikhil Gupta Avatar answered Oct 18 '22 12:10

Nikhil Gupta