0

I'm updating my code with git pull, but got commit conflict in one file. I want to take changes from pull and overwrite what was done in local repo. Is there some command to simply take changes from pull and overwrite local changes?

Or do I need to manually edit such file/files?..

Andrius
  • 19,658
  • 37
  • 143
  • 243

2 Answers2

0

Try this:

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

P.S.: Possible duplicity: Force Git to overwrite local files on pull

Community
  • 1
  • 1
Zlopez
  • 660
  • 1
  • 5
  • 11
0

As I understand your scenario:

  • You have local changes on a branch that has a remote.
  • You encountered a conflict in a file when attempting to pull remote's changes into your local branch.
  • You wish to instead overwrite the changes on your local branch to match those from the remote branch.

Simply put, it's doable, but it'd be better for you to resolve your conflicts instead in case you lose work.

We can't use git pull for this since that does a git fetch and git merge operation, but what we can do is move the reference of your local branch to match that of the remote branch.

To do that, we will use git fetch and git reset.

git fetch && git reset --hard origin/BRANCH

This will throw your local changes away and your branch will now match the remote. I cannot stress enough that, if you do this, your local changes will be a fair bit tricker to recover (there are ways to do it through the reflog), so before you try this, be sure that this is what you want to do.

It sounds like you want to keep your local changes, pull the latest changes, and have your changes on top of that instead. That can be accomplished with git rebase BRANCH. You'll still have to resolve the conflict, but the conflict will come before your work in history, making it slightly easier to deal with.

The final alternative to keep your original work is to stash it. Move your existing, non-pushed commit back into staging with git reset --soft HEAD~1, and follow that up with git add . && git stash so that your changes are kept in the stash. Should you want to apply/pop the stash to the branch, you'll still have to resolve the conflict, but it'll be more straightforward to do.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • No, I actually want to overwrite my local changes. The thing is that repo is only used to make pull. Local changes usually are not made, only when there is a bug and the only way to fix (at that time) is to modify it locally. Changes are made in other repositories that are related with that code (with dependencies), but it is in separate directory to avoid conflicts (or in other way that repo is source code which is used to inherit it and add additional functionality) – Andrius Feb 09 '15 at 07:51
  • Wait, what? You're telling me that this branch is pull-only? I'm a little confused. – Makoto Feb 09 '15 at 07:52
  • Yes. What is so confusing? Whole branch is used in OOP principle. I mean it is not meant to be modified, but its code only inherited in other modules. If I need to be specific, that branch is this: https://github.com/odoo/odoo. And my code/configurations are on separate repository and some other files. So this way I don't directly modify standard functionality. In other words git pull is like software update that I don't do any changes, just update it to have newer version. – Andrius Feb 09 '15 at 07:58
  • This is why I'm confused: each branch is unto itself; that is, unless you explicitly instruct it to, Branch A and Branch B will only ever have a single common ancestor between them, which may not be indicative of the current state of that common ancestor at all. Since you don't want to accidentally update the state of the master branch (I would imagine that any contributor would reject it anyway, though), you should consider `rebase` instead, so you could just replay your work as if you had created the branch from the tip of `master`. – Makoto Feb 09 '15 at 08:30
  • The thing is that branch (better word repository, because all branches in the repository are not modified by me. But it happens that we use only one branch from that repository) is not directly related with my code. I use it as a base, but do not modify it in any way. Don't know how to put it, its like a core for OS or something. And my branch is not created from that branch, in system they have different location paths (this way they are related, because both are found by system). Thats why I'm saying repository, not branch, because there are two different repositories. – Andrius Feb 09 '15 at 09:05