You're right that the local name could be outdated. Note, however, that there is a race here, no matter how clever you are with "grab the latest from some other repository, then use the latest we grabbed" because some time elapses between "grab" and "use". No matter how small you make that interval, someone else could sneak in between those two moments.1
In the end, then, the attempt to be "up to date" is simply a best effort at avoiding major issues. That's still a good idea, of course.
While kosist's answer is fine, you need not actually use git pull
, which just runs git fetch
followed by a second command (generally git merge
or git rebase
). You can do the equivalent manually, and that's what I usually prefer for personal reasons. Now that git pull
has a built in "merge-with---ff-only
" option, I might eventually start using git pull
more, but several decades worth of habits can be hard to break.
One other note applies here: since Git 2.23 came out, I recommend using git switch
rather than git checkout
for the purpose of changing or creating branch names like this. Switching to git switch
is equally hard in terms of habit-breaking but I find it has been pretty valuable. The checkout
command is too all-in-one, bundling most of git restore
into itself as well, and it's too easy to accidentally invoke the wrong behavior (via a simple typo).
1Assuming, that is, that either there's no smallest unit of time in the universe, or that in terms of this fundamental "graininess" of time, the elapsed time between "grab" and "use" is greater than one "grain". However, we don't really need that kind of assumption since in practice, the work happens on human timescales (many seconds, minutes, hours, etc.) while the computers operate in nanosecond timescales, so there are always multiple billions of "computer grains".