Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

some confusion on git pull vs git-fetch [duplicate]

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?

like image 539
Dannyboy Avatar asked Dec 12 '13 17:12

Dannyboy


People also ask

What is difference between git fetch and pull and clone?

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.

Should I use git pull or fetch?

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.

Should I fetch before pull?

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.

Is git pull the same as fetch and merge?

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.


1 Answers

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.

like image 56
Ash Wilson Avatar answered Oct 28 '22 05:10

Ash Wilson