In this article, we will learn How to use the fetch command in Git.
Total Views: 1142
In this article, we will learn How to use the fetch command in Git. The act to download the commits, refs, objects from the remote repositories to the local repository is called Fetching. Git fetch will download the changes in the remote-tracking branches through which it can be merged into the local branches.
1. How to fetch a remote repository in Git?
Use the ”git fetch” command to fetch the data from the remote repository. When no remote name is specified, by default the “origin” remote will be used unless there is an upstream is configured in the remote.
How to use the git remote command in Git Bash?
For demonstration, I already have a repository configured in GitHub which have more changes (i.e., have Contact.html available in the remote repository) as compared to the local repository.
Fetch the changes from the remote repository using the “Git Fetch” command.
After fetching the changes, the “git status” command clearly shows that the branch in the local repository is behind the remote-tracking branch i.e., origin/master. Merge the changes from the remote-tracking branch to the local branch with the “git merge” command.
2. How to fetch a specific branch in Git?
We can fetch specific branches from the remote repository using “git fetch <Remote_Name> <Branch_Name>”. In the below example, we fetched the master branch changes from the Remote repository named “Origin “.
3. How to fetch all Remotes simultaneously in Git?
Use the “git fetch –all” command which will fetch all registered remotes and their associated branches.
There are several options that can be passed along with the “git fetch” command. Use the “git fetch -h” command to check the list of available options and configurations.
4. What is the difference between git fetch and git pull?
a. Git Fetch is a command which allows downloading the objects from the remote repository to remote-tracking branches whereas Git Pull is a command which allows you to fetch the objects from the remote repository and merge it in the working branch.
b. Git Fetch is safe as it only pulls the changes from the remote repository but doesn’t make any changes in the local working area whereas Git pull may lead to merge conflicts if the changes were there (on local and remote repository) in the same file as it performs fetch and merge a single command.