28

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

random
  • 9,774
  • 10
  • 66
  • 83
Michael Stum
  • 177,530
  • 117
  • 400
  • 535

3 Answers3

33

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
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 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 Apr 19 '10 at 05:33
  • 4
    @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 Apr 19 '10 at 05:49
  • Greg, I am not getting your 2 step process to work... after the git checkout I see the message `fatal: git checkout: updating paths is incompatible with switching branches. Did you intend to checkout 'git@github.com:myProject/myApp.git ' which can not be resolved as commit?` – Someone Somewhere May 21 '12 at 21:01
  • @SomeoneSomewhere: It sounds like you're trying to use that URL in a `git checkout` command. Perhaps you want to do the following: `git add remote my_project_on_github git@github.com:myProject/myApp.git` then `git fetch my_project_on_github` and finally `git checkout -b master my_project_on_github/master`. – Greg Hewgill May 21 '12 at 21:05
4

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://git-scm.com/docs/git-checkout

Michael Shimmins
  • 19,961
  • 7
  • 57
  • 90
  • I thought checkout only resets my local working copy to the last revision in my repo. Does it work with remotes? – Michael Stum Apr 19 '10 at 05:33
  • 1
    Yes, `git checkout` takes a commitish argument, so `git checkout origin/foo bar.baz` is possible. – Bombe Apr 20 '10 at 12:27
2

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 aren't extra files that need to be removed (files in your master that were not present in the remote branch)
  • commit
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250