How to ignore error on git pull about my local changes would be overwritten by merge?

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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.

回答1:

If you want remove all local changes from your working copy, simply stash them:

git stash save --keep-index 

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

git stash drop 

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.


回答2:

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 


回答3:

This works for me to override all local changes and does not require an identity:

git reset --hard git pull 


回答4:

Here is a solution that throws away staged changes:

git reset file/to/overwrite git checkout file/to/overwrite 


回答5:

You can either commit your changes before you do the merge, or you stash them:

  1. git stash
  2. git merge origin/master
  3. git stash pop


回答6:

My solution to solve this issue was:

git checkout -- 

Then I could overwrite the file just using:

git pull 


回答7:

If your repo contains few files which is removed from master:

  1. git checkout master
  2. git fetch origin
  3. git reset --hard origin/master
  4. git checkout -b newbranch


回答8:

In the recent Git, you can add -r/--rebase on pull command to rebase your current branch on top of the upstream branch after fetching. The warning should disappear, but there is a risk that you'll get some conflicts which you'll need to solve.


Alternatively you can checkout different branch with force, then go back to master again, e.g.:

git checkout origin/master -f git checkout master -f 

Then pull it again as usual:

git pull origin master 

Using this method can save you time from stashing (git stash) and potential permission issues, reseting files (git reset HEAD --hard), removing files (git clean -fd), etc. Also the above it's easier to remember.



回答9:

Sometimes, none of these work. Annoyingly, due to the LF thing I think, what will work is deleting the files, then pulling. Not that I recommend this solution, but if the file doesn't exist, git won't uselessly inform you that your changes (which may not even be changes) will get overridden, and will let you continue.

Use at your own risk.



回答10:

This problem is because you have made changes locally to file/s and same file/s are exists with changes in git repository, so before pull/push you will need stash local changes:

To overwrite local changes of a single file:

git reset file/to/overwrite git checkout file/to/overwrite 

(In case, ) To overwrite all the local changes (Changes in all files):

git stash git pull git stash pop 

Also this problem may be because of you are on a branch which is not merged with master branch.



回答11:

You can use this for overwrite file

git checkout file_to_overwrite 


回答12:

If you want to overwrite specific changes, you need some way of telling it which ones you want to forget.

You could try selectively stashing the changes you want to abandon using git stash --patch and then dropping that stash with git stash drop. You can then pull in the remote changes and merge them as normal.



回答13:

The best way to solve this problem is:

git checkout -- 

After that you can overwrite the file by:

git pull origin master 


回答14:

I had a special case of this: I had a file with --assume-unchanged on it. It was hard to locate, as the git status command was not showing any changes



回答15:

This worked for me to discard changes in the live remote server and pull from the source control github

git reset --hard git pull origin master 


回答16:

I was ignoring a file in my repo and when I did git pull upstream master I got the following error:

error: Your local changes to the following files would be overwritten by merge: myfile.js Please, commit your changes or stash them before you can merge. Aborting

To resolve it I did the following

git update-index --no-assume-unchanged myfile.js 

I then did git status and got this message

On branch master Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded. (use "git pull" to update your local branch)

Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory)

modified: myfile.js

no changes added to commit (use "git add" and/or "git commit -a")

Then I did git checkout myfile.js followed by git pull upstream master. This time the git pull operation was successful.



回答17:

I encountered this when pulling from the master.

The way I handle it. Using Visual Studio,

  1. First, I performed Undo commit on my solution.
  2. Then do the Git pull process.

Hope this helps!



回答18:

If you want to keep production changes on the server, just merge into a new configuration items, processing method is as follows: git stash git pull git stash pop maybe you don't exe all operation,you can know what you can do next



回答19:

If this error is because of line endings,

git add git checkout mybranch 

will work. I'm not really sure why it works.



回答20:

Here is my strategy to solve the problem. This will be a bit long post but I hope this will help large number of people looking for solution.

Problem Statement

We need to make changes in more than 10 files. We tried PULL (git pull origin master) but GIT shouted.

error: Your local changes to the following files would be overwritten by merge: Please, commit your changes or stash them before you can merge.

We tried execute to commit and then did pull but the didn't work also.

Solution

We were in DIRTY stage actually, because the files were in "Staging Area" a.k.a "Index Area" and some were in "Head Area" a.k.a "local GIT directory". And we wanted to pull the changes from server.

Check this link for information about different stages of Git in a clear manner GIT Stages

We followed the following steps

 - git stash ( this made our working directory clean. Your changes are stored on stack by GIT )   - git pull origin master ( Pull the changes from the server )   - git stash apply ( Applied all the changes from stack )    - git commit -m 'message' ( Committed the changes )   - git push origin master ( Pushed the changes to the server )   - git stash drop ( Drop the stack ) 

Lets Understand When and Why you need Stashing

If you are in DIRTY state, means you are making changes in your files and then you are compelled, due to any reason, to PULL or Switch to another branch for some very Urgent work, so at this point you can't PULL or Switch until you commit your change. Stash command is here as a helping hand.

From the book ProGIT 2nd Edition

Hope this will be beneficial for you guys.



回答21:

For Pycharm, you can do Git-->Revert and then pull.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!