Tola Odejayi
Tola Odejayi

Reputation: 3059

How do I forcibly ovewrite a local repo with changes from a remote repo while still preserving history?

I know that if I want to forcibly overwrite a local repo with files from a remote repo, I do this:

git fetch --all
git reset --hard origin/master

The problem with this is that when I go to look at the history of the local repo, it has completely obliterated the history and replaced it with a carbon copy of the history of the remote repo.

Is there a way I can do this forcible overwrite while still preserving the history?

For example, if I have a file in the local repo with the following content:

- ContentA
- ContentB
- ContentC

And I have the same file in the remote repo with the following content:

- ContentA
- ContentC
- ContentD

After the pull from the remote repo, I want the history to show this for the file:

Version #1
- ContentA
- ContentB
- ContentC

Version #2
- ContentA
- ContentC
- ContentD

I don't want it to show this (from a forcible overwrite):

Version #1
- ContentA
- ContentC
- ContentD

Nor do I want it to show this (from a merge):

Version #1
- ContentA
- ContentB
- ContentC

Version #2
- ContentA
- ContentB
- ContentC
- ContentD

Is this possible?

Upvotes: 1

Views: 64

Answers (1)

David Neiss
David Neiss

Reputation: 8237

You can't have the remote do a merge for you. You need to do the the merge and then you push to it what you have and it just fast forwards up to your commit. So you need to arrange your commits to resolve whatever you want it to look like and then push that

You can do a merge with a --no-commit option and then pick which side's files you want with a git checkout --ours or git checkout --theirs, then git add those files and finally git commit to conclude the merge, to select which of the file changes you want. As for the history that you want, well you get to edit the actual commit comments, if thats what you want.

Upvotes: 1

Related Questions