Dev Blog

Blog articles that make it func to craete something with the Web. Git, HTML and Mac utilities.

How to force “git pull” ?

Latest Update: 2017-09-21 by Joe

When you are following updates on a remote branch (e.g. the master branch on origin), sometimes you get a conflict between your local master branch and remote’s.

This article is about how to solve the problem.

Conflict between a local branch and a remote branch

“git pull” is the most use full Git command to merge any update made on the master that the local branch is following.

So, it’s frustrating to see the error message like this:

Conflict.. , the sound of the Git term has never been so comfortable for me, or maybe for any developer.

When you see this message, maybe you’ll think:

“All I need to do is, just make the local master up to date.  Overwrite the local with the remote’s master”.

How to FORCE “git pull”?

The questions is, “is it possible to FORCE the execution of git pull?”

OK, here’s the answer first: Yes, it is. But there’s not an option like “git pul --force”.

So, Check out the command below. Be sure you are on your local master branch before the execution.

As you see, this is not a command with “git pull” at all. However, I bet it’ll do what exactly you want to do,  “git pull --force”, that is.

So, what exactly is going on?

Again, there’s no command options like “git pull --force”. But the command shown above does almost the same thing. Here’s a little bit of the elaboration.

What exactly “git pull” does

Well, getting back from the digression, as you see in the above figure, “git pull” is a short hand of “git fetch” and “git merge”. and this is exactly what’s executed when you run “git pull origin master”

(1) git fetch updates the remote tracking branch. and (2) is the merge of the remote tracking branch into the local branch.

JFYI, FETCH_HEAD is the reference to the commits that have been fetched when you have run “git fetch”. You can find it at .git/FETCH_HEAD. There’s should be some set of commit SHA1 and ref names written in a text file.

In the above case, what’s actually merged in “origin/master”.

How to force git pull

The remote tracking branch updated by “git fetch”

If you’re not so familiar with the term “remote tracking branch”, maybe you might want to know more about it.

A remote tracking branch is a “reference” to a branch on a remote repository. Since a remote tracking branch is just like a mirrored version of a branch that exists on a remote repository, it’s not something you edit or work on. It’s just used as a reference. So they are basically treated differently than your local branches.

If you have pushed a local branch to a remote (or fetched a branch from a remote, ) you should have a remote tracking branch that is tracking the remote’s branch with the same branch anme.

Check remote tracking branches

Try the command to check it:

The option of “-a” is to show all the local branches including the remote tracking branches.

The option of “-vv” is just useful because it shows not only local branches, but the corresponding upstream branch for each local branch.



Here are useful links about git pull force:

OK, this is about it.

Enjoy your project. Take care.