git pull
does not do what you think it does.
In particular, git pull
does not "pull files" at all!
What git pull
does is:
- run
git fetch
;
- run a second Git command, usually
git merge
.
The first command—the git fetch
part of git pull
—has your Git call up some other Git, usually the Git at the URL you have stored under the name origin
. This other Git may have some commits that you do not have yet. If so, the other Git gives those commits to your Git, along with the branch names that the other Git uses to remember those commits. Your Git then renames their branches, so that your Git remembers their master
under your name origin/master
, their dev
under origin/dev
, and so on.
Having learned of any new commits they have that you don't, it's now possible for your Git to run git merge
on one of those commit hash IDs. If you have not told it otherwise, your git pull
command does that now.
A merge operation merges "what you did"—which in this case, includes "set this file back in time"—with "what they did". To do this, the merge must first find a commit that you share with them. If you're on your master
branch, for instance, you might find the last commit you have that is also on your origin/master
(this being your Git's way of remembering their master
). Your Git can then compare:
What was in this old commit, and what's in your current commit? Whatever is different, you must have changed.
What was in this old commit, and what's in their current commit? Whatever is different, they must have changed.
If they did not change the file, and you did change the file—you did change the file, you said so yourself—then Git will keep your changes.
(Note that if you have not committed your changes, doing this kind of git merge
is tricky and not a good idea. If you don't want your changes, you can discard them using git reset
or git checkout
; any reasonably modern Git will give you advice on this with git status
. If you do want to keep your changes, you should commit them.)