Comparing changes in commits to the current file by Git

How can you show the differences of a file in the last 5 commits to the current uncommitted file by Git-show?

I made a change to my file which breaks my code. However, I do not know where the change is.

I would like to compare the current uncommitted files to the recent commit (HEAD), to the previous commit (^HEAD) and at least 3 commits deeper.

However, I do not know how you can do it efficiently.

In trying to see the changes of the five last commits of one file to the current file in the given branch, I unsuccessfully ran

git show next~5:handle_questions.php

Upvotes: 29

Views: 21760

Answers (5)

zzapper
zzapper

Reputation: 5041

From @db_ answer (but shown as an actual example)

I just want to see what difference there is between the current file and the last commit for just a single file.

git diff scripts/processQueue.php


-  if( filemtime( $filename ) < time() - 10 ) {
+  $filemtime=filemtime($filename);
+  if( $filemtime < time() - 10 && $filemtime> (time() - 86400)) 

Upvotes: 0

Mike Tierney
Mike Tierney

Reputation: 111

If you know the file that the change was made in, you can also use git blame <path> - this will give you ahistory of each line of code in the following format:

SHA (Author Timestamp Line Number) code

Upvotes: 2

William Pursell
William Pursell

Reputation: 212564

To see the diff between handle_questions.php in the working directory and in the repository 5 commits back, use:

$ git diff HEAD~5 handle_questions.php

Upvotes: 4

db_
db_

Reputation: 1182

Here is my cheat-sheet:

# uncommited file to HEAD
git diff <path>

# uncommited file to before last commit
git diff HEAD^ -- <path>

#last commit to before last commit
git diff HEAD^ HEAD -- <path>

#difference between HEAD and n-th grandparent
git diff HEAD~n HEAD -- <path>

#Another cool feature is whatchanged command
git whatchanged -- <path>

Upvotes: 69

Cody Caughlan
Cody Caughlan

Reputation: 32748

You can use git bisect to track down the commit which introduced a bug.

Upvotes: 3

Related Questions