
git pull Doesn't Just Download: It's fetch Plus merge (or rebase)
- كاتب موثّق
محتويات المقال
git pull "just to download what the team pushed". The terminal answers with CONFLICT (content): Merge conflict in checkout.js, the file is full of conflict markers, and his branch is in the middle of a merge he never asked for. He wanted to look. Git changed his branch.
The common misconception
git pull downloads the latest changes from the remote. It's the opposite of git push, the command that gets you "up to date", and it's safe to run any time.
Updating f3f6d28..85d5919 and Fast-forward: the branch simply moved ahead.
What actually happens
Your clone keeps bookmarks of the remote
origin/main. Pro Git describes them as "local references that you can't move; Git moves them for you whenever you do any network communication." Your own main and the bookmark origin/main are two different pointers, and they can point at different commits.
git fetch is the download
git fetch downloads the commits you don't have yet and moves the bookmarks, so origin/main now matches the remote. Your branches stay where they were, and Pro Git says fetch "will not modify your working directory at all." Afterwards you can compare main with origin/main and decide what to do.
git pull adds an integration step
git pull docs describe two steps: "First, git pull runs git fetch with the same arguments", then it picks the branch to integrate, by default the upstream of your current branch, and "integrates that branch into the current branch." That second step has several modes. A moves your branch ahead when you have no commits of your own. --no-rebase runs git merge, --rebase runs git rebase, and the pull.rebase and pull.ff settings choose for you. When your branch and the remote have diverged and nothing chooses, the docs say pull does "fast-forward" updates only and "fails if your local branch has diverged". In our test, a diverged pull with no settings stopped with fatal: Need to specify how to reconcile divergent branches., after it had already fetched.
Which mode you get depends on configuration
git config --show-origin --get-regexp '^pull' printed pull.rebase false, read from C:/Program Files/Git/etc/gitconfig, the system file Git for Windows installs. So a plain git pull there means fetch plus merge. Git's own hint for diverged branches suggests git config pull.rebase false, so it's an easy value to set once and forget. Rebasing on pull is another choice with its own trade-offs; a later article compares merge and rebase.
The fix: download first, then decide
git pull # "just download what the team pushed"# From ../shop# f3f6d28..85d5919 main -> origin/main# Auto-merging checkout.js# CONFLICT (content): Merge conflict in checkout.js# Automatic merge failed; fix conflicts and then commit the result.git status --short# UU checkout.jsgit fetch # download only: moves origin/main, nothing else# From ../shop# f3f6d28..85d5919 main -> origin/maingit status -sb# ## main...origin/main [ahead 1, behind 1]git log --oneline main..origin/main # what they pushed# 85d5919 Add shipping to totalgit diff --stat main...origin/main# checkout.js | 2 +-# 1 file changed, 1 insertion(+), 1 deletion(-)origin/main. The difference is everything after it. The pull went on to merge, hit a conflict and left checkout.js unmerged (UU). The fetch stopped and showed the situation: one commit on each side, and the incoming one touches checkout.js, the file Omar changed. Running git merge origin/main right after that fetch printed the exact same CONFLICT lines. That merge is all the pull had added, but now Omar would run it on purpose, when his work is ready.
git pull did on Git 2.54 | |
|---|---|
Need to specify how to reconcile divergent branches | |
pull.rebase false | |
pull.ff only | Not possible to fast-forward, aborting |
How to apply it in your project
Find out what your pull does Run git config --show-origin --get-regexp '^pull'. It lists every pull setting and the file it comes from, including system files you never edited. No output means Git's default: fast-forward, or stop.Use git fetch when you want to look git fetch, thengit status -sbfor ahead/behind counts,git log main..origin/mainfor the incoming commits andgit diff --stat main...origin/mainfor the files they touch. None of it changes your branch.Integrate on purpose Merge or rebase origin/mainwhen your work is committed and you have time for conflicts. Or setpull.ff onlyso pull never decides for you.Commit or stash before integrating When uncommitted edits sit in files the incoming commits change, the merge refuses to start, as the table shows. Start from a clean git status.
Key takeaways
git pullisgit fetchfollowed by integrating the fetched branch into your current branch.git fetchis the real "download": it movesorigin/mainand leaves your branches and files alone.Whether pull fast-forwards, merges, rebases or stops depends on settings that can come from files you never edited. When you have local commits, a pull can create a merge commit or leave you mid-merge with conflicts. Fetch to look, integrate on purpose, and consider pull.ff onlyso pull never decides for you.
التعليقات (0)