Skip to main content

Git Fetch

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/git-fetch

In short

git fetch downloads new commits, branches and tags from a remote and updates remote-tracking branches like origin/main but not your own branches or files.

What does git fetch do?

Your local repository keeps a record of what each remote looked like the last time you talked to it, in remote-tracking branches such as origin/main and origin/feature-x. git fetch contacts the remote, downloads any commits you don't have yet and moves those remote-tracking branches to match. Your current branch, staging area and files stay exactly as they were.

That makes fetch the safe way to see what others have done. After fetching, git log main..origin/main lists commits on the remote that you haven't merged, and git diff main origin/main shows the changes. You can then decide to merge them, rebase onto them, or leave them for later.

git pull is essentially git fetch followed by a merge or rebase into your current branch. Fetching separately gives you a chance to look first. git fetch --prune also removes remote-tracking branches that were deleted on the server, and git fetch --all updates every configured remote. Many editors fetch in the background so they can show when your branch is behind.

A common misconception is that fetch updates your local branches. It never touches main itself; only origin/main moves. That is why a branch can show as behind its upstream after a fetch, until you pull, merge or rebase.

Key takeaways

  • git fetch downloads new commits and refs from a remote.
  • It updates remote-tracking branches such as origin/main.
  • Your branches, staging area and files are not changed.
  • git pull is fetch plus a merge or rebase.
  • --prune removes remote-tracking branches deleted on the server.

Example

Fetching and reviewing before mergingbash
git fetch origin --prune

git status
# Your branch is behind 'origin/main' by 3 commits, and can be fast-forwarded.

git log --oneline main..origin/main   # what arrived from the remote
git diff main origin/main             # the changes themselves

git merge --ff-only origin/main       # bring them in when ready
# or: git rebase origin/main

Readers ask

What is the difference between git fetch and git pull?

git fetch only downloads changes and updates remote-tracking branches. git pull fetches and then immediately merges or rebases those changes into your current branch.

Is git fetch safe?

Yes. It doesn't change your branches, staged changes or working files, so you can run it at any time to see what has changed on the remote.

What is origin/main?

A remote-tracking branch: your local copy of where the main branch on the remote named origin pointed the last time you fetched. It moves only when you fetch or pull.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings