What is the "git" syntax to get a single distinction of how a single commit X modifies a single Y file?

I am writing code to programmatically run git commands and learn git at the same time. Am I reading manual pages incorrectly or is this what I want to make impossible?

The following describes how MYFILE has changed between two commits:

git diff COMMIT1..COMMIT2 - MYFILE

Good.

But let's say I just want to ask how COMMITX changed the file without specifying the previous commit. In my imagination, the syntax would be something like this:

git diff COMMITX -- MYFILE 

or that:

 git diff COMMITX^..COMMITX -- MYFILE 

But the above commands do not work (for me).

The following works in the sense that it gives me a unified diff showing how COMMITX changed MYFILE, but it also includes other things that I have to cross out - for example, author, date, checkin msg. Itโ€™s easy to clean up things, but it seems to me that I have nothing to do. Is there a team? Am I misunderstanding something?

 git show COMMITX -- MYFILE 

EDIT1: I am showing here the actual output from my git bash window. I changed show to diff and didnโ€™t get the output.

  $ git show 789e9 - dir1 / file3.txt
 commit 789e948bce733dab9605bf8eb51584e3b9a2eba3
 Author: corey 
 Date: Sun Oct 11 21:54:14 2009 -0500

     my msg

 diff --git a / dir1 / file3.txt b / dir1 / file3.txt
 index a351259..cf2bd35 100644
 --- a / dir1 / file3.txt
 +++ b / dir1 / file3.txt
 @@ -4.5 +4.7 @@ c
  ddd
  e
  f
 + a new line
 + another new line
  g
  h

 Administrator@BIOSTAR / c / temp / mygit (master)
 $ git diff 789e9 - dir1 / file3.txt

 Administrator@BIOSTAR / c / temp / mygit (master)
+4
source share
4 answers

Try:

 git show --pretty=format: <commitid> -- <file> 

Here's what it looks like:

 diff --git a/config.yb/config.y index 7750514..f051b99 100644 --- a/config.y +++ b/config.y @@ -454,8 +454,8 @@ definegame : TYPE_DEFINE_GAME '{' } game_definitions '}' { - num_games = ncnf; ncnf++; + num_games = ncnf; } ; 

At the beginning there is an empty line (not shown here due to restriction restrictions), but patch happily ignore this (indeed, it will ignore the header material in the default git show output). You can also skip through tail -n +2 to remove the specified line.

--pretty=oneline also useful:

 3ed347de4c6e0e3230f651f82aabf964c6d16100 Fix a bug where more than one defined game didn't show up or showed gibberish diff --git a/config.yb/config.y index 7750514..f051b99 100644 --- a/config.y +++ b/config.y @@ -454,8 +454,8 @@ definegame : TYPE_DEFINE_GAME '{' } game_definitions '}' { - num_games = ncnf; ncnf++; + num_games = ncnf; } ; 

However, if you format the patch to be sent somewhere, do not remove this material. In fact, use git format-patch and enjoy it. Third-party putting tools will happily ignore additional metadata, and for projects using git, git apply commit messages you provided and the author lines provided by you will be used, which will simplify their use.

+5
source

git diff COMMIT^..COMMIT file or git diff COMMIT^..COMMIT -- file both work fine for me with git 1.6.3.3. Update the courtesy of Jakub Narbsky: you can also write git diff COMMIT^! -- file git diff COMMIT^! -- file .

 $ git log --oneline b8ad655^! b8ad655 Bring in the "SimpleMenu" loader plugin $ git diff b8ad655^! lib/WWW/MenuGrinder/Plugin/SimpleLoader.pm diff --git a/WWW-MenuGrinder/lib/WWW/MenuGrinder/Plugin/SimpleLoader.pm b/WWW-MenuGrinder/lib/WWW/MenuGrinder/Plugin/SimpleLoader.pm new file mode 100644 index 0000000..14f6cd8 --- /dev/null +++ b/WWW-MenuGrinder/lib/WWW/MenuGrinder/Plugin/SimpleLoader.pm [...] 
+5
source

You do not need to use a workaround, such as git-show - you are just slightly disabled in your syntax. git-diff shows the difference between the two commits given. On the other hand, .. means "the range of fixation between ...". The correct syntax is:

 git diff COMMITX^ COMMIT -- MYFILE 

However, it really works for me with .. (I tested git diff master^..master -- git-add--interactive.perl in git.git) This is probably not always the case, but it has been working at least since git moved to the version index 2 (between versions 1.5.1 and v1.5.2) - it'd be a pain for me to check back before that.

+1
source

Usually I just do this:

 git diff > unified.diff 

and then open it in my favorite diff diff viewer.

0
source

All Articles