OOZOU
與我們聯繫
Back to TIL
shell

Diff a local file with remote version in Git

October 13, 2020

Easily run the below command and get a diff version from the remote

$ git diff origin/master -- [local-path]

Local and Remote Branches

When using Git, it’s important to know the difference between local and remote branches. A local branch is a branch that only exists on your machine and is not shared with other developers or the remote repository. A remote branch is a branch that exists on the remote repository and can be accessed by other developers or through the remote repository.

Local branches are used for personal development, testing and experimentation, remote branches are used for collaboration, sharing and deployment. When you create a local branch you can work on it independently without affecting the remote branch. But when you’re ready to share your changes with others you can push your local branch to the remote repository, creating a new remote branch or updating an existing one.

Fetching the latest changes

Before comparing local and remote branches you need to fetch the latest changes from the remote repository. This will make sure your local repository is up to date with the latest changes from the remote repository. To fetch the latest changes use the git fetch command. This command updates your local repository with the latest changes from the remote repository without merging them.

Using Git Diff to compare files

Git Diff is a great tool to compare files between local and remote branches. To compare files using Git Diff use the git diff command followed by the names of the branches you want to compare. For example to compare the files in the local branch main with the files in the remote branch origin/main use the command git diff main origin/main.

Git Diff will show you the differences between the two branches, it will highlight the changes made in the remote branch compared to the local branch. You can also use the --stat option to show a condensed version of the differences, showing the number of added, deleted and modified lines.

Best practices for comparing local and remote

When comparing local and remote branches you should follow best practices to get accurate and fast results. Here are some best practices to keep in mind:

  • Always fetch the latest changes from the remote repository before comparing local and remote branches.
  • Use Git Diff to compare files between local and remote branches.
  • Use the --stat option to show a condensed version of the differences.
  • Use the git log command to compare commit histories between local and remote branches.
  • Use the git status command to see the status of your local branch and the remote branch.

Advanced comparison techniques

In addition to Git Diff and Git Log there are advanced comparison techniques you can use to compare local and remote branches. Here are a few:

  • Use git difftool to compare files between local and remote branches using a graphical difftool.
  • Use git diff --name-only to list the modified files between local and remote branches.
  • Use git diff --word-diff to show the differences word by word.
  • Use git diff --ignore-space-change to ignore whitespace changes.

Now you have more tools to compare local and remote branches.

More on shell

shellDecember 8, 2020

Run incognito/private commands for Bash and Zsh

Simply put a space before the command and it won't be saved to ~/.bash\_history. E.g. qrencode -o out.png "Some secret text"

shellOctober 22, 2020

Upgrade postgres database with homebrew

To upgrade postgres you can run

shellOctober 13, 2020

Amazon AWS S3 CLI copy with set perms

Open term (or equivalent) and paste the following:

有專案構想嗎?

我們很樂意聆聽您正在打造的產品。

開始對話hello@oozou.com
OOZOU Logo

曼谷 · 新加坡 · 香港

X
Facebook
Instagram
LinkedIn

服務

  • Web 開發
  • AI 與生成式 AI 解決方案
  • 行動應用程式開發
  • 資料分析與工程
  • UI/UX 設計與產品設計
  • 數位轉型

公司

  • 關於我們
  • 職涯
  • 部落格
  • 白皮書
  • 案例研究
  • Today I Learned
  • 聯繫我們

更多

  • 產業
  • 索取報價
  • 合作夥伴網絡
© 2026 OOZOU. 版權所有。
隱私政策行為準則反賄賂反貪腐政策