The question we secretly ask
This is a brief explanation for the difference between
git pull and
git fetch then
merge. It’s a question that a lot of people want the answer to, being the 4th more most updated question on stackoverflow.
The reason so many people get confused is that upon first glance, they seem to do the same thing (fetching is kind of the same as pulling, right?), but, each has a distinctly different job.
git is what you would call a version control system. It tracks changes in code for software development, ensuring that there’s one central truth to code- and any changes to it are accurately recorded. It’s designed to coordinate work amongst programmers, but can be used to track any set of fils really. It’s pretty handy and a lot of people use it!
Note: In a coming article, I talk about
gitin a lot more detail, so I’ll be leaving out an introduction to it in this post. If you have any questions though, please leave a comment at the bottom!
If you want to update your local repo from the remote repo, but, you don’t want to merge any differences, then you can use:
Then after we download the updates, we can check for any differences as follows:
git diff master origin/master
After which, if we’re happy with any differences, then we can simply merge the differences as follows:
On the other hand, we can simply
merge at the same time, where any differences would need to be solved. This can be done as follows:
Which to use:
Depending on how quick you work and how your project is set up: it’ll determine whether you want to use
pull. I tend to use
pull more because i’m generally working from a fresh and clean project.
pull attempts to merge remote changes with your local ones, you’re often at risk of creating a ‘merge conflict’. Merge conflicts are where your local branch and the branch on your network differ, so any differences need to be sorted before merging the differences. You can always use git stash and un-stash in the face of differences (making conflict resolution a bit easier to deal with).
Other situations as to why you may want to use
fetch instead of
pull are as follows:
a)You want to check which new commits have been made after you’ve made some local changes:
git fetch origin master
git cherry FETCH_HEAD HEAD
b)You want to to apply a commit from
master of another remote repository to your local branch.
git fetch <url_of_another_repository> master
git cherry-pick commit_abc
c)Your colleague has made a commit to a ref
refs/reviews/XXX and asks you to have a review but you don’t have a web service to do it online. So you fetch it and checks it out.
git fetch origin refs/reviews/XXX
git checkout FETCH_HEAD
git isn’t the easiest product to get the hang of but after a while of playing around with it and fixing some mistakes, you’ll learn to depend on it quite a lot. Especially when you’re working with a big team, you’ll need to depend on
git to coordinate code integration. Given that, decisions as to whether you do
fetch becomes quite imperative and hopefully, you’ll know which to do now!
Thanks for reading! If you have any messages, please let me know!
Keep up to date with my latest articles here!