开发者

Can I tell git pull to overwrite instead of merge?

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开发者_Python百科 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.)


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜