transilvlad
transilvlad

Reputation: 14532

GIT get the commit hash prior to a specific commit

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 get the file of the previous commit before the one whose hash I have.

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

I was wondering if there is a simple way.

Upvotes: 75

Views: 52942

Answers (3)

CypherX
CypherX

Reputation: 7353

Showing 4 commits chronologically BEFORE and AFTER the target commit hash.

# Method-1
git log --oneline | grep -C 4 <commit-hash>
# Method-2
git log --oneline | grep -A 4 -B 4 <commit-hash>

Showing 4 commits chronologically BEFORE the target commit-hash:

git log --oneline | grep -A 4 <commit-hash>

Showing 4 commits chronologically AFTER the target commit-hash:

git log --oneline | grep -B 4 <commit-hash>

Upvotes: 1

Anton Melnyk
Anton Melnyk

Reputation: 21

Depends on commit message: git log | grep -A <number_of_lines> <commit_hash> <number_of_lines>

Upvotes: 0

Nikhil Gupta
Nikhil Gupta

Reputation: 1778

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.

Upvotes: 94

Related Questions