Smitter
Posted on July 16, 2022
When working collaboratively with other developers on a repository, you may most likely find yourself in a situation where: Other developers are continually contributing to the remote repository and at some point, you want to incorporate these new contributions(changes) from the remote repository into your local copy of the repository.
Remember that there are often at least three copies of a project on your workstation.
- One copy is your own repository with your own commit history.
- The second copy is your working copy where you are editing and building.
- The third copy is your local cached copy of a remote repository.
Basically, git
will keep a local copy of your remote repository in your local machine. The remote repository may evolve and your local copy of remote repository is lagging behind. This is no problem untill you want to git push
or need the latest changes of the remote into your local repository.
Your options here are limited. There are two common ways to get changes from your remote repository. You've got git fetch
and git pull
to work this out for you.
And, what’s the difference between git pull
vs git fetch
, and under what circumstances should you utilize each command? Well, the good news is that I have your answers.
git fetch
. What is it?
git fetch
downloads commits, objects and refs(new content) from a remote repository to the local machine - but it doesn't integrate any of this new data into your current branch.
You can view your current branch by typing git branch
from your terminal opened at the root of the project.
git pull
. What is it?
You may have guessed it is the vice versa, right?
git pull
downloads commits, objects and refs(new content) from a remote repository to the local machine - and it immediately updates the repository in your local machine, updating your current branch to match that new downloaded content.
Normally git pull
does this by doing a git fetch
to bring the local copy of the remote repository up to date, and then merging the changes into your own code repository and possibly your working copy.
When should you use git fetch
?
git fetch
is particularly useful if you need to keep your repository up to date(local copy of your remote), but are working on something that might break if you update your local files.
This gives you time to decide on the best course of action for incorporating your changes, such as merging them in.
When should you use git pull
?
Git pull is a preferrable action when you have complete context about the changes you will be getting from your remote repository and adding to your local copy.
Comparisons
Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn’t make any changes to your local files.
This gives you time leave to find out changes in the remote branch since your last pull. You can check before doing a merge, on what files have changed and what files may result to conflicts. You may get this snapshot by running:
git fetch origin
git diff <your current branch>..origin/<remote branch>
Running git pull
on the other hand is the equivalent of running git fetch
then instantaneously merging your changes into the current branch.This makes it faster since you’re performing multiple actions in one go.
git pull
is your go-to choice if you’re probably less worried about introducing conflicts into your local repo and you just want the most up-to-date changes from the remote repository you’re pulling from.
Thanks for reading. I hope you learnt something from this article. We can connect via my Linkedin or follow me on twitter to get nuggets about programming and tech content I share. Peace✌️.
Posted on July 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.