Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell git pull to overwrite instead of merge?

Tags:

git

git-pull

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

like image 452
Michael Stum Avatar asked Apr 19 '10 03:04

Michael Stum


People also ask

Does git pull merge or overwrite?

git pull --force it feels like it would help to overwrite local changes. instead, it fetches forcefully but does not merge forcefully ( git pull --force = git fetch --force + git merge ). Like git push, git fetch allows us to specify which local and remote branch we want to work on.

How do I allow git to overwrite local files on pull?

Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force ) allows overwriting local branches.

Do we need to merge after pull?

In a pull request, you propose that changes you've made on a head branch should be merged into a base branch. By default, any pull request can be merged at any time, unless the head branch is in conflict with the base branch.


2 Answers

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

like image 72
Greg Hewgill Avatar answered Sep 23 '22 17:09

Greg Hewgill


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

like image 20
Michael Shimmins Avatar answered Sep 22 '22 17:09

Michael Shimmins