git diff is a powerful command-line tool provided by Git that you can use to show differences between two snapshots in your Git repository. It’s very useful for reviewing changes before you commit them to the repository or for seeing what has changed in your project over time. To use
git diff to see the differences between your local branch and an upstream branch, you’ll need to perform the following steps.
Fetch the upstream branch: Before comparing your local branch with an upstream branch, you first need to ensure that your local repository has the latest data from the upstream repository. You can fetch the upstream branch using the
git fetch command followed by the name of your remote repository and the branch name. For example, if your remote repository is named
origin and the upstream branch is
master, you would fetch the upstream branch like this:
git fetch origin master
Compare the local branch to the upstream branch: Once you’ve fetched the upstream branch, you can compare it to your local branch using the
git diff command followed by the name of your local branch and the name of the upstream branch. For example, if your local branch is named
mybranch and the upstream branch is
master, you would compare them like this:
git diff mybranch origin/master
This will print a list of all the differences between
origin/master to your console.
If you want to see only the summary of changes (which files have changed, and how many lines were added or deleted), you can use the
git diff --stat mybranch origin/master
git diff by itself without any additional parameters will compare your working directory (i.e., the state of your project files) with the index (i.e., the staging area for changes that will go into your next commit). If you want to compare two specific branches, as in the case of this question, you need to specify those branch names as arguments to
This is a straightforward way to see how your local branch differs from the upstream branch. However, the specific commands might vary slightly depending on how you’ve configured your Git environment or what exactly you want to compare. Be sure to read the documentation for
git diff and related commands for more information.