32 Comparing Differences

Git’s log provides you with the information about a change—who made it, what their intent was, and so on—but sometimes looking at the changes made to the code provides more information. git diff shows you the changes that were made between two commits.

You can use git diff to view the changes—referred to as diffs or patches—of two different commits or states. Most of the time it’s called as git diff with no parameters. That’s the same as telling Git, “Show me the changes between my working tree and the staging area.”

git diff considers changes that are staged and ready to be committed as part of the repository. You can tell Git to show the differences between what is staged and what is stored in the repository by adding the --staged parameter. Note that this output does not show any changes that are in your working tree but not yet staged.

You can also provide a single, explicit commit ID to git diff to tell Git to compare your current working tree against what is in the repository at that point. For example, if you want to compare your working tree against the latest commit, regardless of what has been staged, you can provide the HEAD parameter to tell Git to compare against that commit.

Most of the time you use git diff one of the ways discussed, but you can use it in other ways. For example, you can provide a first and second commit to compare the differences between those two commits.

Looking at the differences between two commits that are far apart might generate a lot more noise than you need. You can limit the files shown in the diff by providing a path. The best practice is to include the path after a --, which tells Git, “This is a path, not a revision.” Without that, Git can confuse the path you provide with a branch or tag of the same name.

What To Do...
  • View the differences between the current working tree and the staging area.
     
    prompt>​ git diff
  • View the differences between the staged changes and repository.
     
    prompt>​ git diff --staged
  • View the differences between the working tree and a commit in the repository.
     
    prompt>​ git diff HEAD
     
    prompt>​ git diff Commit ID
  • View the differences between two “commits.”

    You can use a commit ID, branch name, or tag to reference a commit here.

     
    prompt>​ git diff first second
     
    ...​ or ...
     
    prompt>​ git diff first..second
     
    ...​ example ...
     
    prompt>​ git diff 423d021 1e85ac3
     
    ...​ or ...
     
    prompt>​ git diff 423d021..1e85ac3
  • Limit the diff output to a specific path.
     
    prompt>​ git diff -- path/

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.174.253