Jim
Jim

Reputation: 19582

How do we diff in Git?

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?

Upvotes: 0

Views: 311

Answers (3)

Mir S Mehdi
Mir S Mehdi

Reputation: 1530

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.

Upvotes: 1

Petr Mensik
Petr Mensik

Reputation: 27536

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

Upvotes: 2

user4815162342
user4815162342

Reputation: 155505

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

Upvotes: 0

Related Questions