Issue
I’ve read about git fetch
most of them said we need to fetch
from remote before we can compare it with our local using git diff
but why i can use git diff
without using fetch
and see exact same changes that i pushed to remote. i think because all the change was pushed from my local? or sth else?
Solution
Git fetch
command downloads objects and refs from another repository. When no remote is specified, by default the origin remote will be used, unless there’s an upstream branch configured for the current branch.
Below is an instance to understand what might be happening:
-
Let’s say you are currently in the
main
branch and in your local, you directly rangit diff test
wheretest
is the name of the other branch you want to compare to. In this case, Git will directly find the diff between the branchesmain
andtest
from local and show it on the console. -
But there is an interesting thing about
diff
, if you want to directly diff between the current local branch i.emain
and the remotetest
branch. And if you haven’t set any remote, by default it’s the origin. So you can directly rungit diff origin/test
and it will give you diff between the localmain
branch and remotetest
branch
You are getting the same diff even without doing any fetch is because you might have the branch test' from local and remote synced with each other. Please check the
git logs` for that and compare the commits.
If you want to explore more on what git fetch
has done, open .git/FETCH_HEAD
. The names of refs that are fetched, together with the object names they point at, are written to .git/FETCH_HEAD. Interestingly, this information is used by scripts or other git commands, such as git-pull. This way you can find what git fetch
has done.
Answered By – Alok Raj
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0