Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devinschumacher/26a05e6c1d8976981a42f28f65eecdb3 to your computer and use it in GitHub Desktop.
Save devinschumacher/26a05e6c1d8976981a42f28f65eecdb3 to your computer and use it in GitHub Desktop.
How to See a `git diff` Between a Remote Branch and Your Local Branch
title tags
How to See a `git diff` Between a Remote Branch and Your Local Branch
git
github
git diff

How to See a git diff Between a Remote Branch and Your Local Branch

Example: You're working locally on the dev branch and before you start you want to check to make sure that you have the most updated version.

  1. Run a git fetch to fetch any new updates from the remote
$ git fetch

# example output
remote: Enumerating objects: 49, done.
remote: Counting objects: 100% (44/44), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 22 (delta 12), reused 19 (delta 10), pack-reused 0 (from 0)
Unpacking objects: 100% (22/22), 48.79 KiB | 832.00 KiB/s, done.
From github.com:serpcompany/serp-nuxt-template
   384b705..153f709  dac/14/nuxt-seo -> origin/dac/14/nuxt-seo
 * [new branch]      dac/41/catch-all-routing -> origin/dac/41/catch-all-routing
   c03b351..477f6a8  dev             -> origin/dev

It pulled down some new branches, and some updates to the dev branch we're working on.

  1. Run this git diff command to see the specific changes between the remote dev branch and the local
# git diff <local-branch> <remote-name>/<remote-branch-name>
$ git diff dev origin/dev

And you'll see a diff:

Screenshot 2024-09-28 at 00 13 29

git diff <local-branch> <remote-name>/<remote-branch-name>

  • Considers the differences in content between the two branches, regardless of their relationship (whether one is ahead of the other).
  • It does not take into account the common ancestor of the two branches; it's just a straight comparison of changes between the two tips (local dev and remote origin/dev).

For example: If dev has 3 new commits and origin/dev has 2 new commits, it will show the combined differences between both sets of changes.

Example 2

You're working locally on a branch ds/48/add-drizzle and you did some work, push your changes, made a PR, and it failed because you forgot to update your lock file after editing the package.json file.

Screenshot 2024-09-28 at 18 51 39

Requires minor edits, so you decide to make the minor change, stage it, and instead of making a new commit you decide to just ammend your previous commit.

git commit --amend --no-edit

[ds/48/add-drizzle 9b6438f] Add drizzle studio to project for interacting with the db via GUI.
 Date: Sat Sep 28 18:20:37 2024 -0700
 4 files changed, 20 insertions(+), 7 deletions(-)

Now you try to push it up again but...

git push

To github.com:serpcompany/serp-nuxt-template.git
 ! [rejected]        ds/48/add-drizzle -> ds/48/add-drizzle (non-fast-forward)
error: failed to push some refs to 'github.com:serpcompany/serp-nuxt-template.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Uh oh! Something is wrong...

1 (1)

The issue is that because you updated your pnpm-lock.yaml file to address the PR error, which changes the commit hash.

Lets confirm:

  1. Run a git fetch --all to fetch the remote
  2. Check your local commit hash vs. the remote's
# show your HEAD commit hash
git rev-parse HEAD

# show the remote's HEAD commit hash
# git rev-parse <remote-branch-to-reference>
git rev-parse origin/ds/48/add-drizzle

They don't match, as excepted. Let's fiz it.

  1. Run a dit diff command to compare the branches
git diff HEAD <remote-branch-name>
git diff HEAD origin/ds/48/add-drizzle

Awesome, now you can see the diff.

Screenshot 2024-09-28 at 18 47 15 (1)

If these changes are what you want (likely just the lockfile update), and you're sure no one else has based work on the remote branch, you can force push your changes:

# git push --force origin <your-branch-name>
git push --force origin ds/48/add-drizzle

2

Alright cool. Now let's run the PR again:

gh pr merge 49 

Merging pull request serpcompany/serp-nuxt-template#49 (feat: Add drizzle studio)
? What merge method would you like to use?  [Use arrows to move, type to filter]
> Create a merge commit
  Rebase and merge
  Squash and merge

The option that most closely matches clicking the "Merge pull request" button in the GitHub GUI is: "Create a merge commit"

This is typically the default merge method in GitHub's web interface, unless your repository settings have been configured differently. When you choose this option:

  1. It creates a new merge commit that combines the changes from your feature branch with the base branch (usually main or master).
  2. It preserves the entire commit history of your feature branch.
  3. The commit message will typically include the PR title and number.

This method:

  1. Maintains a clear record of when and how the feature branch was integrated.
  2. Keeps all the individual commits from the feature branch in the main branch's history.
  3. Is generally the most straightforward and commonly used method, especially for teams that prefer to keep detailed historical information in their repository.
gh pr merge 49

Then answer the prompts. I recmomend NOT deleting the branches bc the PR could still fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment