1

I wanted to do git fetch instead of a git pull. But when I did a fetch it never mentioned anything like "this" file requires merging or anything like that. Why is it so?. If I am not able to know that these files require merging then what purpose does git fetch solve

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81

2 Answers2

4

To expand on anio's correct answer, git fetch updates what are called your remote tracking branches. You can think of remote tracking branches as proxies for what's on the remote server. For example, let's say you when you cloned the repository, there were three commits in it:

+----------------------------------+
| Remote repository (origin):      |
| master: A-B-C                    |
+----------------------------------+

+-------------------------------------------------------+
| Your repository:                                      |
| remotes/origin/master: A-B-C (remote tracking branch) |
| master: A-B-C (your working branch)                   |
+-------------------------------------------------------+

Now let's say that someone does and pushes some work, say commits D and E. Now the picture looks like this:

+----------------------------------+
| Remote repository (origin):      |
| master: A-B-C-D-E                |
+----------------------------------+

+-------------------------------------------------------+
| Your repository:                                      |
| remotes/origin/master: A-B-C (remote tracking branch) |
| master: A-B-C (your working branch)                   |
+-------------------------------------------------------+

Note that your repository hasn't changed; changes to a remote repository are NOT automatically pushed to your repository. However, if you do git fetch, it will update your remote tracking branch only:

+----------------------------------+
| Remote repository (origin):      |
| master: A-B-C                    |
+----------------------------------+

+-----------------------------------------------------------+
| Your repository:                                          |
| remotes/origin/master: A-B-C-D-E (remote tracking branch) |
| master: A-B-C (your working branch)                       |
+-----------------------------------------------------------+

Note how your remote tracking branch now has all the latest from the remote repository, but your working branch has not been updated because doing so requires a merge (a fast-forward merge in this case, since you haven't done any work).

That's all that git fetch does; it only updates the remote tracking branches, it does not tell you what will be merged in. To see what will be merged in, see the link anio mentioned (How can I preview a merge in git?).

Community
  • 1
  • 1
Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
1

git fetch downloads the changes but doesn't do a merge. git pull does that. Are you looking to preview what a git merge will do? Then see this: How can I preview a merge in git?

Since undoing a failed git merge is so easy, I personally just do a git merge and see what conflicts I get. Most of the time the merge goes surprisingly well. If I get too many conflicts or change my mind it is easy to undo the git merge:

git reset --hard HEAD

Git makes it easy to try things out. Don't be afraid.

Community
  • 1
  • 1
anio
  • 8,903
  • 7
  • 35
  • 53