Git Fetch and Git Pull commands are frequently used to work with remote repositories. A remote repository is a Git repository present on some hosting websites like GitHub or BitBucket.
Developers use remote repositories to collaborate on a project. Each collaborator clones the remote repository to the local machine and makes changes or adds new features. After making commits in the local repo, the developers push their changes to the remote repository.
Both Git Fetch and Git Pull collect changes from the remote repository. But there are some differences in their working. Let's learn about these commands and understand the differences between them.
The Git Fetch command fetches or collects the changes made to the remote repository by some other collaborators. These changes or commits are brought to the local repo and added to the remote-tracking branches. Remote-tracking branches track changes of the remote repository branches. Git creates remote-tracking branches whenever we clone a remote repository.
Git Fetch is used to view the differences between the local repository and the remote repository. After running the Git Fetch command, we can use the Git Diff commands to view the differences between our local branches and the remote-tracking branches.
Git Pull command is a combination of two commands - Git Fetch and Git Merge. Git Pull fetches the changes or commits from the remote repository to our local repo and adds them to the remote-tracking branch. Next, Git Pull will merge these new changes with our local branch. Git Pull is a way of making our local repository up-to-date with the remote repository.
Git Pull = Git Fetch + Git Merge
Git Fetch and Git Pull work in a slightly different manner. The key differences between them are summarized in the table below.
|Git Fetch||Git Pull|
|It Fetches new changes or commits from the remote repository to the local repository.||Fetches and merges new changes or commits from the remote repository to the local repository.|
|Updates the remote-tracking branches with the new commits but does not alter the local modifications.||Updates the remote-tracking branches with the new commits and merges the remote-tracking branches with local branches.|
|Git Fetch is a safe and harmless command.||Git Pull may lead to merge conflicts in some cases.|
|Git Fetch is used to view the changes made in the remote repository without making any local changes.||Git Pull is used to add remote changes to our local repository.|
Let's take an example to understand the working of Git Fetch and Git Pull commands.
Consider, we have a remote repository with a master branch and three commits(Commit A, B, and C). We clone this repository to our local system using the Git Clone command. We will have a local master branch and a remote-tracking branch(origin/master). Both of them point to the last commit(commit C).
Local Repo: A -- B -- C <- master, origin/master
Remote Repo(origin): A -- B -- C <- master
Now, let's add two more commits to our local repository(Commit D and E). Let's assume that a different collaborator pushes two new commits(Commit F and G) to the remote repository. The local master branch will now point to commit E. The remote-tracking branch origin/master still points to commit C. The remote master branch now points to commit G.
Local Repo: A -- B -- C -- D -- E <- master ^ origin/master
Remote Repo(origin): A -- B -- C -- F -- G <- master
When we run the Git Fetch command, the two new commits of the remote repository are added to the remote-tracking branch(origin/master). However, our local master branch is not affected.
Local Repo: A -- B -- C -- D -- E <- master \ \ F -- G <- origin/master
Instead, if we run the Git Pull command, the new commits are added to the remote-tracking branch. Next, the Git Pull command merges the two branches(local master and origin/master). A new merge commit(commit H) is created.
Local Repo: A -- B -- C -- D -- E -- H <- master \ / \ / F -- G ^ origin/master
Git Fetch and Git Pull help us interact with the remote repository. These commands allow us to download changes from the remote repository to our local repo.
Git Fetch will simply fetch the new commits and add them to the remote-tracking branch. Git Pull will go a step further and merge these changes to our local branch. Git Fetch is a safe and harmless option. It allows us to view the remote repo changes without affecting our local work.
It is recommended to use Git Pull when we have a complete idea of what the remote repository changes and commits are. Otherwise, Git Pull may lead to conflicts. Git Fetch is recommended when we just want to view the remote repo changes without affecting our local work.