What is the difference between fetch vs pull in git?
When you start a new coding project, there is a 99% chance that you will use git for version control. Probably two of the most popular git commands are git pull and git fetch. A lot of the time, developers confuse them and don't understand the difference between them.
The most important differences between git fetch vs pull are:
- git fetch downloads refs and objects from a remote. It is safe to execute because it will not make any changes to your local repository.
- git pull downloads refs and objects from a remote AND updates your local repository to match the remote's content.
In this article, I will go over, in detail, all the differences between the git pull and git fetch commands and will explain which to use and when.
Let's get to it 😎.
What is the git fetch command?
git fetch downloads branches, tags, and objects from a remote, WITHOUT updating your local repository. It is safer than using git pull.
Here is an example of how to fetch from a remote in git using bash:
git fetch <remote>
P.S. If you do not specify the remote it will fetch from the default one, usually origin.
git fetch supports a lot of options that you can use to customize how the fetching will work.
For example, if you use the --all flag, git will fetch from all known remotes.
If you want to fetch from a specific branch you can do it like so:
git fetch <remote> <branch>
What is the git pull command?
git pull downloads branches, tags, and objects from a remote and UPDATES the local repository to match the remote's content.
git pull is shorthand for git fetch then git merge.
Here is an example of how to pull from a remote in git using bash:
git pull <remote>
P.S. If you do not specify the remote it will pull from the default one, usually origin.
By default, if there were remote changes, git pull will create a new merge commit. If you don't want that, use the --rebase flag.
If you want to pull from a specific branch you can do it like so:
git pull <remote> <branch>
This command will only modify the specified local branch.
When to use git fetch?
Use git fetch if you want to SEE the remote changes, but do not want to update your local repository. By using git fetch, you will have the time to think about what to do with the remote changes.
When to use git pull?
Use git pull if you are certain that you want to update your local repository or branch (depending on the options) with the remote changes.
fetch vs pull
Here is a table comparing those two git commands.
|git fetch||git pull|
|Main function||Download remote changes||Download remote changes and update the local repository|
|When to use it?||When you want to SEE the remote changes locally||When you want to UPDATE the local repository with remote's changes|
|Safety||Very safe||Less safe|
As you can see, understanding git pull vs fetch is no rocket science.
git fetch fetches the remote changes.
git pull fetches remote changes AND updates your local repository to match the remote's content.
After reading this article, I hope that you understand the differences between those two git commands better and will be able to use them when needed.
If you are interested in learning more about git, I have written a few tutorials on it.
If you have any questions don't hesitate to ask them in the comments. I always respond 😉.
Thank you for reading through this article, please share it with your fellow coders.