Both get changes from a remote repository, but git pull also changes your current branch, while git fetch does not. git fetch - Downloads new commits, branches and tags from the remote and updates your remote-tracking branches (for example origin/main). - Does not modify your working directory or your local branches. git fetch origin git log main..origin/main # see what is new on the remote git diff main origin/main # inspect the changes - Safe to run at any time; you decide what to do next (merge, rebase or ignore). git pull - Equivalent to git fetch followed by integrating the remote changes into your current branch: - By default with a merge (git fetch + git merge origin/main), which may create a merge commit. - With --rebase (or pull.rebase=true in configuration), your local commits are replayed on top of the remote changes for a linear history. - With --ff-only, Git only updates if a fast-forward is possible and refuses otherwise — a safe default many teams prefer. git pull origin main git pull --rebase - Can produce merge conflicts immediately, because it changes your branch. Key differences - What it does: fetch downloads changes only; pull downloads and integrates them. - Effect on local work: fetch none; pull updates your current branch and working tree. - Safety: fetch is always safe; pull can create merge commits or conflicts. - Control: fetch lets you review before integrating; pull is a convenient shortcut. When to use fetch - To check what teammates pushed before deciding how to integrate. - To update your view of all remote branches (git fetch --all --prune also removes deleted remote branches from your list). - In scripts and CI pipelines where you want explicit control. - Before rebasing a feature branch: git fetch origin then git rebase origin/main. When to use pull - On a branch with no local changes, or when you simply want to bring your branch up to date and are comfortable with the configured strategy. Recommended configuration git config --global pull.ff only or git config --global pull.rebase true These avoid unexpected merge commits from routine pulls. Interview summary: "git fetch downloads remote changes into remote-tracking branches without touching my work; git pull is fetch plus merge (or rebase) into my current branch. I often fetch first to review changes, then merge or rebase deliberately."
Ravindra Wadile · · 0 likes