Can I tell git pull to overwrite instead of merge?

Michael Stum picture Michael Stum · Apr 19, 2010 · Viewed 21.6k times · Source

As far as I see, git pull someRemote master tries to merge the remote branch into mine.

Is there a way to say "Completely discard my stuff, just make me another clone of the remote" using git pull? I still want to keep my own repository and keep it's history, but I want to have a 1:1 copy of someRemote's master branch after that command.

To clarify, imagine there are 2 repositories, RM and MY. Numbers are commits, and this assumes only one branch (master).

RM1 --- RM2 --- RM3 --- RM4 --- RM5 --- RM6 ...
|                        |
+-> MY1 --- MY2 --- MY3 -+-> MY4 --- MY5 --- MY6 ...

So I start my own repository as a clone of RM1. Then I develop happily and RM develops happily, but we never share our work. After MY3 I realize that my branch isn't that great but that RM4 is pretty good. So I want to git pull RM4 into MY. However, I don't want my changes in MY1-3 to persist, I want MY4 be a 1:1 copy of RM4.

However, I want to keep my history, ideally I would like to have a change set between MY3 and RM4 or between MY3 and RM2-4.

It should still stay my repository.

Is that possible?

(This is for GitHub projects where I may fork a project, experiment a bit, leave it alone for a few weeks but then want to update it without keeping my changes. At the moment I delete my fork and re-fork, which isn't the best approach.)

Answer

Greg Hewgill picture Greg Hewgill · Apr 19, 2010

First, rename your master branch to something else:

git branch -m master my_old_master

Then, create a new master:

git checkout -b master someRemote

The great thing about Git's branch names is that they aren't actual places themselves, they're just pointers to places (where a "place" is a SHA1 commit id).