git compare file between branches

To compare a specific file between two branches in Git, use the git diff command. 

Comparing a Specific File:

To see the differences in a particular file between two branches, use the following command:

git diff <branch1>..<branch2> -- <path/to/file>
  • Replace <branch1> and <branch2> with the names of the branches you want to compare.
  • Replace <path/to/file> with the full path to the specific file you are interested in.

Example:

To compare the file src/index.js between the main branch and the feature/login branch:

git diff main..feature/login -- src/index.js

Using a Graphical Diff Tool:

For a more visual comparison, you can use git difftool, which opens the differences in a configured diff tool (e.g., vimdiffmeld). 

git difftool <branch1>..<branch2> -- <path/to/file>

Understanding the Output:

The git diff output displays the changes between the two versions of the file, with lines prefixed by a minus sign (-) indicating content present in <branch1> but not in <branch2>, and lines prefixed by a plus sign (+) indicating content present in <branch2> but not in <branch1>

Leave a Reply