tags:

views:

409

answers:

3

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.

Edit: 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 some 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 :))

+1  A: 

Sounds like you're after git checkout, which will discard your local changes to a path.

You can revert your changes using checkout:

git checkout myfile.h

will restore myfile.h from the index

http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html

Michael Shimmins
I thought checkout only resets my local working copy to the last revision in my repo. Does it work with remotes?
Michael Stum
Yes, `git checkout` takes a commitish argument, so `git checkout origin/foo bar.baz` is possible.
Bombe
+7  A: 

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).

Greg Hewgill
Basically I have to nuke my branch? Is there a way to have git magically merge the changes so that it looks like a new revision? I would like to keep my branch and it's history, I just want to make the current revision be a 1:1 copy of the current revision of a remote. Or would that still work and the new master shares the history with the old master?
Michael Stum
@Michael: Your changes would not be gone; they still exist in the `my_old_master` branch. Your goals of "1:1 copy of the remote" and "keep my history" are incompatible if you only want one branch. Because a revision SHA1 includes the SHA1s of all history, a branch isn't the same as another branch unless *all* the history is identical in both branches.
Greg Hewgill
+1  A: 

If you want to keep your current master branch but register a new version (mirroring the remote branch), you can;

  • register a merge filter driver (just for this merge: a keepTheir one)
  • do a git pull --no-commit
  • check if there isn't extra files that need to be removed (files in your master that were not present in the remote branch)
  • commit
VonC