Introduction
The git fetch command downloads objects to the local computer without overwriting existing local code in the current branch. The command pulls a log of changes from the remote repository, allowing you to get information about progress history before adjustments.
Read on to learn how to use the git fetch command through practical examples.
Prerequisites
Git
- installed, updated, and configured
- Command line/terminal access
- link or a configured remote Git
.
. A remote repository
.
What is git fetch?
The git fetch command retrieves commits, files, branches, and tags from a remote repository. The general syntax of the command is:
git fetch <options> <remote name> <branch name>
Git isolates the content obtained from the local code. Therefore, search provides a secure way to review information before committing to your local branch.
How does the git fetch command work?
The git fetch command gets all changes from a remote repository. The retrieved metadata resides in the .git directory, while the working directory remains unchanged.
Indeed, git fetch retrieves metadata without applying changes locally. The git pull command combines the git fetch and git merge functions into one.
Because the working directory state is not affected, the retrieved content must be checked out with the git checkout command or merged with git merge.
However, since joining content is a manual process, git fetch allows you to review your code before changing anything. The review process helps avoid merge conflicts.
Git Fetch
options
Below is the list of the most commonly used options when working with git fetch:
-all –
- Get all remote controls
- append (-a) – Appends to recovered existing contents without overwriting
- depth=<depth> – Limit to a specific number of commits from the tip of each remote branch history.
- -deepen=<depth> – Limit to a specific number of commits from the current limit of each remote branch history to the tip
- Includes all commits accessible after the specified date. –shallow-exclude=<
- revision> – Excludes commits from a specified remote branch or tag. Use the option multiple times to specify multiple exclusions.
- dry-run – The option added to any command shows what happens without running the command as a demonstration.
- –multiple – Allows multiple arguments <repository or group>.
- no-tags – Disable automatic tag tracking.
- –tags – Retrieves all tags from a remote control.
- set-upstream – Add upstream tracking.
. –
. –
. -shallow-since=<date> –
–
–
–
To see all available options
, run the following command in the terminal:
git fetch -help
The command displays the full help page with a detailed description of all available options
.
Retrieve Git repositories
The examples require a remote repository added. If you already have a remote configuration, go to the next section. Otherwise, add a Git remote by following the steps below:
1. Open the terminal and create a directory for the project:
mkdir <directory name>
2. Enter the directory using the command cd:cd <directory
name>
3. Initialize the local repository with:
git init
4. Use the following command to add a remote URL to the local repository
: git remote add <short remote name> <remote URL>
For example
: git remote add origin git@github.com:phoenixNAP-KB/test.git
5. Confirm that the remote control was added successfully
: git remote -v
Follow the examples in the following sections to see how the git fetch command works for various use cases. Retrieve a remote repository The easiest way to use the git fetch command is to get a remote repository: git fetch <remote name> For example: git fetch source
The output shows that the command got all the contents of the remote repository, including branches and tags.
List all remote branches recovered with
: git branch -r
To list all retrieved tags, run
: git tag Use the git
checkout command to make content available locally for a new branch, or use git merge to synchronize information from the local repository.
Get a specific branch To get a specific branch of a repository, run this command: git fetch <remote name> <branch name>
For example, to retrieve a branch named test from the source, run:
git fetch origin test The
command
retrieves only the contents of
the specific branch. To retire the obtained content to a new branch, run:
git checkout -b test_branch source/test
Content is now available locally in the test_branch branch
. Get all branches of all remotes In cases where there are multiple remotes,
git fetch offers a command to retrieve information from all remotes. To view
all remote controls,
Run
: remote git
Retrieve all contents of
all remote controls with: git fetch -all
The output shows the process of getting all remote repositories
. Synchronize the local repository Use the git fetch command with git merge to synchronize
the local repository
. Follow the steps below to see how the example works:
1. Find the remote repository with:
git fetch <remote name>
2. Compare the local branch with the remote branch by listing the commit differences:
git log -oneline <local branch>.. <remote name>/<remote branch name
> For example, if the local branch is test_branch and the remote branch is in source/test, the following command lists the differences:
git log -oneline test_branch.. Source/Test
The result lists the confirmations and associated messages
.
3. Check out the local branch where you want to merge changes
: git checkout <local branch>
For example:
git checkout
test_branch 4. Synchronize the local branch with the git merge command: git merge
origin/main
The content is now updated with the remote repository. If there is a conflict warning, follow our tutorial to resolve merge issues: How to resolve merge conflicts in Git.
git fetch Vs. git pull The following table describes how the git fetch and git pull commands compare. git fetch git pull
Of the two commands, git fetch is the
safer choice when retrieving code changes from a remote repository. Conclusion After reading this tutorial, you know what the git fetch command is, when to use it, and how it compares to the git push command. Next, check out our Git Command Cheat Sheet, which introduces the git fetch command
.