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., vimdiff, meld). 

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

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny