So - can someone clarify this one:
I run:
git pull origin master
git status
And it then pulls the changes and says:
your branch is ahead of origin/master ... blahblah by 6 commits...
When I then run
git fetch
git status
It says:
# On branch master
nothing to commit (working directory clean)
So - I thought git pull
does git fetch by default - so why does it says "ahead by 6 commits" without additional git fetch
?
git fetch is similar to pull but doesn't merge. i.e. it fetches remote updates ( refs and objects ) but your local stays the same (i.e. origin/master gets updated but master stays the same) . git pull pulls down from a remote and instantly merges. git clone clones a repo.
When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn't make any changes to your local files. On the other hand, Git pull is faster as you're performing multiple actions in one – a better bang for your buck.
It's important to fetch and pull before you push. Fetching checks if there are any remote commits that you should incorporate into your local changes. If you see any, pull first to prevent any upstream merge conflicts.
The git pull command first runs git fetch which downloads content from the specified remote repository. Then a git merge is executed to merge the remote content refs and heads into a new local merge commit.
The "ahead or behind by X commits" text in git status
is based on the state of the tracking branch for the current branch; remotes/origin/master
if you're on master
, for example.
When you run git pull
with both a remote and a branch specified, it fetches the new commits and merges them in to the current branch, but it does not update origin's remote tracking branches. Instead, it points to the just-fetched commits as FETCH_HEAD
.
Running git fetch
with no arguments specified, on the other hand, does update all of the remote tracking branches, so it makes the message go away. git pull
with no arguments does the same.
A subtle gotcha that I've hit a bunch of times myself! I wish git updated all remote tracking branches on every fetch against a particular remote, instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With