Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore an error on 'git pull' about my local changes would be overwritten by merge?

How do I ignore the following error message on Git pull?

Your local changes to the following files would be overwritten by merge

What if I want to overwrite them?

I've tried things like git pull -f, but nothing works.

To be clear, I only want to overwrite specific changes, not everything.

like image 448
mae Avatar asked Jan 14 '13 12:01

mae


People also ask

How do I ignore an error on git pull about my local changes?

git reset --hard origin/<branch> did help!

How do you fix git error your local changes to the following files will be overwritten by merge?

The “Your local changes to the following files would be overwritten by merge” error occurs when you try to pull a remote repository to your local machine whose contents conflict with the contents of your local version of the repository. To fix this error, either stash your changes away for later or commit your changes.

Does git pull without overwriting local changes?

No - it only changes the repository, tags snd remote heads - it never changes the working tree, local branches or the index. git pull can change local branches, the local tree and the index. It won't overwrite charges but may do a merge, a rebase or fail if there are conflicting changes.


2 Answers

If you want remove all local changes - including files that are untracked by git - from your working copy, simply stash them:

git stash push --include-untracked 

If you don't need them anymore, you now can drop that stash:

git stash drop 

If you don't want to stash changes that you already staged - e.g. with git add - then add the option --keep-index. Note however, that this will still prevent merging if those staged changes collide with the ones from upstream.


If you want to overwrite only specific parts of your local changes, there are two possibilities:

  1. Commit everything you don't want to overwrite and use the method above for the rest.

  2. Use git checkout path/to/file/to/revert for the changes you wish to overwrite. Make sure that file is not staged via git reset HEAD path/to/file/to/revert.

like image 200
Daniel Hilgarth Avatar answered Sep 20 '22 16:09

Daniel Hilgarth


Alright with the help of the other two answers I've come up with a direct solution:

git checkout HEAD^ file/to/overwrite git pull 
like image 42
mae Avatar answered Sep 18 '22 16:09

mae