Do you fetch or pull

I’m used to, when I want to update my local git repository with the distant one, to use git pull It works well. Until …

A few days ago, I wanted to release the version 1.0.0 of jesue. And to do so, I wanted to update the documentation. The principle is simple. I use GitHub Pages. So I need a branch “gh-pages”, which content is what’s online at GitHub for my documentation. Whenever I commit on that branch, the online page is updated.

However my two branches master and gh-pages doesn’t have the same index. So they can’t be merged ! So I did

git pull origin gh-pages:gh-pages

Thinking “I get the content of my documentation branch”. And I see a dozen of conflict errors ! WTF as we should say.

And I should have done

git fetch origin gh-pages:gh-pages

The difference between those two commands apparently similar is huge. Git fetch only gets the datas. Git pull gets them and merges them with the current branch … So when merging my two branches, as they don’t have the same base, it’s not cool.

Morality : you can git fetch when you want to update your local copy with the last distant commits. The branch on which you do the fetch will have the last commits. But your working branch won’t be modified. If you do a pull, not only the branch where you do the fetch will have the last distant commits. But your working branch will also be merged with those last commits.

After a second thought, fetch seems highly more recommandable that pull, which can break a repository pretty easily.