In this tutorial we will learn about Git fetch to import commits from remote repository.
When we use the git fetch commands we fetch the commits of a remote repository into our local repository. The fetched commits are saved as remote branches separate from the local branches. So, this helps in reviewing the commits before integrating them in the local working branches.
We use the git fetch [remote-name] command to fetch all the branches, commits and files of the remote connection.
git fetch [remote-name]
$ git fetch origin
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), done.
82f16db..ba36209 master -> origin/master
Note! origin is the name we set for the central repository in the previous tutorial Git Remote - Connecting with repository.
If we want to fetch specific branch then we pass the branch name using the git fetch [remote-name] [branch-name] command.
git fetch [remote-name] [branch-name]
$ git fetch origin master
* branch master -> FETCH_HEAD
To view the remote branches that was fetched we use the git branch -r command.
git branch -r
$ git branch -r
As the fetched commits are saved as remote branches and not integrated into our local branches it gives us an opportunity to review the changes and decide whether we want to merge the fetched changes in our local branches.
Lets say other developers have committed changes and pushed those changes to the central repository which then got merged to the master branch.
Now we want to fetch those changes from the master branch. So we will run the git fetch command.
So, we now have the origin/master branch with us as remote branch and is not yet merged into the master branch of our local repository.
After reviewing the changes in the remote branch we can merge it into our local branch using the git merge command.
$ git merge origin/master
Merge made by the 'recursive' strategy.
action.php | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 action.php
Copyright © 2014 - 2017 DYclassroom. All rights reserved.
rendered in 0.0539 sec