0

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?

adwardwo1f
  • 817
  • 6
  • 18

1 Answers1

0

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:

  1. Let's say you are currently in the main branch and in your local, you directly ran git diff test where test is the name of the other branch you want to compare to. In this case, Git will directly find the diff between the branches main and test from local and show it on the console.

  2. But there is an interesting thing about diff, if you want to directly diff between the current local branch i.e main and the remote test branch. And if you haven't set any remote, by default it's the origin. So you can directly run git diff origin/test and it will give you diff between the local main branch and remote test 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pratap Alok Raj
  • 1,098
  • 10
  • 19
  • but i have another question. i don't understand "if you haven't set any `remote`, by default it's the `origin`". What is the meaning of `origin` here isn't the `origin` added to our remote `branch` name? "So you can directly run `git diff origin/test` and it will give you diff between the local `main` branch and remote `test` branch". isn't `origin/test` is the remote branch? – adwardwo1f May 31 '21 at 14:24
  • so origin by default means your immediate remote repository. But sometimes, in large projects, you might not have access to directly create a branch and push to their repository. You might have to fork it and then push to your immediate remote i.e `origin` and create a pull request for the other main repository. Sometimes that repo is also referred to as `upstream` – Pratap Alok Raj May 31 '21 at 14:27
  • So in here, you need to specify whether you want to compare with your fork repository branch i.e `origin\` or you want to compare to `upstream\` – Pratap Alok Raj May 31 '21 at 14:28
  • You don't own both the repos. One is just a copy of another original repo and you will be using it for your personal development. Please have a read about fork from here once :- https://www.toolsqa.com/git/difference-between-git-clone-and-git-fork/ And let me know if you still have doubt on this – Pratap Alok Raj May 31 '21 at 14:47
  • Nps at all, keep asking your doubts :) Will be happy to help :D Please accept and upvote it, if it answered your question, it keeps me motivated :D – Pratap Alok Raj May 31 '21 at 15:57
  • @jorieitomuke You might want to read this intro to all the key concepts involved here. https://www.biteinteractive.com/picturing-git-conceptions-and-misconceptions/ – matt May 31 '21 at 22:58