0

When I have a file, e.g. x.java in Git, how can I see the differences from previous versions in Git?

In ClearCase we do a diff graphically or from the CLI? How do we do this in Git in CLI mode?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jim
  • 18,826
  • 34
  • 135
  • 254
  • 1
    Are you asking about [`git diff`](https://www.kernel.org/pub/software/scm/git/docs/git-diff.html)? – Carl Norum May 10 '13 at 05:39
  • Git also supports a graphical mode, try `git gui` - this brings up a UI which allows to browse the whole version history, including diffs to previous versions (open "Repository/Visualize all Branch history") – Andreas Fester May 10 '13 at 05:40
  • @Andreas:just type `git gui` in cli? – Jim May 10 '13 at 05:44
  • @Jim: Yes, just `git gui`, see https://www.kernel.org/pub/software/scm/git/docs/git-gui.html. Through the context menu of any entry in the history browser, you can also diff to any other commit, not only to the previous one. There are also some other graphical git frontends, see http://git-scm.com/downloads/guis – Andreas Fester May 10 '13 at 05:48
  • Possible duplicate of [Show diff between commits](https://stackoverflow.com/questions/3368590/show-diff-between-commits) – Pooya Raki Jul 19 '18 at 07:56

3 Answers3

2

git diff HEAD~1 x.java

This will compare your file with the same file one commit back

The most recent change of file would be

git log -n 1 -- x.java, then you can copy commit hash a use it in git diff.

You can also run GUI with gitk x.java

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
1

If you know the commit numbers and you want to compare this file between commits, you can do this command:

git diff <commit_old> <commit_new> x.java

Or you can also install and use any external tool for comparing:

git difftool x.java

For using difftool, you should have installed and configured difftool on your local system.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mir S Mehdi
  • 1,482
  • 3
  • 19
  • 32
0

The simplest way to inspect when and how a particular file changed is with:

git log -p x.java

This will show you the commits that changed file.java (ignoring the commits that don't) with diffs describing the changes to the file. After locating commit(s) of interest, you can produce diffs using:

git diff COMMIT_ID x.java               # diff between COMMIT_ID and HEAD
git diff COMMIT_ID1 COMMIT_ID2 x.java   # diff between COMMIT_ID1 and COMMIT_ID2
user4815162342
  • 141,790
  • 18
  • 296
  • 355