68

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.

transilvlad
  • 13,974
  • 13
  • 45
  • 80
  • 5
    Are you looking to go *backwards* through history, from commit to (one of its) parent(s), or *forwards*, from commit to (one of its) child(ren)? The former is trivial (use the `^` or `~` notations), the latter is difficult (see http://stackoverflow.com/questions/2263674/how-do-i-find-the-next-commit-in-git). – torek Sep 04 '14 at 11:34
  • 1
    I am trying to go backwards. – transilvlad Sep 04 '14 at 11:35
  • 1
    `git show --format="%P" ` shows you all parents of a given `SHA`. Tested it on `git 1.8.5.2`. Took the answer from this: http://stackoverflow.com/a/7211140/630866 – Bhaskar Sep 04 '14 at 11:44
  • Your title doesn't specify this is the commit has of a specific file. I need the general last commit and only the hash. – TamusJRoyce Jul 12 '19 at 17:28
  • I don't follow. Can you please rephrase. – transilvlad Jul 14 '19 at 01:29

2 Answers2

87

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.

Nikhil Gupta
  • 1,708
  • 1
  • 23
  • 38
  • 4
    If `HEAD` has more than one parent, this command will only show you the first one of them. – jub0bs Sep 04 '14 at 11:40
  • 3
    @Jubobs If HEAD has more than one parent, then what the OP is trying to do is inherently ambiguous, and there's probably not one good answer in that case. Using `git show ^1:` will show the file as it exists in the first parent, and will be what is wanted most of the time... – twalberg Sep 04 '14 at 14:15
  • @twalberg True. My comment was only a remark, not a criticism. – jub0bs Sep 04 '14 at 14:44
-2

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

Jerven Clark
  • 1,191
  • 2
  • 13
  • 26