What is the difference between git pull
and git pull origin master
?
What if I am on a branch other than master
, will the two commands achieve a different result?
What is the difference between git pull
and git pull origin master
?
What if I am on a branch other than master
, will the two commands achieve a different result?
Remember, a pull is a fetch and a merge.
git pull origin master fetches commits from the master branch of the origin remote (into the local origin/master branch), and then it merges origin/master into the branch you currently have checked out.
git pull only works if the branch you have checked out is tracking an upstream branch. For example, if the branch you have checked out tracks origin/master, git pull
is equivalent to git pull origin master
First, let us understand what git pull
is:
The git pull command is used to fetch and download content from a
remote repository and immediately update the local repository to
match that content. The git pull
command is a combination of
git fetch
and git merge
. git pull
will download the content from
the remote repository. Once the content is downloaded, git merge
will
merge the content to your local repository. A new merge commit will
be created and HEAD updated to point at the new commit.
Now that we know what git pull
does, when we do
git pull origin master
, it simply fetches a copy of the master
branch from the original repository, and merges it with the current
branch that you have checked out.
For more information, you can go to this link.
From the documentation:
https://git-scm.com/docs/git-pull
git pull [<options>] [<repository> [<refspec>…]]
git-pull - Fetch from and integrate with another repository or a local branch
...
Default values for and are read from the "remote" and "merge" configuration for the current branch as set by git-branch[1] --track.
So
If your branch is set to "master", then git pull and git pull origin master will do the same thing.
If your branch is set to "master", Git pull and git pull origin some-other-branch will be different
If your branch is set to "some-other-branch", then Git pull and git pull origin master will be different
Git pull = Git fetch + Git merge.
git pull origin master Let's say you are on local/master, and run this command, git will fetch commits from origin/master and then merge it into local/master.
git pull This is a shorthand for pulling commits into local branch that is tracking a remote branch.
And that brings the question, how does one make a local branch track a remote branch.
As far as I know, there are two common ways to do so:
1. When pushing for the first time:
git push -u origin branch_name
The -u
flag tells git to make the local branch track the remote branch.
git branch --track branch_name origin/branch_name