Why we should prefer git fetch over git pull?

Have you ever wondered, when to use git pull and when to use git fetch.

If you observe closely, they both kind of do something similar. so the only difference is that when you do git pull it internally does git fetch followed by a git merge.

Mostly, git fetch is used to update your remote-tracking branches under refs/remotes/<remote_name>/. This will not change your local branch, and your local branch head under refs/head.

This is particularly useful if you need to keep your local repository updated with remote repository, but are working on something that might break the code if you update your files. To merge the commits into your current local branch, you must use git merge later, when you feel code is clean and nothing will break.

And, git pull is what you will do to bring your remote branch equal to your local branch, while also updating other remote tracking branches. so Git will merge any pulled commits into the branch you are currently working on. If you don’t carefully manage your branches, you may run into frequent conflicts.