Junior
What is the difference between the pull and fetch commands in Git?
sobes.tech AI
Answer from AI
git fetch downloads changes from a remote repository but does not apply them to the current branch.
git pull performs git fetch, then automatically merges the fetched changes with the current branch.
Key differences:
| Command | Action | Impact on working copy / branch |
|---|---|---|
git fetch |
Downloads objects (commits, files) | Does not change the working copy or current branch |
git pull |
Downloads objects and merges changes | Updates the current branch and working copy |
Example usage:
To simply see what has changed on the remote server:
git fetch origin
(You can check changes with git log origin/<branch>)
To get changes and automatically apply them to the current branch:
git pull origin main # for example
git pull is essentially equivalent to executing git fetch and then git merge or git rebase (depending on settings).
# Equivalent to git pull
git fetch origin
git merge origin/main # if the current branch is main and merge strategy is used
Using git fetch provides more control, as it allows you to review incoming changes before merging. git pull is convenient for quick synchronization.