Git
In this tutorial we will learn about Git pull which helps to fetch and merge changes.
So in the previous tutorial Git Fetch - Import commits from remote repository we learned how to fetch commits from remote repository using git fetch
command and then merge the changes using git merge
command. The git pull
command puts the two into one single command. So we fetch and merge commits using one command.
We use the git pull [remote]
command to fetch the commits from a remote repository to our local branch on which we are currently on and then merge the changes.
So, if we want to fetch and merge master branch from a remote repository into our local repository master branch then, we will first checkout master branch and we will run the git pull [remote]
command and it will fetch the master branch from the remote repository and will merge it into the master branch of our local repository.
So, basically we are running two commands git fetch
and git merge
using git pull
command.
$ git pull origin
Equivalent to:
$ git fetch origin
$ git merge origin/[current-branch-name]
Note! We have named the remote central repository connection as origin in our previous tutorial Git Remote - Connecting with repository.
Lets say, other developers of our team have committed and pushed their changes to the central repository. And those changes are now merged into the master branch of the central repository.
In the above image our local master branch is represented in blue and the remote master branch is represented in pink. We can see that the local master branch is behind the remote master branch and so, we will pull the commits from remote to local.
So, first we will checkout the master branch.
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git branch
dev
* master
Now we will run the git pull
command which will fetch and merge remote master branch into local master branch.
$ git pull origin
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:yusufshakeel/git-project
ba36209..cc6d9fc master -> origin/master
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
So, from the above output we can see that we have switched to master branch and then pulled the remote's master branch using the git pull origin
command and merged it into local master branch.
After the pull operation both the local master branch pointer and the remote master branch (origin/master) pointer will point at the same commit as shown in the above image.
ADVERTISEMENT