Git Fetch
In this article, we will focus on the fetch command and its usage in Git. We will explain what fetch does, how it differs from pull
, and when to use it. We will also discuss some common scenarios where fetch
can come in handy and provide practical examples to illustrate its usage. By the end of this article, you will have a clear understanding of fetch
and be able to use it effectively in your Git workflow.
1. Introduction
Git is a distributed version control system that is free and open source and is made to efficiently and quickly handle projects of all sizes. Git is simple to use, has a small environmental impact, and performs really well. With capabilities like affordable local branching, handy staging areas, and numerous workflows, it surpasses SCM solutions like Subversion, CVS, Perforce, and ClearCase. Git’s branching approach is what really sets it different from almost every other SCM out there. Git enables and encourages the use of numerous locally maintained branches that are completely independent of one another. It takes only a few seconds to create, merge, and delete those lines of development.
2. git fetch
The git fetch
command transfers commits, files, and references into your local repository from a remote repository. When you want to know what everyone else has been working on, you fetch. In that it allows you to observe how the central history has developed without requiring you to actually merge the changes into your repository, it is comparable to svn update
. Git completely has no impact on your local development work; it separates fetched material from already-existing local stuff. The git checkout
command must be used to specifically check out any fetched material. As a result, fetching is a secure method for reviewing contributions before incorporating them into your local repository. As a result, tags pointing at branches that you are interested in are fetched by default together with any tag that links into the history being requested. This default behavior can be changed by using the --tags
or --no-tags
options or by configuring remote.<name>.tagOpt
git fetch
can fetch from either a single named repository or URL, or from several repositories at once if given and there is a remotes. entry
in the configuration file. Unless an upstream branch is set up for the current branch, the origin
remote will be utilised by default if no remote is supplied.
3. git fetch vs git pull
Using the git pull
and git fetch
commands, it is possible to obtain files from a remote repository. The ‘safe’ version of the two commands may be thought of as git fetch
. Your existing work will be preserved because it won’t alter the working state of your local repo while downloading the remote material. The more aggressive option is git pull
, which launches git merge
to generate a merge commit for the newly downloaded remote material as soon as the remote content is downloaded for the current local branch. Conflicts will arise if you have outstanding modifications that are still being processed, which will start the merge conflict resolution cycle.
4. Example
In this section we will discuss how the git fetch
works using an example. Let’s talk about how Git organises and saves commits so that you can comprehend how git fetch
functions. Git keeps all commits, local and remote, in the repository’s ./.git/objects
directory. Git uses branch refs to clearly distinguish between remote and local branch commits. The ./.git/refs/heads/
directory houses the references for local branches. A list of the local branch references is produced by using the git branch
command. The result of a git branch
with a few demo branch names is shown in the example below:
~$ git branch
feature-1
feature-2
* master
Similar results might be found by looking at the /.git/refs/heads/
directory’s contents:
ls ./.git/refs/heads/
feature-1 feature-2 master
Remote branches are just like local branches, except they map to commits from somebody else’s repository. To avoid confusing remote branches with local branches, remote branches are prefixed with the remote to which they belong. Git has references for remote branches in addition to local branches. Remote branch refs live in the ./.git/refs/remotes/
directory.
git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/ase-code-scanning-rollout
remotes/origin/gh-pages
remotes/origin/ghpages-javadocs
Like a local branch, a remote one may be checked out, however doing so places you in a detached HEAD state (similar to checking out an older commit). You can inspect remote branches with the usual git checkout
and git log
commands. You may use a standard git merge
to merge a remote branch into a local branch if you approve the modifications it includes. Therefore, unlike SVN, there are two steps involved in synchronising your local repository with a remote repository: fetch, then merge. The git pull
command is a convenient shortcut for this process.
5. git fetch options
git fetch <remote>
Fetch all of the branches from the repository. Additionally, all necessary files and commits from the other repository are downloaded in this process.
git fetch <remote> <branch>
Same as the aforementioned command, but just get the designated branch.
git fetch --all
Fetches all the registered remotes and their branches.
git fetch --dry-run
The --dry-run
option will perform a demo run of the command. It will output examples of actions it will take during the fetch but not apply them. E.g:
git fetch --dry-run
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 29 (delta 10), reused 28 (delta 10), pack-reused 0
Unpacking objects: 100% (29/29), done.
From https://github.com/square/okhttp
7bacd2e62..cfaefe00c master -> origin/master
* [new branch] jwilson.0506.http_url_test -> origin/jwilson.0506.http_url_test
+ dc1fa2d88...d3ca4aa6c renovate/kotlin-monorepo -> origin/renovate/kotlin-monorepo (forced update)
6. Summary
The git fetch
command fetches branches and/or tags from one or more other repositories, together with the objects required to complete their histories (collectively, “refs”). This is the main tool used to obtain files from a remote repository. git fetch
is used in conjunction with git remote
, git branch
, git checkout
, and git reset
to update a local repository to the state of a remote. git fetch
has similar behavior to git pull
, however, git fetch
can be considered a safer, nondestructive version.