Local repositories reside on the computers of team members. In contrast, remote repositories are hosted on a server that is accessible for all team members - most likely on the internet or on a local network.
You can git branch -a to list all branches (local and remote) and then choose the branch name from the list (just remove remotes/ from the remote branch name. Example: git diff main origin/main (where "main" is the local main branch and "origin/main" is a remote, namely the origin and main branch.)
HEAD is not the latest revision, it's the current revision. Usually, it's the latest revision of the current branch, but it doesn't have to be. master is a name commonly given to the main branch, but it could be called anything else (or there could be no main branch). origin is a name commonly given to the main remote.
The git fetch command will fetch all changes that happened in the origin. And the git diff will show us the differents files between our working tree and the remote.
If [remote-path]
and [local-path]
are the same, you can do
$ git fetch origin master
$ git diff origin/master -- [local-path]
Note 1: The second command above will compare against the locally stored remote tracking branch. The fetch command is required to update the remote tracking branch to be in sync with the contents of the remote server. Alternatively, you can just do
$ git diff master:<path-or-file-name>
Note 2: master
can be replaced in the above examples with any branch name
To view the differences going from the remote file to the local file:
git diff remotename/branchname:remote/path/file1.txt local/path/file1.txt
To view the differences in the other direction:
git diff HEAD:local/path/file1.txt remotename/branchname:remote/path/file1.txt
Basically you can diff any two files anywhere using this notation:
git diff ref1:path/to/file1 ref2:path/to/file2
As usual, ref1
and ref2
could be branch names, remotename/branchname, commit SHAs, etc.
To compare the local repository with the remote one, simply use the below syntax:
git diff @{upstream}
For that I wrote a Bash script:
#set -x
branchname=`git branch | grep -F '*' | awk '{print $2}'`
echo $branchname
git fetch origin ${branchname}
for file in `git status | awk '{if ($1 == "modified:") print $2;}'`
do
echo "PLEASE CHECK OUT GIT DIFF FOR "$file
git difftool FETCH_HEAD $file ;
done
In the above script, I fetch the remote main branch (not necessarily its master branch - any branch) to FETCH_HEAD
, make a list of my modified file only, and compare modified files to git difftool
.
There are many difftool
supported by Git, I configured Meld Diff Viewer for good GUI comparison.
From the above script, I have prior knowledge what changes done by other teams in the same file before I follow the Git stages untracked → staged → commit, which help me to avoid unnecessary resolve merge conflict with a remote team or make a new local branch and compare and merge on the main branch.
I tried a couple of solutions, but I think an easy way is like this (you are in the local folder):
#!/bin/bash
git fetch
var_local=`cat .git/refs/heads/master`
var_remote=`git log origin/master -1 | head -n1 | cut -d" " -f2`
if [ "$var_remote" = "$var_local" ]; then
echo "Strings are equal." #1
else
echo "Strings are not equal." # 0 if you want
fi
After running this script, you will be finished comparing the local Git and remote Git using the last commit number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With