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
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/mainReaders 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
- Git PullVersion Control, p. 17Git pull is a Git command that downloads new commits from a remote repository and immediately integrates them into your current local branch.
- Git RemoteVersion Control, p. 19A Git remote is a named shortcut, such as origin, to another copy of your repository, usually on a server, that you push commits to and pull commits from.
- Git PushVersion Control, p. 18Git push is a Git command that uploads your local commits to a remote repository, updating the matching branch there so others can see and use your work.
- BranchVersion Control, p. 2A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.
- MergeVersion Control, p. 29A merge in Git combines the changes from one branch into another, joining separate lines of development back together into a single, shared history.
- RebaseVersion Control, p. 33A rebase in Git replays a branch's commits on top of another commit, rewriting history to produce a cleaner, linear sequence of changes without merge commits.
Spotted a mistake or something missing on this page?Suggest an edit