Difference Between Git Merge Origin/Master and Git Pull
This article outlines the differences between the git merge origin/master
and the git pull
commands. The two commands integrate changes from a remote repository to the current local branch.
However, each command operates uniquely and has different use cases, as we will discuss shortly. This is the right place if you are new to Git and struggling with the two commands.
Difference Between git merge origin/master
and git pull
We will start by dissecting each command to derive the key difference. Let’s start with the git pull
command.
the git pull
Command
According to the Git documentation, by default, the git pull
command is a combination of two commands.
- The
git fetch
command - The
git merge Fetch_Head
command
The git push
command will fetch changes from the remote repository and invoke the git merge
command to merge Fetch_Head
to the checked-out local branch.
In simpler terms, Fetch_Head
is the reference that keeps track of what has been fetched. It will store the commit at the tip of all the remote branches.
The git pull
command requires your local branch to have a remote tracking branch. A remote-tracking branch is a branch in the remote repository to which your local branch pulls changes from and pushes changes to.
If you have not set up a remote tracking branch for your local branch, the git pull
command on its own will fail. You will have to specify a remote branch in such a situation.
the git merge origin/master
Command
The git merge origin/master
integrates changes from the remote master
branch to the current branch. On its own, the git merge origin/master
command does not affect local branches.
You will need to run the git fetch
command first since your local repository is unaware of the changes in the remote repository.
Combined with the git fetch
command, the git merge origin/master
command works like the git pull
command. However, it does not require a remote tracking branch.
Take this hypothetical scenario:
In our repository, we have the master
branch. We have created a development
branch where we make edits, merge them to the master
and push them to the remote repository.
Other developers working on the same project follow the same workflow.
We have new changes in the remote master
branch, and we want to bring the changes directly to our development
branch without updating the master
branch. How do we go about this?
Since our local development
branch does not have a remote tracking branch, the git pull
command will not work.
We must fetch the changes from the remote repository and merge them directly to the development
branch. This is where the git merge origin/master
command comes into play.
To fetch from the remote, we will run:
$ git fetch
Note that this only downloads the changes but does not update anything. To merge the changes to our development
branch, we will run:
$ git merge origin/master
On the other hand, if we were checked out in the local master
branch, the git pull
command would have worked and updated the master
branch.
We use the git pull
command to integrate changes to our local branch, provided the branch has a remote tracking branch. On the other hand, the git merge origin/master
command incorporates changes from the remote master
branch to the current local branch.
You will need to fetch from the remote repository before invoking the command.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedInRelated Article - Git Merge
- Git Pull VS Merge
- Git Unmerged Files
- Difference Between Git Merge Master and Git Merge Origin/Master
- Git Cherry-Pick vs Merge Workflow
- Git Merge Dry Run