What Does git fetch Do?

In short, it updates the local copy of the remote repository without changing the state of the project’s files. When you run the git fetch command, Git communicates with the remote repository and downloads all new commits and updates that have been added since your last interaction. For example, after executing this command, you can switch to new remote branches.

Typically, when using Git through an IDE or Git client, you don’t have to worry about this command as it is executed implicitly. However, understanding its principles becomes critically important when working with Git via the console.

Here are the key functions of the git fetch command:

  • It updates information about all remote branches so you can see the latest changes and commits.
  • This command does not change the files and branches in your working directory, so there should be no “surprises”.
  • It allows you to review all changes and updates before integrating them, helping to avoid conflicts and errors when merging branches.

Main Options and Commands

Specifying Remote Repository and Branches

Updating data from a specific remote repository <remote>:

git fetch <remote>

Updating data from a specific branch <branch> of the remote repository <remote>:

git fetch <remote> <branch>

Options for Managing Command Behavior

  • --all: Updates data from all remote repositories configured for this repository.
  • --dry-run: Shows what data will be downloaded but does not perform the actual update.
  • --force or -f: Forces data update even if it overwrites existing changes.
  • --tags: Downloads all tags from the remote repository.
  • --prune: Removes local branches that have been deleted in the remote repository.

Options for Output Detailing

  • --verbose or -v: Shows more detailed information about the command execution process.
  • --quiet or -q: Disables process execution messages.

Options for Managing References

  • --refmap=<refspec>: Allows you to set rules for mapping branches between the local and remote repositories.
  • --depth=<depth>: Limits the number of downloaded commits to the specified number.

Git Pull VS Git Fetch

“Why do I need to know git fetch if I’ve already learned git pull?” a beginner developer once asked me while we were discussing the idea for this article. Indeed, in what situations does knowing only git pull not suffice?

It’s actually very simple. The git pull command is the sequential execution of two commands: git fetch and git merge. It updates data in the local repository (git fetch) and applies these changes in your working directory (git merge).

When is it Necessary to Use git fetch?

For example, you are working locally in the dev branch and want to know what changes have been made to the remote main branch before merging these changes into the dev branch. Naturally, you don’t want to switch from your branch or change the state of the main branch.

git fetch origin
git log HEAD..origin/main --oneline

Or when you want to know about new branches that have been created in the remote repository:

git fetch origin
git branch -r

Or when you want to update your branch with changes from a remote branch without merging:

git fetch origin
git checkout feature
git reset --hard origin/feature

Another common example is when you want to create a new branch based on the latest changes from the main branch:

git fetch origin
git checkout -b new-feature origin/main

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.

Similar Posts