The Difference between GIT Fetch and GIT Pull can be explained with the following scenario:
(Keeping in mind that pictures speak louder than words!, I have provided pictorial representation)
Let's take an example that you are working on a project with your team members. So there will be one main Branch of the project and all the contributors must fork it to their own local repository and then work on this local branch to modify/Add modules then push back to the main branch.
So,
Initial State of the two Branches when you forked the main project on your local repository will be like this- (A
, B
and C
are Modules already completed of the project)
Now, you have started working on the new module (suppose D
) and when you have completed the D
module you want to push it to the main branch, But meanwhile what happens is that one of your teammates has developed new Module E
, F
and modified C
.
So now what has happened is that your local repository is lacking behind the original progress of the project and thus pushing of your changes to the main branch can lead to conflict and may cause your Module D
to malfunction.
To avoid such issues and to work parallel with the original progress of the project there are Two ways:
1. Git Fetch- This will Download all the changes that have been made to the origin/main branch project which are not present in your local branch. And will wait for the Git Merge command to apply the changes that have been fetched to your Repository or branch.
So now You can carefully monitor the files before merging it to your repository. And you can also modify D
if required because of Modified C
.
2. Git Pull- This will update your local branch with the origin/main branch i.e. actually what it does is a combination of Git Fetch and Git merge one after another.
But this may Cause Conflicts to occur, so it’s recommended to use Git Pull with a clean copy.