0

I've read a few post on fetch vs pull. I get that to push to the remote you need to fetch. I also understand that if you need to checkout a branch that was added to the remote you need to fetch from the remote to update your local tracking branches. My question is why wouldn't I simply just to a pull? Why do most developers suggest doing a fetch instead? In what circumstance are you just doing a fetch? Draw a scenario for me.

glenn
  • 31
  • 1
  • 3
  • [this](https://stackabuse.com/git-difference-between-git-fetch-and-git-pull/) link may help your understanding. – ignacio Oct 15 '19 at 15:27
  • I don't know where you read that "I get that to push to the remote you need to fetch", but it's very misleading. You don't need to fetch anything to push. It likely was a clumsy way to say "Before pushing, the interest of fetching is to check if the ref you're trying to update has changed since last time you fetched from that remote." – Romain Valeri Oct 15 '19 at 15:45
  • I disagree that explaining the difference between ‘git fetch’ and ‘git pull’ is a duplicate of explaining why you would use one or the other. A very good explanation of the underlying concepts and when each is suitable is here - https://longair.net/blog/2009/04/16/git-fetch-and-merge/ – MikeA Nov 21 '19 at 22:37

2 Answers2

2

git fetch updates your local copy of the repository but does not modify any files in the working directory. It simply makes sure that the cached info it has about the repository you fetched is up-to-date. (If other people created new branches or added some commits since the last time you fetched, git will learn about them.)

git pull is shorthand for git fetch followed by git merge FETCH_HEAD. That is, it runs git fetch and then merges the changes from the remote repository into your current branch and working directory. (As Mark pointed out, this actually modifies HEAD.)

So, the command that you should use depends on whether you want to update your working directory with commits others have made since the last time you pulled.

mkasberg
  • 16,022
  • 3
  • 42
  • 46
0

You fetch when you want to see any updates on the remote, but you don't want to immediately integrate any such changes into the current branch.

That is the only generally-applicable answer; anything else is just an example that is based on a specific workflow and a specific user's preferences; which may or may not, then, apply to you.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52